简体   繁体   English

最好的是根据元组的第一个元素重新排列一个元组列表以匹配字符串?

[英]What is the best was to rearrange a list of tuples based on their first element, to match a string?

I have a list of tuples that looks like: 我有一个元组列表,看起来像:

[(['A', 'E', 'J', 'M', 'S'],), (['E', 'C', 'D', 'A', 'A'],), (['F', 'B', 'F', 'C', 'C'],)]

or if it's easier, before I used zip() it looked like this: 或者,如果更简单,在我使用zip()之前,它看起来像这样:

[['A', 'E', 'J', 'M', 'S'],
['E', 'C', 'D', 'A', 'A'],
['F', 'B', 'F', 'C', 'C']]

I'm trying to re-order the top row so it reads JAMES, and have the bottom rows follow. 我正在尝试重新排列最上面一行的行,以便它显示为JAMES,然后跟随最下面一行。 What is the best way to go about this. 最好的方法是什么。 Thanks for any help. 谢谢你的帮助。

The output i'm aiming for is 我想要的输出是

[['J', 'A', 'M', 'E', 'S'],
['D', 'E', 'A', 'C', 'A'],
['F', 'F', 'C', 'B', 'C']]

assuming you arrange the first row manually, make a a numpy.ndarray , and slice it with the manually chosen index array: 假设您手动排列第一行,则创建a numpy.ndarray ,并使用手动选择的索引数组对其进行切片:

In [117]: a=np.array([['A', 'E', 'J', 'M', 'S'],
     ...: ['E', 'C', 'D', 'A', 'A'],
     ...: ['F', 'B', 'F', 'C', 'C']])

In [118]: idx=[2,0,3,1,4]  #index array

In [119]: a[:,idx]
Out[119]: 
array([['J', 'A', 'M', 'E', 'S'],
       ['D', 'E', 'A', 'C', 'A'],
       ['F', 'F', 'C', 'B', 'C']], 
      dtype='|S1')

Create a dictionary that contains keys like 'J' , 'A' , 'M' , .. pointing to some numbers to get the desired order and then use this dictionary to sort the unzipped version( zip(*..) ) of your list of lists. 创建一个包含'J''A''M' ,..等键的字典,指向一些数字以获取所需的顺序,然后使用此字典对您的未压缩版本( zip(*..) )进行排序列表清单。

>>> lis = [['A', 'E', 'J', 'M', 'S'],
['E', 'C', 'D', 'A', 'A'],
['F', 'B', 'F', 'C', 'C']]
>>> d = {'J':0, 'A':1, 'M':2, 'E':3, 'S':4}
>>> zip(*sorted(zip(*lis), key= lambda x: (d[x[0]])))
[('J', 'A', 'M', 'E', 'S'), ('D', 'E', 'A', 'C', 'A'), ('F', 'F', 'C', 'B', 'C')]

You want to associate the data with your sort keys, sort, then extract the data in order. 您想要将数据与排序键相关联,进行排序,然后按顺序提取数据。 More or less DSU. 或多或少的DSU。 In this case, zip is a quick way to 'cross' the matrix, so zip the tuples, do your sorting (however you want to do it... in the below, i used a fixed mapping since i don't know your sort criteria), zip the other way: 在这种情况下,使用zip是一种快速“交叉”矩阵的方法,因此请使用zip压缩元组,然后进行排序(但是您想这样做...在下面,由于我不知道您的使用情况,因此我使用了固定映射排序标准),以另一种方式压缩:

input = [['A', 'E', 'J', 'M', 'S'],
    ['E', 'C', 'D', 'A', 'A'],
    ['F', 'B', 'F', 'C', 'C']]

input_zip = zip(*input)
print input_zip
[('A', 'E', 'F'),
 ('E', 'C', 'B'),
 ('J', 'D', 'F'),
 ('M', 'A', 'C'),
 ('S', 'A', 'C')]

indices = [2,0,3,1,4]
output = [input_zip[x] for x in indices]
[('J', 'D', 'F'),
 ('A', 'E', 'F'),
 ('M', 'A', 'C'),
 ('E', 'C', 'B'),
 ('S', 'A', 'C')]

# Zip it the other way now.
output_zip = zip(*output)
[('J', 'A', 'M', 'E', 'S'),
 ('D', 'E', 'A', 'C', 'A'),
 ('F', 'F', 'C', 'B', 'C')]

hopefully that doesn't just answer the question, but explain the thinking behind it. 希望这不仅可以回答问题,而且可以解释其背后的想法。

So it sounds like what you want is to first order the data so that you have the corresponding element from each of your lists in a list of its owns so: 因此,听起来您想要的是首先对数据进行排序,以使每个列表中的对应元素都在其自己的列表中,因此:

    input = [['A', 'E', 'J', 'M', 'S'],
            ['E', 'C', 'D', 'A', 'A'],
            ['F', 'B', 'F', 'C', 'C']]
    rearrangedlist = [[input[x][y] for x in range(len(input))] for y in range(len(input[0]))]

    # Gives rearrangedlist = [['A', 'E', 'F'],
                             ['E', 'C', 'B'],
                             ['J', 'D', 'F'],
                             ['M', 'A', 'C'],
                             ['S', 'A', 'C']]

then: 然后:

    correctorder = ['J','A','M','E','S']
    rearrangeddict = [dict((x[0],x[1:]) for x in rearrangedlist)]
    for lettertofind in correctorder:
        print rearrangeddict[lettertofind]

Gives: 给出:

    ['J', 'D', 'F']
    ['A', 'E', 'F']
    ['M', 'A', 'C']
    ['E', 'C', 'B']
    ['S', 'A', 'C']

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

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