在奇数位置提取列表元素

问题:在奇数位置提取列表元素

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

例如,

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

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

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.


回答 0

是的你可以:

l = L[1::2]

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

1, 3, 5

因此结果(实际数字)将为:

2, 4, 6

说明

[1::2]在年底只是为了列表切片的符号。通常采用以下形式:

some_list[start:stop:step]

如果省略start,将使用默认值(0)。所以第一个元素(在position 0,因为索引是0基于-的)。在这种情况下,将选择第二个元素。

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

我们还提供了第三个参数(step2。这意味着将选择一个元素,将跳过下一个元素,依此类推…

因此,总结起来,在这种情况下[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

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

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


回答 1

对于奇数位,您可能需要:

>>>> list_ = list(range(10))
>>>> print list_[1::2]
[1, 3, 5, 7, 9]
>>>>

For the odd positions, you probably want:

>>>> list_ = list(range(10))
>>>> print list_[1::2]
[1, 3, 5, 7, 9]
>>>>

回答 2

我喜欢List理解,因为它们具有Math(Set)语法。那么呢:

L = [1, 2, 3, 4, 5, 6, 7]
odd_numbers = [y for x,y in enumerate(L) if x%2 != 0]
even_numbers = [y for x,y in enumerate(L) if x%2 == 0]

基本上,如果您枚举列表,则将获得索引 x和value y。我在这里所做的就是将值y放入输出列表(偶数或奇数),并使用索引x来找出该点是否为奇数(x%2 != 0)。

I like List comprehensions because of their Math (Set) syntax. So how about this:

L = [1, 2, 3, 4, 5, 6, 7]
odd_numbers = [y for x,y in enumerate(L) if x%2 != 0]
even_numbers = [y for x,y in enumerate(L) if x%2 == 0]

Basically, if you enumerate over a list, you’ll get the index x and the value y. What I’m doing here is putting the value y into the output list (even or odd) and using the index x to find out if that point is odd (x%2 != 0).


回答 3

您可以使用按位AND运算符&。让我们看下面:

x = [1, 2, 3, 4, 5, 6, 7]
y = [i for i in x if i&1]
>>> 
[1, 3, 5, 7]

按位AND运算符与1一起使用,其工作原理是因为,用二进制编写时,奇数必须以其第一位为1。

23 = 1 * (2**4) + 0 * (2**3) + 1 * (2**2) + 1 * (2**1) + 1 * (2**0) = 10111
14 = 1 * (2**3) + 1 * (2**2) + 1 * (2**1) + 0 * (2**0) = 1110

如果值是奇数,则与1的AND运算只会返回1(二进制数1也将有最后一位数字)。

查看Python Bitwise Operator页面了解更多信息。

PS:如果要在数据框中选择奇数和偶数列,则可以在战术上使用此方法。假设面部关键点的x和y坐标以x1,y1,x2等列给出。要使用每个图像的宽度和高度值对x和y坐标进行归一化,您可以简单地执行

for i in range(df.shape[1]):
    if i&1:
        df.iloc[:, i] /= heights
    else:
        df.iloc[:, i] /= widths

这与问题不完全相关,但对于数据科学家和计算机视觉工程师而言,此方法可能有用。

干杯!

You can make use of bitwise AND operator &. Let’s see below:

x = [1, 2, 3, 4, 5, 6, 7]
y = [i for i in x if i&1]
>>> 
[1, 3, 5, 7]

Bitwise AND operator is used with 1, and the reason it works because, odd number when written in binary must have its first digit as 1. Let’s check

23 = 1 * (2**4) + 0 * (2**3) + 1 * (2**2) + 1 * (2**1) + 1 * (2**0) = 10111
14 = 1 * (2**3) + 1 * (2**2) + 1 * (2**1) + 0 * (2**0) = 1110

AND operation with 1 will only return 1 (1 in binary will also have last digit 1), iff the value is odd.

Check the Python Bitwise Operator page for more.

P.S: You can tactically use this method if you want to select odd and even columns in a dataframe. Let’s say x and y coordinates of facial key-points are given as columns x1, y1, x2, etc… To normalize the x and y coordinates with width and height values of each image you can simply perform

for i in range(df.shape[1]):
    if i&1:
        df.iloc[:, i] /= heights
    else:
        df.iloc[:, i] /= widths

This is not exactly related to the question but for data scientists and computer vision engineers this method could be useful.

Cheers!


回答 4

list_ = list(range(9))print(list_ [1 :: 2])

list_ = list(range(9)) print(list_[1::2])