【问题标题】:Summarize data in a list in python在python中汇总列表中的数据
【发布时间】:2018-07-12 15:11:26
【问题描述】:

在 python 中,我需要以这种方式汇总 count_list 中的数据(如直方图):

"""
number | occurence
  0 | *
  1 | **
  2 | ***
  3 | **
  4 | **
  5 | *
  6 | *
  7 | **
  8 | ***
  9 | *
  10 | **
"""

但是我得到了这个错误的输出:

"""
number | occurence
  0 | 
  1 | **
  2 |
  3 |
  4 |
  5 |
  6 | **
  7 |
  8 |
  9 |
  10 | **
"""

这是我的代码:

import random
random_list = []
list_length = 20
while len(random_list) < list_length:
    random_list.append(random.randint(0,10))
count_list = [0] * 11
index = 0

while index < len(random_list):
   number = random_list[index]
    count_list[number] = count_list[number] + 1
    index = index + 1


def summerizer():
    index = 0
    print count_list
    print '"'*3
    print 'number  |  occurrence'
    while index < len(count_list):
      print '%s' %' '*(7),
      print index,#the problem is here
      print ' | ',#and here
      print '%s' %'*'*(count_list[index])
      index += 1
    print '%s'%'"'*3


summerizer()

【问题讨论】:

  • 你得到的输出是什么?请尽可能地提供更多的资料!很有帮助
  • 我已经编辑了问题,添加了我得到的错误答案....附图
  • 错误是数字前缺少空格?
  • 你为什么选择print '%s'%'"'*3 而不仅仅是print '"""'
  • 我认为您的输出没有问题。计数似乎匹配。你知道你在桌子前打印count_list,而不是random_list,对吧?

标签: python list summarize


【解决方案1】:

此方法使用collections.Counter:

from collections import Counter
import random

random_list = []
list_length = 20

while len(random_list) < list_length:
    random_list.append(random.randint(0,10))

c = Counter(random_list)

print('number  |  occurrence')
def summerizer(dic):
    for v,d in dic.items():
        print(v, '|', '%s'%'*'*c[v])

summerizer(dic)

【讨论】:

  • 也没有相同的输出
  • @AhmedAl-Bahnassy 这应该告诉你你的输出是正确的输出。那么有什么问题呢?
【解决方案2】:

是的,我找到了问题

它来自 ide 本身! 这是关于 UDACITY android 应用程序的课程中的一个测验,其中的嵌入式编译器给出了错误的答案..

我现在从 Android 上的 pydroid 应用程序中尝试的相同代码也得到了我需要的答案,无需任何更改

感谢大家的帮助

`import random
 random_list = []
 list_length = 20
 while len(random_list) < list_length:
  random_list.append(random.randint(0,10))
 count_list = [0] * 11
 index = 0

 while index < len(random_list):
  number = random_list[index]
  count_list[number] = count_list[number] + 1
  index = index + 1

def summerizer():
 index = 0
 print count_list
 print '"'*3
 print 'number  |  occurrence'
 while index < len(count_list):
  print '%s' %' '*(7),
  print index,
  print ' | ',
  print '%s' %'*'*(count_list[index])
  index += 1
 print '%s'%'"'*3

summerizer()`

【讨论】:

    【解决方案3】:

    试试这个

    import random
    random_list = []
    list_length = 20
    while len(random_list) < list_length:
        random_list.append(random.randint(0,10))
    dic={}
    for i in random_list:
        dic[i]=dic.get(i,0)+1
    print 'number  |  occurrence'
    for i in range(0,11):
        if(i not in dic):    
            print i,"|",'%s' %'*'*(0)
        else:
            print i,"|",'%s' %'*'*(dic[i])
    

    输出

    [9, 8, 4, 2, 5, 4, 8, 3, 5, 6, 9, 5, 3, 8, 6, 2, 10, 10, 8, 9]

    number  |  occurrence
    0 |
    1 | 
    2 | **
    3 | **
    4 | **
    5 | ***
    6 | **
    7 | 
    8 | ****
    9 | ***
    10 | **
    

    【讨论】:

    • 与我没有相同的输出
    • 所以如果它是零你也想数1?
    猜你喜欢
    • 2021-10-13
    • 2021-02-05
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多