简体   繁体   English

如何将多个参数传递给python函数

[英]How to pass multiple parameters to python function

I have a numpy array with 300 entries. 我有一个包含300个条目的numpy数组。 Within each entry is another numpy array [2048 x 2048]. 每个条目内都有另一个numpy数组[2048 x 2048]。

Each entry is a "tiff" entry (in matrix form) which corresponds to the pixel position in a detector. 每个条目都是一个“ tiff”条目(矩阵形式),它对应于检测器中的像素位置。 Now, what I want to do is centralize this so that I have an [2048 x 2048] array with each entry having 300 entries corresponding to the pixels from the 300 frames. 现在,我要集中处理此问题,以便有一个[2048 x 2048]数组,每个条目都有300个条目,分别对应于300帧中的像素。

I think I have found a way using the zip function. 我想我已经找到一种使用zip功能的方法。 But, each time I get back either a [300 x 2048 x 2048] or [2048 x 300 x 2048]. 但是,每次我返回[300 x 2048 x 2048]或[2048 x 300 x 2048]时。

I want a [2048 x 2048 x 300]. 我想要[2048 x 2048 x 300]。 I'm trying to do this in a rather economical and pythonic way beyond simply reloading into a new array and reindexing. 我正在尝试以一种非常经济和Python的方式做到这一点,而不仅仅是将其重新加载到新数组中并重新建立索引。

T_prime = zip(([list(t) for t in zip(*Tiffs)])) 

Where Tiffs is the array as described above. 其中, Tiffs是如上所述的数组。

In numpy we often add dimmensions to an array instead of using nested arrays (which is the norm with lists for examples). 在numpy中,我们通常向数组添加维度,而不是使用嵌套数组(这是带有示例列表的规范)。 Once you have all your data in a single array, it's easy to operate on it. 将所有数据保存在单个阵列中后,就可以轻松对其进行操作。 In your case it looks like you're looking to transpose the array. 在您的情况下,您似乎要转置数组。 An example: 一个例子:

import numpy as np
example_data = np.empty(30, dtype=object)
for i in range(30):
    example_data[i] = np.zeros((100, 101))

structured = np.array(list(example_data))

print structured.shape
# (30, 100, 101)
print structured.transpose([1, 2, 0]).shape
# (100, 101, 30)

you can use this way 你可以用这种方式

result =  map(lambda x: zip(*x) ,zip(*Tiffs))

and here is full example 这是完整的例子

list1 = [[1,2,3,4],[1,2,3,4],[1,2,3,4]]
list2 =  [[5,6,7,8],[5,6,7,8],[5,6,7,8]]
listoflists = [list1,list2]

result =  map(lambda x: zip(*x) ,zip(*listoflists))
print result

which will result in 这将导致

[[(1, 5), (2, 6), (3, 7), (4, 8)], [(1, 5), (2, 6), (3, 7), (4, 8)], [(1, 5), (2, 6), (3, 7), (4, 8)]]

Would you describe this as an array of 3 items, where each is a 2x4 array? 您能否将其描述为3个元素组成的数组,每个元素均为2x4数组?

In [300]: Tiffs=np.arange(24).reshape(3,2,4)

In [301]: Tiffs
Out[301]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23]]])

In [302]: Tiffs.shape
Out[302]: (3, 2, 4)

If so how about doing a selective transpose? 如果是这样,如何进行选择性转置?

In [304]: Tiffs.transpose(1,2,0)
Out[304]: 
array([[[ 0,  8, 16],
        [ 1,  9, 17],
        [ 2, 10, 18],
        [ 3, 11, 19]],

       [[ 4, 12, 20],
        [ 5, 13, 21],
        [ 6, 14, 22],
        [ 7, 15, 23]]])

In [305]: _.shape
Out[305]: (2, 4, 3)

It's still a 3d array, but could be viewed a (2x4) with 3 items each. 它仍然是3D数组,但可以被视为(2x4),每个都有3个项目。

Another possibility is that it is really an array of objects, where each object is a 2d array, but I think you'd have had to put some extra effort into constructing it. 另一种可能性是它实际上是一个对象数组,其中每个对象都是一个2d数组,但是我认为您在构建它时必须付出一些额外的努力。

In [319]: Tiffs
Out[319]: 
array([array([[0, 1, 2, 3],
       [4, 5, 6, 7]]),
       array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]]),
       array([[16, 17, 18, 19],
       [20, 21, 22, 23]])], dtype=object)

Transposing this is trickier because it really is (3,) array of (2,4) arrays, and axis swapping doesn't cross that object boundary. 进行此处理比较麻烦,因为它实际上是(2,4)数组的(3,)数组,并且轴交换不会越过该object边界。 Something with zip is probably required. 可能需要带zip东西。 zip can be used to transpose nested lists, but your zip expression are a bit confusing. zip可以用于转置嵌套列表,但是您的zip表达式有点令人困惑。

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

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