Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python在列表中汇总数据_Python_List_Summarize - Fatal编程技术网

用python在列表中汇总数据

用python在列表中汇总数据,python,list,summarize,Python,List,Summarize,在python中,我需要以这种方式(如直方图)汇总count_列表中的数据: 但我得到的却是错误的输出: """ 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_len

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

但我得到的却是错误的输出:

"""
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()
随机导入
随机列表=[]
列表长度=20
而len(随机列表)<列表长度:
random_list.append(random.randint(0,10))
计数列表=[0]*11
索引=0
索引
试试这个

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])
此方法使用:

从集合导入计数器
随机输入
随机列表=[]
列表长度=20
而len(随机列表)<列表长度:
random_list.append(random.randint(0,10))
c=计数器(随机列表)
打印('编号|出现')
def加温器(dic):
对于dic.items()中的v、d:
打印(v,“|”,“%s”%*”*c[v])
避暑器(dic)

是的,我发现了问题

它来自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
`import random
随机列表=[]
列表长度=20
而len(随机列表)<列表长度:
random_list.append(random.randint(0,10))
计数列表=[0]*11
索引=0
索引

summerizer()`

您得到的输出是什么?请提供尽可能多的信息!这将有助于洛蒂编辑问题,添加我得到的错误答案。。。。附件图片错误是数字前缺少空格?为什么要打印“%s”%'*3
,而不是只打印“
”?我看您的输出没有问题。计数似乎相符。您知道您正在表格前打印
count\u list
,而不是
random\u list
,对吗?meSo没有相同的输出如果它为零,您想计数1吗?没有相同的输出also@AhmedAl-Bahnasy,它应该向您显示您的输出是正确的输出。那有什么问题?
number  |  occurrence
0 |
1 | 
2 | **
3 | **
4 | **
5 | ***
6 | **
7 | 
8 | ****
9 | ***
10 | **
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)
`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