I am trying to update an older script of mine for a class. Typically this script will just record a '1' if any of the items from a list appear in that line. However, now I want it to count and sum the number of times any of those unique words appear in that line. For example, using this list:
ess = ['jim','bob','sally','tom']
.
.
.
elif 'SCHOOL' in line:
csvfile.write( str(line.count(',') + 1)+ ',')
flag = 0
for staff in ess:
if staff in line:
csvfile.write('1')
flag = 1
break
if flag == 1:
csvfile.write('\n')
else:
csvfile.write('0\n')
Instead of simply recording a "1" if any of the names appear. I would like it return a sum of how many names appear in that line. For example, if both jim and sally appear in that line, return a "2"
You can simply run multiple count on the string/sentence.
or you can split
the sentence by " " (space) delimiter and go over that returned list and check each word is equal to one of your desired words:
I would of do something like that:
sum = 0
for line in lines:
split_list = line.split(" ")
for word in ess:
if word in split_list :
sum+=1
or alternative: sum = 0 for line in lines: for word in ess: sum += line.count(word)
There are two ways to attempt this:
1) You do not care how many times a name appears in the sentence as long as the name appears at least once:
def names_in_sentence(sentence_str):
return sum([1 for name in lis_names if name in sentence_str])
This is a fairly pythonic way of doing it. I am using list comprehension to create a list of 1's for each name that is there in the input sentence.
2) You do care about how many times a name appears in the sentence. ie if the sentence was "This is a bob, he's quite the bob', you would return 2:
def names_in_sentence(sentence_str):
return sum([sentence.count(name) for name in lis_names])
In this case, I use list comprehension to count the number of times each name appears in the sentence, and sum the list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.