在奇数位置提取列表元素 [英] Extract elements of list at odd positions

查看:132
本文介绍了在奇数位置提取列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想创建一个列表,该列表是一些现有列表的子列表.

例如,

L = [1, 2, 3, 4, 5, 6, 7],我想创建一个子列表li,以使li包含L中所有元素的奇数位置.

虽然我可以做到

L = [1, 2, 3, 4, 5, 6, 7]
li = []
count = 0
for i in L:
    if count % 2 == 1:
        li.append(i)
    count += 1

但是我想知道是否还有另一种方法可以以更少的步骤高效地完成相同的工作.

解决方案

解决方案

是的,您可以:

l = L[1::2]

这就是全部.结果将包含放置在以下位置的元素(基于0,因此第一个元素位于位置0,第二个元素位于1等):

1, 3, 5

所以结果(实际数字)将是:

2, 4, 6

说明

最后的[1::2]只是列表切片的一种表示法.通常采用以下形式:

some_list[start:stop:step]

如果我们省略了start,则将使用默认值(0).因此,将选择第一个元素(在位置0,因为索引基于0).在这种情况下,将选择第二个元素.

因为省略了第二个元素,所以使用了默认值(列表的末尾).因此,列表是从第二个元素到最后一个迭代.

我们还提供了第三个参数(step),即2.这意味着将选择一个元素,将跳过下一个元素,依此类推...

因此,总而言之,在这种情况下,[1::2]的意思是:

  1. 采用第二个元素(顺便说一句,如果从索引中判断,它是一个奇数元素),
  2. 跳过一个元素(因为我们有step=2,所以我们跳过了一个元素,这与默认的step=1相反)
  3. 选择下一个元素
  4. 重复步骤2.-3.直到到达列表的末尾,

编辑:@PreetKukreti提供了有关Python列表切片表示法的另一种解释的链接.参见此处:解释Python的切片符号

其他-用enumerate()

替换计数器

在您的代码中,您显式创建并增加了计数器.在Python中,这不是必需的,因为您可以使用 enumerate() :

for count, i in enumerate(L):
    if count % 2 == 1:
        l.append(i)

以上功能与您使用的代码完全相同:

count = 0
for i in L:
    if count % 2 == 1:
        l.append(i)
    count += 1

有关在Python中使用计数器模拟for循环的更多信息:在Python"for"循环中访问索引

So I want to create a list which is a sublist of some existing list.

For example,

L = [1, 2, 3, 4, 5, 6, 7], I want to create a sublist li such that li contains all the elements in L at odd positions.

While I can do it by

L = [1, 2, 3, 4, 5, 6, 7]
li = []
count = 0
for i in L:
    if count % 2 == 1:
        li.append(i)
    count += 1

But I want to know if there is another way to do the same efficiently and in fewer number of steps.

解决方案

Solution

Yes, you can:

l = L[1::2]

And this is all. The result will contain the elements placed on the following positions (0-based, so first element is at position 0, second at 1 etc.):

1, 3, 5

so the result (actual numbers) will be:

2, 4, 6

Explanation

The [1::2] at the end is just a notation for list slicing. Usually it is in the following form:

some_list[start:stop:step]

If we omitted start, the default (0) would be used. So the first element (at position 0, because the indexes are 0-based) would be selected. In this case the second element will be selected.

Because the second element is omitted, the default is being used (the end of the list). So the list is being iterated from the second element to the end.

We also provided third argument (step) which is 2. Which means that one element will be selected, the next will be skipped, and so on...

So, to sum up, in this case [1::2] means:

  1. take the second element (which, by the way, is an odd element, if you judge from the index),
  2. skip one element (because we have step=2, so we are skipping one, as a contrary to step=1 which is default),
  3. take the next element,
  4. Repeat steps 2.-3. until the end of the list is reached,

EDIT: @PreetKukreti gave a link for another explanation on Python's list slicing notation. See here: Explain Python's slice notation

Extras - replacing counter with enumerate()

In your code, you explicitly create and increase the counter. In Python this is not necessary, as you can enumerate through some iterable using enumerate():

for count, i in enumerate(L):
    if count % 2 == 1:
        l.append(i)

The above serves exactly the same purpose as the code you were using:

count = 0
for i in L:
    if count % 2 == 1:
        l.append(i)
    count += 1

More on emulating for loops with counter in Python: Accessing the index in Python 'for' loops

这篇关于在奇数位置提取列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆