Identify and count repeated words in the list

Question:

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 By: simonkerike

||

Answers:

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 By: Shradha
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))
Answered By: Koren

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 By: nsh1998

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 By: pakpe

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 By: Wolf

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 By: Halley

Help me with the code
Sample Input : All of us communicate with the help of langauge
Sample Output: 9 # Length of the words in the list
of # Most Repeated word in the list
11 # Number of characters in the longest word

While excuting the below code ,i am getting the sample output like this : 9 all 11

s=input().split(" ")

print(len(s))

new=[]

for i in s:

if i not in new:

    repeat=[i]

    repeat.append(new)

print(i.lower())

break

listing=[]

for i in s:

word=len(i)

listing.append(word)

print(max(listing))

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.