【问题标题】:List Manipulation of Duplicates列出重复项的操作
【发布时间】:2019-02-23 05:39:30
【问题描述】:

所以我在一个列表中有我想要操作的元素,基本上我希望这发生:

input:
my_list = ['Gold Trophy (January)', 'Gold Trophy (February)', 'Bronze Trophy 
(March)']

output:
['Gold Trophy x2', 'Bronze Trophy (March)']

当有重复的公共字符串时(例如在 Gold Trophy 的情况下),我希望删除这两个元素,并形成一个新元素,上面写着 (Gold Trophy x(amount of duplicates))

这是我目前所拥有的:

my_list = ['Gold Trophy (January)', 'Gold Trophy (February)', 'Bronze Trophy 
(March)']

# function to count how many duplicates
def countX(my_list, myString): 
    count = 0
    for ele in my_list: 
        if (myString in ele): 
            count = count + 1
    return count 

myString = 'Gold Trophy'
real_count = (countX(my_list, myString))


print(*my_list, sep=', ')
print('duplicates = '+str(countX(my_list, myString)))

此时,此代码运行并返回指定字符串的重复项在列表中的数量。关于从哪里获得所需输出的任何想法?谢谢!

【问题讨论】:

    标签: python string list function count


    【解决方案1】:

    这应该可以在不使用正则表达式的情况下工作。为了清楚地说明正在发生的事情,我已经把 cmets 放在了上面。

    from collections import Counter
    my_list = ['Gold Trophy (January)', 'Gold Trophy (February)', 'Bronze Trophy (March)']
    output_ls = []
    trophy_ls = []
    month_ls = []
    trophy_cnt_dc = {}
    for item in my_list:
        trophy_ls.append(item.split(' (')[0])
        month_ls.append(item.split(' (')[1])
    # print(trophy_ls) >> ['Gold Trophy', 'Gold Trophy', 'Bronze Trophy']
    # print(month_ls) >> ['January)', 'February)', 'March)']
    trophy_cnt_dc = dict(Counter(trophy_ls))
    #print(trophy_cnt_dc) >> {'Gold Trophy': 2, 'Bronze Trophy': 1}
    for k,v in trophy_cnt_dc.items():
        if v > 1:
            output_ls.append(k+' x'+str(v))
        else:
            ind = trophy_ls.index(k)
            output_ls.append(k+' ('+month_ls[ind])
    print(output_ls)
    

    输出:

    ['Gold Trophy x2', 'Bronze Trophy (March)']
    

    【讨论】:

      【解决方案2】:

      这是一个解决方案(请参阅 cmets 进行澄清)。请注意,我使用了一个小技巧来拆分名称和日期:我在 ( 上拆分,然后在需要时恢复它。可以做得更干净,但不清楚是否需要。

      my_list = ['Gold Trophy (January)', 'Gold Trophy (February)', 'Bronze Trophy (March)']
      
      # Create map of tuples: (name, date)
      pairs = [tuple(x.split('(')) for x in my_list]
      
      # count the number of each name
      counts = dict()
      for (name, day) in pairs:
          counts[name] = counts.get(name, 0) + 1
      
      # create a dictionary from initial list
      # it doesn't matter how collisions are resolved
      # the dictionary is required to process each name only once
      init = dict(pairs)
      res = []
      
      # for each name:
      #   if count is > 1, print the count
      #   if count is 1, then print its date
      for (name, date) in init.items():
          if counts[name] > 1:
              res.append(name + 'x' + str(counts[name]))
          else:
              res.append(name + '(' + date)
      print(res)
      

      【讨论】:

        猜你喜欢
        • 2019-01-28
        • 1970-01-01
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-11
        • 1970-01-01
        • 2017-12-21
        相关资源
        最近更新 更多