There are two lists, string and query, both containing strings. I want to create a function that returns a 2D array showing:
- the number of occurrences of each element as a substring in the query list in the string list, and
- the length of each element in the query list.
For example, if we have
- string = ['om', 'om', 'omg']
- query = ['om', 'omg', 'mg']
As:
- 'om' occurs thrice in string, and its length is 2, we get [3, 2]
- 'omg' occurs once in string, and its length is 3, we get [1, 3]
- 'mg' occurs once in string, and its length is 2, we get [1, 2]
So the function should return the array [[3, 2], [1, 3], [1, 2]]
I have tried the following:
def countstuff(string, query)
result = []
for i in query:
array = [[sum(i in j for j in string], len(i)]]
result.append(array)
return result
But before I get to try the function, it returns SyntaxError: invalid syntax.
I can't get my head around this, please help, thank you.
CodePudding user response:
def countstuff(string, query):
result = []
for i in query:
ocr=0
for j in string:
ocr =j.count(i)
lst=[ocr,len(i)]
result.append(lst)
return result
string = ['om', 'om', 'omg']
query = ['om', 'omg', 'mg']
print(countstuff(string,query))
have fun :)
CodePudding user response:
Hi after fiddling some more, I found another solution, thanks everyone.
def countstuff(string, query):
result = []
for i in query:
array = [sum(i in j for j in string)]
array.append(len(i))
result.append(array)
return result
CodePudding user response:
Simple way to do it:
string = ['om', 'om', 'omg']
query = ['om', 'omg', 'mg']
def countstuff(strings, querries):
result=[]
string = " ".join(strings)
for query in querries:
count= string.count(query)
length = len(query)
result.append([count,length])
return result
print(countstuff(string,query))
Output:
[[3, 2], [1, 3], [1, 2]]