Find repeated words in list python

I want to create a new list with non-repetitive spelling of the elements and a new list with the number of repeats.

Like this

list=["jim","jennifer","roy","roy","mike","jim","roy","jim",mike","roy"]

I want to create 2 lists like these:

  • list1=["jim","jennifer","roy","mike"] containing the unique elements of list
  • list2=[3,1,4,2] containing the number of occurrences of each unique element.

I tried to this

number_of_repeats=[]

for i in range(len(list)):
     number_of_repeats.append(list.count(list[i]))

This give me

number_of_repeats=[3,1,4,4,2,3,4,3,2,4]

How can I get output like list1 and list2?

asked Jan 13, 2021 at 20:18

5

Here is a way to do it using a set and the function count()

lst1 = ["jim","jennifer","roy","roy","mike","jim","roy","jim","mike","roy"]
lst2 = []
set1 = set(lst1)

for i in set1: 
    lst2.append(lst1.count(i))

lst1 = list(set(lst1))
print(lst1)
print(lst2)

output:

['jim', 'jennifer', 'mike', 'roy']
[3, 1, 2, 4]

answered Jan 13, 2021 at 20:33

0

As mentioned in the comments, don't call a variable list or other python words. By doing this, you end up hiding Python's actual inbuilt list class. Let's assume your initial list is items.

items = ["jim","jennifer","roy","roy","mike","jim","roy","jim","mike","roy"]
list1 = list(dict.fromkeys(items))
list2 = [items.count(i) for i in list1]

Output:

['jim', 'jennifer', 'roy', 'mike']
[3, 1, 4, 2]

answered Jan 13, 2021 at 20:29

Find repeated words in list python

ShradhaShradha

1,9781 gold badge10 silver badges25 bronze badges

Converting a list to a set and back to a list gets rid of the duplicates:

my_list=["jim","jennifer","roy","roy","mike","jim","roy","jim","mike","roy"]
list1 = list(set(my_list))
list2 = [my_list.count(item) for item in list1]

#prints: ['jennifer', 'roy', 'mike', 'jim']
#        [1, 4, 2, 3]

answered Jan 13, 2021 at 20:37

Find repeated words in list python

pakpepakpe

5,2552 gold badges7 silver badges22 bronze badges

def non_repetitive(list):

    list1=[]
    list2=[]

    for i in list:
        if not i in list1:
            list1.append(i)


    for j in list1:
        counter=0
        for k in list:
            if j==k:
                counter+=1
        list2.append(counter)


    return list1, list2



list=["jim","jennifer","roy","roy","mike","jim","roy","jim","mike","roy"]

print(non_repetitive(list))

Wolf

9,3847 gold badges59 silver badges102 bronze badges

answered Jan 13, 2021 at 20:32

1

I think a dictionary does a great job here, not only in the implementation,

# count occurrences of unique strings
def str_frequency(names):
    d = dict()
    for name in names:
      d[name] = d.get(name, 0)+1
    return d

# input: a list of names that may contain duplicates
names = ["jim", "jennifer", "roy", "roy", "mike", "jim", "roy", "jim", "mike", "roy"]

# result: unique names and their counts
sf = str_frequency(names)

print(sf)

... but also for serving the final result:

{'jim': 3, 'jennifer': 1, 'roy': 4, 'mike': 2}

If you really insist in getting it as list1 and list2, learn about keys() and values() methods.

answered Jan 14, 2021 at 13:25

WolfWolf

9,3847 gold badges59 silver badges102 bronze badges

I think you just use collections package for acceleration. If you use normal way, you will get O(n^2) but when using collections it will be O(n)

from collections import Counter

list_string = ["jim","jennifer","roy","roy","mike","jim","roy","jim","mike","roy"]
list_string = dict(Counter(list_string))
list_1 = list(list_string.keys())
list_2 = list(list_string.values())

answered Jan 14, 2021 at 13:42

Find repeated words in list python

How do you find duplicate words in a list Python?

Python.
string = "big black bug bit a big black dog on his big black nose";.
#Converts the string into lowercase..
string = string.lower();.
#Split the string into words using built-in function..
words = string.split(" ");.
print("Duplicate words in a given string : ");.
for i in range(0, len(words)):.
count = 1;.

How do you check if an item is repeated in a list Python?

To check if a list contains any duplicate element follow the following steps,.
Add the contents of list in a set. As set contains only unique elements, so no duplicates will be added to the set..
Compare the size of set and list. If size of list & set is equal then it means no duplicates in list..

How do you find the number of repeated values in a list in Python?

Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

How do I find a repeating element in a string Python?

Python.
string = "Great responsibility";.
print("Duplicate characters in a given string: ");.
#Counts each character present in the string..
for i in range(0, len(string)):.
count = 1;.
for j in range(i+1, len(string)):.
if(string[i] == string[j] and string[i] != ' '):.
count = count + 1;.