【问题标题】:How to remove duplicate prints in Python?如何在 Python 中删除重复打印?
【发布时间】:2021-03-14 05:42:20
【问题描述】:

所以我想编写一个程序来打印出一个数字在列表中重复了多少次。我对 Python 很陌生,我使用 .count() 命令来获取元素重复的次数。我希望输出遵循“数字 n 重复 n 次”的格式。

这是我目前所拥有的:

x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
index = 0 
while index < len(x):
print('The number {} is repeated {} times.'.format(x[index],x.count(x[index])))
index = index + 1 

这是输出:

The number 1 is repeated 2 times
The number 1 is repeated 2 times
The number 2 is repeated 3 times
The number 4 is repeated 1 times
The number 5 is repeated 1 times
The number 7 is repeated 2 times
The number 2 is repeated 3 times
The number 3 is repeated 3 times
The number 3 is repeated 3 times
The number 8 is repeated 1 times
The number 2 is repeated 3 times
The number 3 is repeated 3 times
The number 6 is repeated 1 times
The number 7 is repeated 2 times

我希望输出显示一个数字仅重复一次的次数,并且按升序排列。我怎样才能使输出像这样:

The number 1 is repeated 2 times.
The number 2 is repeated 3 times. 
The number 3 is repeated 3 times. 
...

谢谢! :)

【问题讨论】:

    标签: python count duplicates


    【解决方案1】:

    试试这个:

    x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
    index = 0 
    while index < len(x):
        if index==x.index(x[index]):
            print('The number {} is repeated {} times.'.format(x[index],x.count(x[index])))
        index = index + 1 
    

    【讨论】:

      【解决方案2】:

      这会计算所有出现的数字并按升序打印。

      from collections import Counter
      
      x = [1, 1, 2, 4, 5, 7, 2, 3, 3, 8, 2, 3, 6, 7]
      
      for key, val in sorted(Counter(x).items()):
          print(f'The number {key} is repeated {val} times.')
      

      代码输出:

      The number 1 is repeated 2 times.
      The number 2 is repeated 3 times.
      The number 3 is repeated 3 times.
      The number 4 is repeated 1 times.
      The number 5 is repeated 1 times.
      The number 6 is repeated 1 times.
      The number 7 is repeated 2 times.
      The number 8 is repeated 1 times.
      

      【讨论】:

        【解决方案3】:

        使用Counter 进行计数和“去重”,然后迭代其结果:

        from collections import Counter
        
        x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
        
        for n, c in Counter(x).items():
            print('The number {} is repeated {} times.'.format(n, c))
        

        对于按最常见到最常见排序的打印:

        for n, c in reversed(Counter(x).most_common()):
            ...
        

        对于按值排序的打印:

        for n, c in sorted(Counter(x).items()):
            ...
        

        【讨论】:

          【解决方案4】:

          你可以试试这个(尽管这些答案似乎都一样有效):

          x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
          x_sorted = sorted(set(x)) # remove duplicates with set, convert to list, and sort
          for num in x_sorted:
              print('The number {} is repeated {} times.'.format(num,x.count(num)))
          

          这可能不是最漂亮的解决方案(我不是 sorted(set(x)) 行的粉丝),但它会按升序打印结果。

          这个输出:

          The number 1 is repeated 2 times.
          The number 2 is repeated 3 times.
          The number 3 is repeated 3 times.
          The number 4 is repeated 1 times.
          The number 5 is repeated 1 times.
          The number 6 is repeated 1 times.
          The number 7 is repeated 2 times.
          The number 8 is repeated 1 times.
          

          【讨论】:

            【解决方案5】:

            如果您想模仿Counter 的功能,您可以填写自己的dict。我特意不使用try和except,或者setdefault,或者defaultdict,来说明基本逻辑很简单,只是一个循环和一个if-else块:

            def count_elements(lst):
                ret = {}
                for elem in lst:
                    if elem in ret:
                        ret[elem] += 1
                    else:
                        ret[elem] = 1
                return ret
            
            def print_counts(counts):
                for n, c in sorted(counts.items()):
                    print(f'The number {n} is repeated {c} times')
            
            x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
            c = count_elements(x)
            print_counts(c)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-12-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-03-09
              相关资源
              最近更新 更多