简体   繁体   English

Python单元素元组

[英]python single element tuple

Suppose I have a matrix M and an indexing set idx=[(0,1),(2,3),(3,2)] and I want to create two sets of tuples, idx_leq1 consisting of those tuples whose first and second elements are both less than or equal to 1 and idx_geq2 consisting of those tuples whose first and second elements are both greater than or equal to 2. 假设我有一个矩阵M和一个索引集idx=[(0,1),(2,3),(3,2)] ,我想创建两组元组, idx_leq1由第一和第二个元组组成元素均小于或等于1,并且idx_geq2由第一和第二个元素均大于或等于2的那些元组组成。

I want to access the elements M[idx_leq1] and M[idx_geq2] cleanly. 我想干净地访问元素M[idx_leq1]M[idx_geq2] I have tried idx_leq1 = tuple([e for e in idx if e[0]<=1 and e[1]<=1]) , but this returns idx_leq1 = ((0,1),) which I can't use to index M . 我已经尝试过idx_leq1 = tuple([e for e in idx if e[0]<=1 and e[1]<=1])idx_leq1 = ((0,1),) ,但我无法用于索引M On the other hand, idx_geq2 = tuple([e for e in idx if e[0]>=2 and e[1]>=2]) = ((2,3),(3,2)) works. 另一方面, idx_geq2 = tuple([e for e in idx if e[0]>=2 and e[1]>=2]) = ((2,3),(3,2))有效。

How can I solve this for the case where my first index set consists of only one coordinate pair? 如果我的第一个索引集仅包含一个坐标对,该如何解决? I don't want to do M[idx_leq1[0]] . 我不想做M[idx_leq1[0]]

I can do: list(chain(*[(e,) for e in idx if e[0]<=1 and e[1]<=1])) and list(chain(*[(e,) for e in idx if e[0]>=2 and e[1]>=2])) , but then I still have to grab the first element for idx_leq1 whereas I can pass idx_geq2 to M and grab the appropriate elements. 我可以做到: list(chain(*[(e,) for e in idx if e[0]<=1 and e[1]<=1]))list(chain(*[(e,) for e in idx if e[0]>=2 and e[1]>=2])) ,那么我仍然必须获取idx_leq1的第一个元素,而我可以将idx_geq2传递给M并获取适当的元素。

Thanks! 谢谢!

[Tested with numpy.mat ] [使用numpy.mat测试]

[M[0, 1]] should be fetched as in M[[0], [1]] . [M[0, 1]]应该像M[[0], [1]]那样被获取。 When indexing matrix, Multidimensional list-of-locations indexing requires k lists of index each working with one dimension. 在对矩阵建立索引时, 多维位置列表索引需要k个索引列表,每个索引都使用一个维度。

For example, in order to fetch M[0, 3], M[1, 4], M[2, 5] , one should use M[[0, 1, 2], [3, 4, 5]] . 例如,为了获取M[0, 3], M[1, 4], M[2, 5] ,应该使用M[[0, 1, 2], [3, 4, 5]] In other word, the index that you give to M should not be considered lists of coordinates. 换句话说,您赋予M的索引不应视为坐标列表。 Rather, they are lists of "coordinates on each dimension". 而是,它们是“每个维度上的坐标”的列表。
In your case, a M[[0, 1]] (or its equivalent in tuple type) fetches M[0], M[1] as [0, 1] is considered to work on the first dimension, and the second dimension is broadcasted. 在您的情况下, M[[0, 1]] (或等效的元组类型)将M[0], M[1]提取为[0, 1]被认为适用于第一维,而第二维被广播。

Ref: http://scipy-cookbook.readthedocs.io/items/Indexing.html#Multidimensional-list-of-locations-indexing 参考: http : //scipy-cookbook.readthedocs.io/items/Indexing.html#MultiDimension-list-of-locations-indexing
This reference believes that the reason to use "list of dims" rather than "list of coordinates" is to save number of instances, as unpacking many tuples might be expensive. 该参考资料认为,使用“暗角列表”而不是“坐标列表”的原因是为了节省实例数,因为解包许多元组可能很昂贵。

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

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