给定一个集合或一个列表(假设它是有序的)
myset = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
我想找出一个范围内出现了多少个数字。假设我的范围是 10。然后给出上面的列表,我有两组 10。
我希望函数返回[10,10]
如果我的范围是 15。那么我应该得到[15,5]
范围会改变。这是我想出的
myRange = 10
start = 1
current = start
next = current + myRange
count = 0
setTotal = []
for i in myset:
if i >= current and i < next :
count = count + 1
print str(i)+" in "+str(len(setTotal)+1)
else:
current = current + myRange
next = myRange + current
if next >= myset[-1]:
next = myset[-1]
setTotal.append(count)
count = 0
print setTotal
输出
1 in 1
2 in 1
3 in 1
4 in 1
5 in 1
6 in 1
7 in 1
8 in 1
9 in 1
10 in 1
12 in 2
13 in 2
14 in 2
15 in 2
16 in 2
17 in 2
18 in 2
19 in 2
[10, 8]
注意跳过的 11 和 20。我也玩弄了这个条件并得到了有线结果。
编辑:范围定义了一个范围,该范围中的每个值都应计入一个卡盘。
将范围视为从当前值到当前值+范围作为一个块。
编辑:
想要的输出:
1 in 1
2 in 1
3 in 1
4 in 1
5 in 1
6 in 1
7 in 1
8 in 1
9 in 1
10 in 1
11 in 2
12 in 2
13 in 2
14 in 2
15 in 2
16 in 2
17 in 2
18 in 2
19 in 2
[10, 10]