简体   繁体   English

如何挑选清单的特定部分?

[英]How to pick out specific parts of a list?

So I have this list: 所以我有这个清单:

Comps = [
    [1000, 500, 200, 100, 700, 350, 120, 900, 800],
    [1001, 501, 201, 101, 701, 351, 121, 901, 801]
    ]

How would I get the results [500, 350, 120] and [501, 351, 121] from this list? 我如何从该列表中获得结果[500,350,120]和[501,351,121]?

I know how to get the answer [500, 350, 120] if there was only the first list (which would be [1000, 500, 200, 100, 700, 350, 120, 900, 800] ) in the variable Comps. 我知道如果变量Comps中只有第一个列表(即[1000, 500, 200, 100, 700, 350, 120, 900, 800] 1000、500、200、100、700、350、120、900、800 [1000, 500, 200, 100, 700, 350, 120, 900, 800] ),那么如何获得答案[500, 350, 120] 500、350、120]。 However, I am not sure how to work with a list that contains 2 or more lists (also known as "list of list"). 但是,我不确定如何使用包含2个或更多列表的列表(也称为“列表列表”)。 I keep getting the error: "IndexError: list index out of range". 我不断收到错误:“ IndexError:列表索引超出范围”。

What I've done is this: 我所做的是:

print(list(Comps[i] for i in [1, 5, 6]))

Here is the full error for those who are interested: 对于那些感兴趣的人,这是完整的错误:

    Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    print(list(Comps[i] for i in [1, 5, 6]))
  File "<pyshell#74>", line 1, in <genexpr>
    print(list(Comps[i] for i in [1, 5, 6]))
IndexError: list index out of range

The reason you get a IndexError: list index out of range is that Comp is a list with only 2 elements. 出现IndexError: list index out of rangeComp是只有2个元素的列表。 Element at index 1 is the second list ( [1001, 501, 201, 101, 701, 351, 121, 901, 801] ) but there's no element at index 5 in Comps . 索引1处的元素是第二个列表( [1001, 501, 201, 101, 701, 351, 121, 901, 801] ),但是Comps中索引5处没有元素。

You could use a nested list comprehension: 您可以使用嵌套列表理解:

>>> Comps = [[1000, 500, 200, 100, 700, 350, 120, 900, 800], [1001, 501, 201, 101, 701, 351, 121, 901, 801]]
>>> [[l[i] for i in [1, 5, 6]] for l in Comps]
[[500, 350, 120], [501, 351, 121]]

If you want them in a flat list: 如果您希望将它们放在平面列表中:

>>> [l[i] for l in Comps for i in [1, 5, 6]]
[500, 350, 120, 501, 351, 121]

If you work with matrices and arrays, you might want to take a look at numpy: 如果使用矩阵和数组,则可能需要看一下numpy:

import numpy as np
comps = np.array([[1000, 500, 200, 100, 700, 350, 120, 900, 800], [1001, 501, 201, 101, 701, 351, 121, 901, 801]])
comps[:,[1, 5, 6]]
# array([[500, 350, 120],
#       [501, 351, 121]])

If you only want one of them: 如果您只想要其中之一:

>>> [Comps[0][i] for i in [1, 5, 6]]
[500, 350, 120]
>>> [Comps[1][i] for i in [1, 5, 6]]
[501, 351, 121]

And with NumPy: 和NumPy:

>>> comps[0, [1, 5, 6]]
array([500, 350, 120])
>>> comps[1, [1, 5, 6]]
array([501, 351, 121])

You are almost there, you just need to nest one level deeper: 您快到了,只需要嵌套一层即可:

[[sub[i] for i in [1, 5, 6]] for sub in Comps]

Alternatively, you can use operator.itemgetter : 另外,您可以使用operator.itemgetter

>>> list(map(operator.itemgetter(*[1, 5, 6]), Comps))
[(500, 350, 120), (501, 351, 121)]

Or, more directly, 或者,更直接地,

>>> list(map(operator.itemgetter(1, 5, 6), Comps))
[(500, 350, 120), (501, 351, 121)]

As there is one list like: 由于有一个类似的清单:

1) math=[one, list] you always use mathList = math[theIndexOfItemInList] 1) math=[one, list]您始终使用mathList = math[theIndexOfItemInList]

but as there is two or more you need to change how you count lists, like if there is a list: 但由于存在两个或多个,因此您需要更改对列表的计数方式,例如是否有列表:

2) math=[[one, list], [second, list], [third, list], [etc, list]] 2) math=[[one, list], [second, list], [third, list], [etc, list]]

as you call this list: 当您调用此列表时:

3) mathList = math[whichListYouNeed][theIndexOfItemInList] 3) mathList = math[whichListYouNeed][theIndexOfItemInList]

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

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