# How to find the same number of words in each list?

There are 2 list.One word, the number of references you want.
The second list of words is obtained by
``````f = open('text.txt', 'r')
b = []
for line in f:
a = line.split()
for x in a:
b.append(a)
f.close()``````

In the file in addition the text contains punctuation, numbers, other debris.

Question - how can I get the number of occurrences of words from a list to a string?

PS there are some lists, the code preferably with the possibility of optimization.
March 20th 20 at 11:31
March 20th 20 at 11:33
Solution
If I understand correctly, you have several lists with words, mention of which it is necessary to count the rows in the file (list of strings)
spoiler
``````f = open('text.txt', 'r')
b = []
A = [] # List of words, mention of which you should count in the file
B = [] # List of words No. 2
for line in f:
a = line.split()
c = [] # List for normal words
for x in a:
for char in x: # Checks characters of the char in the word x
if not char.isalpha(): # If char is not a letter
x = x.replace(char, ") # Removes char that is not a letter
c.append(x.lower()) # Adds a normal word (without any !"-, characters) in the list (with lower case)
b.append(c) # Adds a line of normal words to the list
f.close()

for (i, j) in zip(A, B): # a Parallel bypass all lists with words whose number it is necessary to calculate (for example A and B)
for line in b:
print('String ', b.index(line), ': ', i, '=>', line.count(i), ' ', j, '=>', line.count(j))``````

PS Read the question, apparently it was on several files and one list with the words you want counted. Then just add another list of strings, as you did originally:
spoiler
``````g = open('text2.txt', 'r')
'...'

for i in A:
for (line1, line2) in zip(b, g): # Parallel bypass multiple lists with strings
print('Line b ', b.index(line1), ': ', i, '=>', line1.count(i), ' ', 'Line g' g.index(line2), ': ', i, '=>', line2.count(i))``````
Describe in more detail.Has the source text will wrap cycle the words using the split command + removal of special characters and numbers..Get a list of all the words in the text.Then I check for the presence therein of the words from other, previously known list.Since such a procedure with the same file should rotate 4 times - I'm looking for the best method. - fatima commented on March 20th 20 at 11:36
If the list of words from a file one, and lists with words that you need to check four. In this case the first option is suitable. Capital letters I noted a known list of words (in addition to A and B can be announced C and D, as all 4 of the list is known). And in parallel bypass 4 lists record view
``````for (i, j, k, l) in zip(A, B, C, D): # where i, j, k , l - the words of the famous lists
for line in b:``````

Thus you will simultaneously check the file directly to the 4 known list
The output data record as you will be easier to read. - elfrieda commented on March 20th 20 at 11:39
March 20th 20 at 11:35

Find more questions by tags Python