基于区间乘数的切片
Slicing based on interval multiplier
我有以下值:
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1021, 1079,
1131, 1183]
我有一个数字200,基于这个,我要对a的值进行切片。
就像 [200, 400, 600, ...]
— 本质上是 200 的倍数。
我想根据 200 的倍数对 a
的值进行切片。
最终结果是以下列表:
[33, 121]
[230, 273, 303, 398]
[445, 502]
[613, 694, 721]
[871, 935]
[1045, 1021, 1079, 1131, 1183]
一个可能的解决方案是
cut = 200
intervals = [[k*cut, (k+1)*cut] for k in range(max(a)//cut + 1)]
[[itm for itm in a if itm > interval[0] and itm < interval[1]] for interval in intervals]
我的实现。
from math import ceil
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1079, 1021, 1131, 1183]
num = 200
max_num = max(a)
divlist = [i*num for i in range(1,ceil(max_num/num)+1)]
dic = {}
for i in divlist:
dic[i] = [x for x in a if (i-num)< x < i]
for i in divlist:
print(dic[i])
这是创建范围 0-200、200-400 ... 的解决方案
他们确定列表 a
中的数字是否在给定范围内
split_points = [i for i in range(0, max(a), 200)]
out: [0, 200, 400, 600, 800, 1000]
parts = [[number for number in a if number in range(split_point, split_point+200)] for split_point in split_points]
out:
[[33, 121],
[230, 273, 303, 398],
[445, 502],
[613, 694, 721],
[871, 935],
[1045, 1079, 1021, 1131, 1183]]
您可以使用 itertools.groupby() 将列表中的项目分组,键为 item//200
from itertools import groupby
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1079, 1021, 1131, 1183]
intervals = [list(g) for _, g in groupby(a, lambda x: x//200)]
输出-
[[33, 121], [230, 273, 303, 398], [445, 502], [613, 694, 721], [871, 935], [1045, 1079, 1021, 1131, 1183]]
虽然已经有人回答了,但这是我的解决方案
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1079, 1021, 1131, 1183]
interval = 200
rang = range(0, max(a) + 1, interval)
b = enumerate(rang, start=1)
c = [list(filter(lambda n: (n < interval * i and n >= x), a)) for i,x in b]
输出:
[[33, 121], [230, 273, 303, 398], [445, 502], [613, 694, 721], [871, 935], [1045, 1079, 1021, 1131, 1183]]
过滤列表理解似乎效率很低。您可以直接计算每个值应附加到的列表:
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1021, 1079, 1131, 1183]
step = 200
result = [[] for _ in range(max(a) // step + 1)]
for x in a:
result[x // step].append(x)
print(result)
输出;
[[33, 121], [230, 273, 303, 398], [445, 502], [613, 694, 721], [871, 935], [1045, 1021, 1079, 1131, 1183]]
我有以下值:
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1021, 1079,
1131, 1183]
我有一个数字200,基于这个,我要对a的值进行切片。
就像 [200, 400, 600, ...]
— 本质上是 200 的倍数。
我想根据 200 的倍数对 a
的值进行切片。
最终结果是以下列表:
[33, 121]
[230, 273, 303, 398]
[445, 502]
[613, 694, 721]
[871, 935]
[1045, 1021, 1079, 1131, 1183]
一个可能的解决方案是
cut = 200
intervals = [[k*cut, (k+1)*cut] for k in range(max(a)//cut + 1)]
[[itm for itm in a if itm > interval[0] and itm < interval[1]] for interval in intervals]
我的实现。
from math import ceil
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1079, 1021, 1131, 1183]
num = 200
max_num = max(a)
divlist = [i*num for i in range(1,ceil(max_num/num)+1)]
dic = {}
for i in divlist:
dic[i] = [x for x in a if (i-num)< x < i]
for i in divlist:
print(dic[i])
这是创建范围 0-200、200-400 ... 的解决方案
他们确定列表 a
中的数字是否在给定范围内
split_points = [i for i in range(0, max(a), 200)]
out: [0, 200, 400, 600, 800, 1000]
parts = [[number for number in a if number in range(split_point, split_point+200)] for split_point in split_points]
out:
[[33, 121],
[230, 273, 303, 398],
[445, 502],
[613, 694, 721],
[871, 935],
[1045, 1079, 1021, 1131, 1183]]
您可以使用 itertools.groupby() 将列表中的项目分组,键为 item//200
from itertools import groupby
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1079, 1021, 1131, 1183]
intervals = [list(g) for _, g in groupby(a, lambda x: x//200)]
输出-
[[33, 121], [230, 273, 303, 398], [445, 502], [613, 694, 721], [871, 935], [1045, 1079, 1021, 1131, 1183]]
虽然已经有人回答了,但这是我的解决方案
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1079, 1021, 1131, 1183]
interval = 200
rang = range(0, max(a) + 1, interval)
b = enumerate(rang, start=1)
c = [list(filter(lambda n: (n < interval * i and n >= x), a)) for i,x in b]
输出:
[[33, 121], [230, 273, 303, 398], [445, 502], [613, 694, 721], [871, 935], [1045, 1079, 1021, 1131, 1183]]
过滤列表理解似乎效率很低。您可以直接计算每个值应附加到的列表:
a = [33, 121, 230, 273, 303, 398, 445, 502, 613, 694, 721, 871, 935, 1045, 1021, 1079, 1131, 1183]
step = 200
result = [[] for _ in range(max(a) // step + 1)]
for x in a:
result[x // step].append(x)
print(result)
输出;
[[33, 121], [230, 273, 303, 398], [445, 502], [613, 694, 721], [871, 935], [1045, 1021, 1079, 1131, 1183]]