0

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?

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 2
    try '''set()''' – Yanirmr Jan 13 '21 at 20:21
  • A [set](https://docs.python.org/3/tutorial/datastructures.html#sets) can help you. – Thomas Sablik Jan 13 '21 at 20:21
  • 4
    1. Don't call a variable `list` or other python words. By doing this, you end up hiding Python's actual inbuilt `list` class. 2. "Get unique elements from list" and "Count unique elements from list" are extensively covered in other questions. Please do [some research before asking](//meta.stackoverflow.com/a/261593/843953) on Stack Overflow. Please take the [tour], read [ask], ask a **specific** question and provide a [mre] that reproduces your problem. Welcome to Stack Overflow! – Pranav Hosangadi Jan 13 '21 at 20:22
  • 1
    Does this answer your question? [How do I count unique values inside a list](https://stackoverflow.com/questions/12282232/how-do-i-count-unique-values-inside-a-list) – Pranav Hosangadi Jan 13 '21 at 20:25
  • Did you notice the syntax error in the actual list? – Wolf Jan 14 '21 at 13:10

6 Answers6

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]
Shradha
  • 2,232
  • 1
  • 14
  • 26
0
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,679
  • 7
  • 62
  • 108
Koren
  • 1
  • I fixed the mentioned display issue in your answer. Do you already know that it's not a good idea to name a variable or parameter [`list`](https://docs.python.org/3/library/functions.html?highlight=int#func-list)? – Wolf Jan 14 '21 at 12:39
0

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]
nsh1998
  • 106
  • 9
0

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]
pakpe
  • 5,391
  • 2
  • 8
  • 23
0

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.

Wolf
  • 9,679
  • 7
  • 62
  • 108
0

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())
Halley
  • 11
  • 3