计算列表中的重复项并将总和分配到列表中

时间:2016-10-11 22:40:20

标签: python

我有一个包含重复字符串的列表:

lst = ["abc", "abc", "omg", "what", "abc", "omg"]

我想制作:

lst = ["3 abc", "2 omg", "what"]

所以基本上计算重复项,删除重复项并将总和添加到字符串的开头。

这就是我现在的做法:

from collections import Counter
list2=[]
for i in lst:
  y = dict(Counter(i))
  have = list(accumulate(y.items())) # creating [("omg", 3), ...]

  for tpl in have: #
    join_list = []
    if tpl[1] > 1:
      join_list.append(str(tpl[1])+" "+tpl[0])
    else:
      join_list.append(tpl[0])
  list2.append(', '.join(join_list))

有没有更简单的方法在python中获得所需的结果?

5 个答案:

答案 0 :(得分:7)

看来你不必要地使事情复杂化。这是一个非常Pythonic的方法:

>>> import collections
>>> class OrderedCounter(collections.Counter, collections.OrderedDict):
...   pass
... 
>>> lst = ["abc", "abc", "omg", "what", "abc", "omg"]
>>> counts = OrderedCounter(lst)
>>> counts
OrderedCounter({'abc': 3, 'omg': 2, 'what': 1})
>>> ["{} {}".format(v,k) if v > 1 else k for k,v in counts.items()]
['3 abc', '2 omg', 'what']
>>> 

答案 1 :(得分:1)

您已正确使用计数器类型来累积所需的值。现在,这只是生成结果的Pythonic方式的问题。最重要的是,将初始化拉出循环,否则你将丢失除最后一个条目之外的所有条目。

list2 = []
for tpl in have:
    count = "" if tpl[1] == 0 else str(tpl[1])+" "
    list2.append(count + tpl[0])

现在,把所有这些都投入到列表理解中:

list2 = [ ("" if tpl[1] == 0 else str(tpl[1])+" ") + tpl[0] \
          for tpl in have]

答案 2 :(得分:1)

试试这个:

lst = ["abc", "abc", "omg", "what", "abc", "omg"]
l = [lst.count(i) for i in lst] # Count number of duplicates
d = dict(zip(lst, l)) # Convert to dictionary
lst = [str(d[i])+' '+i if d[i]>1 else i for i  in d] # Convert to list of strings

答案 3 :(得分:1)

另一个可能提供帮助的解决方案...

import operator

#list
lst = ["abc", "abc", "omg", "what", "abc", "omg"]

#dictionary
countDic = {}

#iterate lst to populate dictionary: {'what': 1, 'abc': 3, 'omg': 2}
for i in lst:
    if i in countDic:
        countDic[i] += 1
    else:
        countDic[i] = 1

#clean list
lst = []

#convert dictionary to an inverse list sorted by value: [('abc', 3), ('omg', 2), ('what', 1)]
sortedLst = sorted(countDic.items(), key=operator.itemgetter(0))

#iterate sorted list to populate list
for k in sortedLst:
    if k[1] != 1:
        lst.append(str(k[1]) + " " + k[0])
    else:
        lst.append(k[0])

#result
print lst

输出:

['3 abc', '2 omg', 'what']

答案 4 :(得分:0)

这是唯一的Pythonic方式,它也很快。

import collections

lst = ["abc", "abc", "omg", "what", "abc", "omg"]
duplicates = collections.Counter(lst)

lst = [f"{value} {key}"
       if value > 1 else key
       for (key, value) in duplicates.items()]

注意:由于列表推导中的f-string语法,此代码仅适用于Python 3.6+。