How to count how many times a word in a list appeared in-another list

Question:

I have 2 lists and I want to see how many of the text in list 1 is in list 2 but I don’t really know of a way to like combine them the output isn’t summed and I have tried sum method but it does it for all words counted not each word.

Code:

l1 = ['hello', 'hi']
l2 = ['hey', 'hi', 'hello', 'hello']
for i in l2:
    print(f'{l1.count(i)}: {i}')

Output:

0: hey
1: hi
1: hello
1: hello

What I want is something more like this:

0: hey
1: hi
2: hello
Asked By: Anthony Orlando

||

Answers:

You can use the in operator to check if each element in l1 is in l2. You can then use a Counter object to count the number of occurrences of each element in l1 that is also in l2.

Here is an example:

from collections import Counter

l1 = ['hello', 'hi']
l2 = ['hey', 'hi', 'hello', 'hello']

# Create a Counter object to count the occurrences of each element in l1 that is also in l2
counter = Counter()

# Loop over each element in l1 and check if it is in l2
for element in l1:
    if element in l2:
        # If the element is in l2, increment the count for that element
        counter[element] += 1

# Print the count for each element
for element, count in counter.items():
    print(f'{count}: {element}')

This will print the following output:

1: hi
2: hello
Answered By: kenjoe41

If you want to count how many times each word in l1 appears in l2, you can use a dictionary to keep track of the counts for each word. Here is one possible way to do this:

l1 = ['hello', 'hi']
l2 = ['hey', 'hi', 'hello', 'hello']

# Create an empty dictionary
counts = {}

# Loop through each word in l1
for word in l1:
    # Initialize the count for this word to 0
    counts[word] = 0
    # Loop through each word in l2
    for word2 in l2:
        # If the word from l1 appears in l2, increment the count
        if word == word2:
            counts[word] += 1

# Print the counts for each word
for word in l1:
    print(f'{counts[word]}: {word}')

This code will print the following output:
2: hello 1: hi
This approach allows you to count the occurrences of each word in l1 in l2, and print the counts in the desired format. You can further customize the code to suit your specific needs. For example, you could sort the counts by their values or print the counts in a different order, depending on your requirements.

Answered By: user7347835

I think a simple fix is to just flip the way you are looping through the lists:

l1 = ['hello', 'hi']
l2 = ['hey', 'hi', 'hello', 'hello']
for i in l1:
    print(f'{l2.count(i)}: {i}')

Output:

2: hello
1: hi
Answered By: ddastrodd

Try this


from collections import Counter

l1 = ['hello', 'hi']
l2 = ['hey', 'hi', 'hello', 'hello']

c = Counter(l2)


for a in l1:
    print(f"{c[a]}: {a}")
    c.pop(a)

print(*["0: " + a for a in c.keys()], sep='n')

OUTPUT

2: hello
1: hi
0: hey

Answered By: codester_09

To count the number of times a word in a list appears in another list, you can use a for loop to iterate over the first list and use the count() method to count the number of times each word appears in the second list. Here’s an example:

# define the two lists
list1 = ["apple", "banana", "cherry"]
list2 = ["apple", "grape", "cherry", "apple", "orange", "banana", "apple"]

# initialize a count variable
count = 0

# iterate over the first list
for word in list1:
    # count the number of times the word appears in the second list
    count += list2.count(word)

# print the final count
print(count)

This code will print 4, since there are four words from the first list ("apple", "banana", "cherry") that appear in the second list.

Answered By: Sbagaria2710
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.