[英]Python grouping items and looping with a step
If I had this list:如果我有这个清单:
y =[a,b,c,d,e,f]
And I want to select a
and b
, then e
and f
, what's the syntax for that?我想 select
a
和b
,然后是e
和f
,它的语法是什么?
I've tried:我试过了:
y = [a,b,c,d,e,f,g,h,i,j]
for letter in range(0,len(y),2):
print(letter)
The result I want is我想要的结果是
[a, b, e, f i,j]
But instead I get every second letter:但相反,我每隔一秒收到一封信:
a
c
e
g
i
When I've tried using slice notation print(y[:1]) I only get the first two values in the list.当我尝试使用切片符号print(y[:1])时,我只得到列表中的前两个值。
You need to set the step to 4. For every 4 items, you need the first two(3 and 4 are skipped).您需要将步骤设置为 4。对于每 4 个项目,您需要前两个(跳过 3 和 4)。 so:
所以:
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)
If the iterable is really just a string/collection of characters:如果可迭代实际上只是一个字符串/字符集合:
from textwrap import wrap
from itertools import chain
print(list(chain.from_iterable(wrap("abcdefghij", 2)[::2])))
Output: Output:
['a', 'b', 'e', 'f', 'i', 'j']
>>>
If it's an arbitrary iterable:如果它是任意可迭代的:
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: Output:
['a', 'b', 'e', 'f', 'i', 'j']
>>>
The most straightforward way is to use a list comprehension with an if-clause ('filter') that will pick the items in your span that you want.最直接的方法是使用带有 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]]
To tease this out more, what we do is a list comprehension:为了更清楚地说明这一点,我们要做的是列表推导:
ls_spans = [x for x in ls] # Essentially just copies the list
But we want to select only certain members of a 'span', which we identify as a 4-unit length: ( abcd
, efgh
, ij
in our list).但是我们希望 select 仅是“跨度”的某些成员,我们将其识别为 4 个单位长度:(我们列表中的
abcd
、 efgh
、 ij
)。
ls_spans = [x for x in ls if x_is_wanted()]
To do this, since the fact of whether we want x
or not is dependent on it's position, we use enumerate
to pair each element in the list with an index (the i
in the list comprehension below):为此,由于我们是否想要
x
取决于它的 position,因此我们使用enumerate
将列表中的每个元素与索引配对(下面列表理解中的i
):
ls_spans = [x for i, x in enumerate(ls) if x_is_wanted(i)]
Now, we consider the function x_is_wanted
which has to say 'yes' or 'no' ( True
or False
) for each element:现在,我们考虑 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.
The complete program would probably look like:完整的程序可能如下所示:
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)]
But we can more easily inline this, if we don't need to re-use the function:但如果我们不需要重用 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.