python loop的问题,怎么提取一堆list of strings中相同的string
发布网友
发布时间:2022-04-30 16:36
我来回答
共2个回答
热心网友
时间:2023-10-09 00:41
可以先统计列表中每一个元素的数目,
然后用表理解选择出统计数目大于的的元素
myList = ["StarWars","cs116","StarWars"]
count = {}
for item in myList:
count[item] = count.get(item, 0) + 1
result = [k for k,v in count.items() if v>=2]
print result
热心网友
时间:2023-10-09 00:41
# try this
index = 0
for i in list_of_strings:
if i in list_of_strings[:index] + list_of_strings[index+1:]:
print i
index += 1
追问
我只需要一个cs116,怎么改呢?