简体   繁体   English

Python:根据输入从列表中获取项目

[英]Python: Get item from list based on input

I appreciate this may not be directly possible so I would be interested how you would go about solving this problem for a general case. 我很欣赏这可能不是直接可能的,所以我会对您在一般情况下如何解决此问题感兴趣。 I have a list item that looks like this, [(array,time),(array,time)...] the array is a numpy array which can have any n by m dimensions. 我有一个看起来像这样的列表项, [(array,time),(array,time)...]数组是一个numpy数组,可以具有任何n×m的维。 This will look like array[[derivatives dimension1],[derivatives dimension 2] ...] 看起来像array[[derivatives dimension1],[derivatives dimension 2] ...]

From the list I want a function to create two lists which would contain all the values at the position passed to it. 从列表中,我希望一个函数创建两个列表,其中将包含传递给它的位置处的所有值。 These could then be used for plotting. 然后可以将这些用于绘图。 I can think of ways to do this with alternative data structures but unfortunately this is no an option. 我可以考虑使用替代数据结构来实现此目的的方法,但是不幸的是,这是没有选择的。

Essentially what I want is 本质上我想要的是

def f(list, pos1, pos2):
    xs = []
    ys = []
    for i in list:
        ys.append(i pos1)
        xs.append(i pos2)
    return xs, ys

Where i pos1 is equivalent to i[n][m] The real problem being when it's 1 by 1 so i can't just pass integers. i pos1等于i[n][m]真正的问题是当它为1乘1时,我不能只传递整数。 Any advice would be great, sorry the post is a bit long I wanted to be clear. 任何建议都很好,很抱歉,我想弄清楚这篇文章有点长。 Thanks 谢谢

If I'm understanding your question correctly, you essentially want to select indexes from a list of lists, and create new lists from that selection. 如果我正确地理解了您的问题,则本质上您想从列表列表中选择索引,然后从该选择中创建新列表。

Selecting indexes from a list of lists is fairly simple, particularly if you have a fixed number of selections: 从列表列表中选择索引非常简单,特别是如果您有固定数量的选择:

parts = [(item[pos1], item[pos2]) for item in list]

Creating new lists from those selections is also fairly easy, using the built-in zip() function: 使用内置的zip()函数,从这些选择中创建新列表也相当容易:

separated = zip(*parts)

You can further reduce memory usage by using a generator expression instead of a list comprehension in the final function: 您可以通过使用生成器表达式而不是最终函数中的列表理解来进一步减少内存使用:

def f( list, pos1, pos2 ):
    partsgen = ((item[pos1], item[pos2]) for item in list)
    return zip(*partsgen)

Here's how it looks in action: 运作方式如下:

>>> f( [['ignore', 'a', 1], ['ignore', 'b', 2],['ignore', 'c', 3]], 1, 2 )
[('a', 'b', 'c'), (1, 2, 3)]

Update: After re-reading the question and comments, I'm realizing this is a bit over-simplified. 更新:重新阅读问题和评论后,我意识到这有点过分简化了。 However, the general idea should still work when you exchange pos1 and pos2 for appropriate indexing into the contained array. 但是,当您交换pos1和pos2以便对包含的数组进行适当的索引编制时,总体思路仍然应该起作用。

if i understand your question, something like the following should be easy and fast, particularly if you need to do this multiple times: 如果我了解您的问题,则类似以下内容的操作应该轻松快捷,尤其是如果您需要多次执行以下操作:

z = np.dstack([ arr for arr, time in lst ])
x, y = z[pos1], z[pos2]

for example: 例如:

In [42]: a = arange(9).reshape(3,3)
In [43]: z = np.dstack([a, a*2, a*3])
In [44]: z[0,0]
Out[44]: array([0, 0, 0])
In [45]: z[1,1]
Out[45]: array([ 4,  8, 12])
In [46]: z[0,1]
Out[46]: array([1, 2, 3])

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

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