[英]Python grouping items and looping with a step
如果我有这个清单:
y =[a,b,c,d,e,f]
我想 select a
和b
,然后是e
和f
,它的语法是什么?
我试过了:
y = [a,b,c,d,e,f,g,h,i,j]
for letter in range(0,len(y),2):
print(letter)
我想要的结果是
[a, b, e, f i,j]
但相反,我每隔一秒收到一封信:
a
c
e
g
i
当我尝试使用切片符号print(y[:1])时,我只得到列表中的前两个值。
您需要将步骤设置为 4。对于每 4 个项目,您需要前两个(跳过 3 和 4)。 所以:
lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
new_list = []
for i in range(0, len(lst), 4):
new_list.append(lst[i])
new_list.append(lst[i + 1])
print(new_list)
如果可迭代实际上只是一个字符串/字符集合:
from textwrap import wrap
from itertools import chain
print(list(chain.from_iterable(wrap("abcdefghij", 2)[::2])))
Output:
['a', 'b', 'e', 'f', 'i', 'j']
>>>
如果它是任意可迭代的:
from itertools import islice, chain
def chunk_wise(iterable, chunk_size):
it = iter(iterable)
while chunk := list(islice(it, chunk_size)):
yield chunk
print(list(chain.from_iterable(islice(chunk_wise("abcdefghij", 2), 0, None, 2))))
Output:
['a', 'b', 'e', 'f', 'i', 'j']
>>>
最直接的方法是使用带有 if 子句(“过滤器”)的列表推导,它将选择您想要的跨度中的项目。
ls = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
ls_spans = [x for i, x in enumerate(ls) if i % 4 in [0,1]]
为了更清楚地说明这一点,我们要做的是列表推导:
ls_spans = [x for x in ls] # Essentially just copies the list
但是我们希望 select 仅是“跨度”的某些成员,我们将其识别为 4 个单位长度:(我们列表中的abcd
、 efgh
、 ij
)。
ls_spans = [x for x in ls if x_is_wanted()]
为此,由于我们是否想要x
取决于它的 position,因此我们使用enumerate
将列表中的每个元素与索引配对(下面列表理解中的i
):
ls_spans = [x for i, x in enumerate(ls) if x_is_wanted(i)]
现在,我们考虑 function x_is_wanted
必须对每个元素说“是”或“否”( True
或False
):
def x_is_wanted(i: int) -> bool:
span_length = 4
acceptable_positions = [0, 1] # Remember we are 0-indexed!
return i % span_length in acceptable_positions # We use modulo to get the remainder, which is equivalent to the position in the span.
完整的程序可能如下所示:
def x_is_wanted(i: int) -> bool:
span_length = 4
acceptable_positions = [0, 1] # Remember we are 0-indexed!
return i % span_length in acceptable_positions
ls_spans = [x for i, x in enumerate(ls) if x_is_wanted(i)]
但如果我们不需要重用 function,我们可以更轻松地内联它:
# Play with these params to see what changes!
span_length = 4
acceptable_positions = [0, 1]
ls = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
ls_spans = [x for i, x in enumerate(ls) if i % span_length in acceptable_positions]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.