简体   繁体   English

Python:制作切片列表

[英]Python: making list of slices

How can I make this code better:我怎样才能使这段代码更好:

slices = [slice(0,10), slice(10,20), slice(20,30), slice(30,40), slice(40,50),
         slice(50,60), slice(60, 70), slice(70, 80), slice(80,90), slice(90,100)]

for s in slices:
   some_function(s)

Number is up to 800s and I do need to change slice range too.数字高达 800 秒,我也确实需要更改切片范围。

Edit:编辑:

with concurrent.futures.ThreadpoolExecutor(max_workers=10) as exec:
   slices = [slice(0,10), slice(10,20), slice(20,30),......]

   for s in slices:
      r = exec.map(some_function, some_list[s]))

You can try this:你可以试试这个:

start, end, slice_range = 0, 800, 10
for i in range(start, end, slice_range):
   some_function(slice(i, i+slice_range))

The list can be completely eliminated, and you can also modify the range, start & stop point with just a single change!列表可以完全消除,还可以一键修改范围、起止点! You can read more about range function here .您可以在此处阅读有关范围 function 的更多信息。

try to use List Comprehensions尝试使用列表理解

start, end, step = 0, 100, 10
slices = [slice(i, i+step) for i in range(start, end, step)]
print(slices)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM