我想在python中创建一个程序,要求用户输入[]格式的数字列表。
然后必须计算并显示列表中有多少这些数字介于1到10,10和20之间, 20和30等。
这是我到目前为止所拥有的。
my_num = [int(i) for i in raw_input("Enter numbers... ").split(",")]
the_list = eval(my_num)
number = 0
if 1<= my_num and my_num <=10:
number = number + 1
答案 0 :(得分:5)
import itertools
numbers = sorted(map(int, raw_input().split(",")))
for k, g in itertools.groupby(numbers, lambda x: x // 10):
print k, list(g)
这将为您提供数字组,您可以通过调用len()
来计算组中的实例。例如,替换
# print k, list(g)
print k, len(list(g))
重要的是先对数字进行排序。
以此为输入:
1, 5, 6, 89, 43, 43, 25, 76, 12, 32, 23, 25, 27, 13, 5, 7
0 [1, 5, 5, 6, 7]
1 [12, 13]
2 [23, 25, 25, 27]
3 [32]
4 [43, 43]
7 [76]
8 [89]
或者使用len调用的输出:
0 5
1 2
2 4
3 1
4 2
7 1
8 1
然后可以将其格式化为:
print "%d - %d: %d" % (k * 10 + 1, (k + 1) * 10, len(list(g))
产量:
1 - 10: 5
11 - 20: 2
21 - 30: 4
31 - 40: 1
41 - 50: 2
71 - 80: 1
81 - 90: 1
答案 1 :(得分:2)
使用Counter
是有效的,因为您只需要迭代一次列表
>>> L = [1, 5, 6, 89, 43, 43, 25, 76, 12, 32, 23, 25, 27, 13, 5, 7]
>>> from collections import Counter
>>> Counter(x//10 for x in L)
Counter({0: 5, 2: 4, 1: 2, 4: 2, 3: 1, 7: 1, 8: 1})
>>> sorted(Counter(x//10 for x in L).items())
[(0, 5), (1, 2), (2, 4), (3, 1), (4, 2), (7, 1), (8, 1)]
>>> for k,v in sorted(Counter(x//10 for x in L).items()):
... print "%d - %d: %d"%(k*10, k*10+9, v)
...
0 - 9: 5
10 - 19: 2
20 - 29: 4
30 - 39: 1
40 - 49: 2
70 - 79: 1
80 - 89: 1
答案 2 :(得分:1)
使用len()
和列表理解:
>>> lis=[1, 5, 6, 89, 43, 43, 25, 76, 12, 32, 23, 25, 27, 13, 5, 7]
>>> [(i,[x for x in lis if x//10==i]) for i in range(0,(max(lis)//10)+1)]
[(0, [1, 5, 6, 5, 7]), (1, [12, 13]), (2, [25, 23, 25, 27]), (3, [32]), (4, [43, 43]), (5, []), (6, []), (7, [76]), (8, [89])]
长度使用len()
:
>>> [(i,len([x for x in lis if x//10==i])) for i in range(0,(max(lis)//10)+1)]
[(0, 5), (1, 2), (2, 4), (3, 1), (4, 2), (5, 0), (6, 0), (7, 1), (8, 1)]
答案 3 :(得分:0)
如果我理解,你想确定不同范围内的数字数:[1; 10],[11; 20]; [21; 30],...,[91; 100]
# Example of user input
your_list = [2, 10, 93, 1, 20]
# All the ranges' right part (sorry if my english is bad). my_ranges = [10, 20, 30, ..., 100]
my_ranges = []
# Array determining how many of those numbers in the list are in [1; 10], [11; 20], ..., [91; 100]
numbers = []
for i in range(1, 11):
my_ranges.append(i*10)
numbers.append(0)
for i in range(0, len(your_list)):
out = False
j = 0
while not out and j<len(my_ranges):
if your_list[i] <= my_ranges[j]:
out = True
numbers[j]+=1
j+=1
for i in range(0, len(numbers)):
print "Number of numbers in ["+str(my_ranges[i]+1-10)+";"+str(my_ranges[i])+"] = "+str(numbers[i])
答案 4 :(得分:0)
不一定是执行此操作的最简单方法,但ast.literal_eval
可以替代eval
。
至于计算每组10个数字的数量,itertools
模块有itertools.groupby
import ast
import itertools
the_input = raw_input("Enter numbers... ")
# Lazy way to parse '[1,2,3]' into [1,2,3]
the_list = ast.literal_eval(the_input)
# Or, parse it manually (this is probably better in this case):
the_list = [int(x) for x in the_input.strip("[]").split(",")]
# Group by dividing number by 10, the result as an integer (15//10==1)
# This means 0-9 are in "group 0", 10-19 are in "group 1" etc
groups = itertools.groupby(the_list, key = lambda k: k // 10)
# Show stats for each block of ten
for group_count, group_members in groups:
print "There were {count} numbers in {start}-{end}".format(
count = len(list(group_members)),
start = group_count * 10, # 15/10 becomes 1, then we do 1*10 for 10
end = group_count * 10 + 9) # and 1*10+9 to get 19 (displayed as "in 10-19"
示例运行:
$ python script.py
Enter numbers... [1,2, 11,12,13, 22,23, 44,45,46,47]
There were 2 numbers in 0-9
There were 3 numbers in 10-19
There were 2 numbers in 20-29
There were 4 numbers in 40-49
要将其调整为1-10,11-20组,依此类推,只需更改行group =
行:
groups = itertools.groupby(the_list, key = lambda k: k // 10 + 1)
或者,更简单一点:
the_input = raw_input("Enter numbers... ")
# Parse "[1,2,3]" into a list of numbers
the_list = [int(x) for x in the_input.strip("[]").split(",")]
group_counter = {}
for number in the_list:
group_start = (number // 10) * 10
group_end = group_start + 9
group_name = "%s-%s" % (group_start, group_end)
group_counter.setdefault(group_name, 0)
group_counter[group_name] += 1
for name, count in group_counter.items():
# Note that the sorting is wrong
print "There were %s in %s" % (count, name)
如果您使用的是Python 2.7或更高版本,也可以使用collections.Counter
答案 5 :(得分:0)
以适当的标题显示所有值。
listNo = [6,8,10,44,33,21,7,1,0,2]
c = 0
s = 0
avg = 0
sOdd = 0
sEven = 0
cOdd = 0
cEven = 0
for i in listNo :
c += 1
s = s+i
avg = s/c
if i % 2 == 0 :
sEven = sEven + i
cEven = cEven + 1
else :
sOdd = sOdd + i
cOdd = cOdd + 1
print ("total number of numbers in the list : ", c)
print("sum of all numbers : ",s)
print("average of all numbers : ",avg)
print("count odd numbers : ",cOdd)
print("sum of odd numbers : ",sOdd)
print("count even numbers : ",cEven)
print("sum of odd numbers : ",sEven)
print("largest number in the list : " ,max(listNo))
print("smallest number in the list : ",min(listNo))