I want to separate out strings(names) that repeat three or more times in a list and store them in a separate list
List:
[['jane','michael','david','Kieran','steven'],['peter','Ivan','michael','michael','Kieran'],['jane','michael','andrea','anthony','david'],
['michael','Kieran','Martinez','david','Courtney']]
Expected Output(New List):
['michael','kieran','david']
try this, Counter
+ chain.from_iterable
from collections import Counter
from itertools import chain
print([k for k,v in Counter(chain.from_iterable(values)).items() if v >= 3])
['michael', 'david', 'Kieran']
you can use a for loop to iterate the list and store the duplicates in a dict and separate based on count
new_list=[]
dicta={}
for i in a:
for j in i:
if j in dicta:
dicta[j]+=1
else:
dicta[j]=1
for i in dicta.keys():
if dicta[i]>=3 :
new_list.append(i)
You first needs to create a new list of names where you could iterate over to generate the new list
source = [['jane','michael','david','Kieran','steven'],['peter','Ivan','michael','michael','Kieran'],['jane','michael','andrea','anthony','david'],['michael','Kieran','Martinez','david','Courtney']]
new_names_list = []
for names in source :
for n in names:
new_names_list.append(n)
print(new_names_list) ## this is the new list of names derived from your source
New you could use Counter to derive the count list
from collections import Counter
counter_name_dict = Counter(new_names_list)
result = []
for k, v in counter_name_dict.items():
if v >= 3:
result.append(k)
The result will now only hold the list of names which are having a count of 3 or more.
This is one solution:
from collections import defaultdict
lists = [['jane','michael','david','Kieran','steven'],['peter','Ivan','michael','michael','Kieran'],['jane','michael','andrea','anthony','david'],
['michael','Kieran','Martinez','david','Courtney']]
d = defaultdict(lambda: 0)
for l in lists:
for e in l:
d[e] += 1
results = [key for key in d if d[key] >= 3]
Here's an example code:
import collections
thelist = [['jane','michael','david','Kieran','steven'],['peter','Ivan','michael','michael','Kieran'],['jane','michael','andrea','anthony','david'],
['michael','Kieran','Martinez','david','Courtney']]
counts = {}
for l in thelist:
print(collections.Counter(l))
for key,value in collections.Counter(l).items():
if key in counts:
counts[key] += value
else:
counts[key] = value
newlist = []
for key,value in counts.items():
if value == 3:
newlist.append(key)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.