使用第一个列表中的数据位置对多个列表进行排序

时间:2017-11-17 18:15:06

标签: python sorting

我有一个动态填充的列表,当它构建时,数据被传递到创建堆积条形图。随着列表中的数据排序,图表也是如此。

list: [
    ['Completion Date', 'New', 'NW-New', 'NW-Info', 'NW-Dec', 'NS-Modify', 'SN-Mod', 'VW-NwClter', 'NW-Del', 'VW-ModClter'],
    ['10/15/2017', 1,   0, 0,   0, 0, 0, 0,  0, 0],
    ['10/16/2017', 5,   8, 3,   2, 1, 0, 0,  0, 0],
    ['10/17/2017', 1,   9, 0,  29, 3, 3, 0,  0, 0],
    ['10/18/2017', 4,  44, 0,  11, 1, 0, 2,  0, 0],
    ['10/19/2017', 4,  39, 0,   0, 1, 0, 0,  1, 0],
    ['10/20/2017', 3,   2, 0,   0, 0, 1, 0,  0, 6],
    ['10/21/2017', 0,   0, 0,   0, 0, 0, 2,  0, 0],
    ['10/22/2017', 0,   0, 0,   0, 0, 0, 0,  0, 0],
    ['10/23/2017', 1,  67, 0,  85, 3, 2, 0,  1, 0],
    ['10/24/2017', 2,  25, 1,   4, 5, 0, 0,  1, 1],
    ['10/25/2017', 4,  65, 0,  11, 5, 0, 0, 11, 1],
    ['10/26/2017', 7,  40, 0,   0, 6, 0, 0,  2, 0],
    ['10/27/2017', 2,  37, 0, 115, 2, 0, 0,  0, 0],
    ['10/28/2017', 2,   0, 0,   0, 0, 0, 0,  0, 0],
    ['10/29/2017', 0,   0, 0,   0, 0, 0, 0,  0, 0],
    ['10/30/2017', 5,  53, 0,   0, 3, 0, 0,  1, 0],
    ['10/31/2017', 1,  30, 0,  19, 3, 0, 0,  0, 0],
    ['11/01/2017', 6, 106, 0,   2, 1, 0, 0,  1, 1],
    ['11/02/2017', 5,  74, 0,  10, 0, 0, 0,  9, 0]
]

第一个列表中的数据与其他列表中的数据匹配,完成日期与日期一致,第一个列表中的字符串分别与其他数据一起使用。

我想要一种情况,如果第一个列表上的字符串按字母顺序排序,那么匹配其另一个列表上的索引位置的数据将更改为已排序字符串的新位置。请注意第一个数据字符串完成日期和相应日期保持不变。

我做了一些研究并看到了一些例子,但它们与两个列表相关。就像使用zip排序的一个例子。任何想法都会受到欢迎。

1 个答案:

答案 0 :(得分:0)

正如你问的那样:

  

如果第一个列表中的字符串按字母顺序排序,那么   与其在另一个列表上的索引位置匹配的数据更改为新的   排序字符串的位置。

您可以从此解决方案中获取帮助和提示,并根据您的需要进行修改:

list1=[
    ['Completion Date', 'New', 'NW-New', 'NW-Info', 'NW-Dec', 'NS-Modify', 'SN-Mod', 'VW-NwClter', 'NW-Del', 'VW-ModClter'],
    ['10/15/2017', 'second', 'third', 'forth',   'fifth', 'sixth', 'seven', 'eight',  'nine', 'ten'],
    ['10/16/2017', 5,   8, 3,   2, 1, 0, 0,  0, 0],
    ['10/17/2017', 1,   9, 0,  29, 3, 3, 0,  0, 0],
    ['10/18/2017', 4,  44, 0,  11, 1, 0, 2,  0, 0],
    ['10/19/2017', 4,  39, 0,   0, 1, 0, 0,  1, 0],
    ['10/20/2017', 3,   2, 0,   0, 0, 1, 0,  0, 6],
    ['10/21/2017', 0,   0, 0,   0, 0, 0, 2,  0, 0],
    ['10/22/2017', 0,   0, 0,   0, 0, 0, 0,  0, 0],
    ['10/23/2017', 1,  67, 0,  85, 3, 2, 0,  1, 0],
    ['10/24/2017', 2,  25, 1,   4, 5, 0, 0,  1, 1],
    ['10/25/2017', 4,  65, 0,  11, 5, 0, 0, 11, 1],
    ['10/26/2017', 7,  40, 0,   0, 6, 0, 0,  2, 0],
    ['10/27/2017', 2,  37, 0, 115, 2, 0, 0,  0, 0],
    ['10/28/2017', 2,   0, 0,   0, 0, 0, 0,  0, 0],
    ['10/29/2017', 0,   0, 0,   0, 0, 0, 0,  0, 0],
    ['10/30/2017', 5,  53, 0,   0, 3, 0, 0,  1, 0],
    ['10/31/2017', 1,  30, 0,  19, 3, 0, 0,  0, 0],
    ['11/01/2017', 6, 106, 0,   2, 1, 0, 0,  1, 1],
    ['11/02/2017', 5,  74, 0,  10, 0, 0, 0,  9, 0]
]


track={index:value for index,value in enumerate(list1[0])}  #create a dict for keep tracking of old index


sorted_list=sorted(list1[0])  #sorting the first sub_list which have headers


for item in list1[1:]:
    right = [0] * len(item)
    for index,value in enumerate(item):
        if index in track:
            index_no=sorted_list.index(track.get(index))     #sorting other than first sub_list according to sorted first sub_list
            right[index_no]=value

    print(right)

输出:

['10/15/2017', 'sixth', 'fifth', 'nine', 'forth', 'third', 'second', 'seven', 'ten', 'eight']
['10/16/2017', 1, 2, 0, 3, 8, 5, 0, 0, 0]
['10/17/2017', 3, 29, 0, 0, 9, 1, 3, 0, 0]
['10/18/2017', 1, 11, 0, 0, 44, 4, 0, 0, 2]
['10/19/2017', 1, 0, 1, 0, 39, 4, 0, 0, 0]
['10/20/2017', 0, 0, 0, 0, 2, 3, 1, 6, 0]
['10/21/2017', 0, 0, 0, 0, 0, 0, 0, 0, 2]
['10/22/2017', 0, 0, 0, 0, 0, 0, 0, 0, 0]
['10/23/2017', 3, 85, 1, 0, 67, 1, 2, 0, 0]
['10/24/2017', 5, 4, 1, 1, 25, 2, 0, 1, 0]
['10/25/2017', 5, 11, 11, 0, 65, 4, 0, 1, 0]
['10/26/2017', 6, 0, 2, 0, 40, 7, 0, 0, 0]
['10/27/2017', 2, 115, 0, 0, 37, 2, 0, 0, 0]
['10/28/2017', 0, 0, 0, 0, 0, 2, 0, 0, 0]
['10/29/2017', 0, 0, 0, 0, 0, 0, 0, 0, 0]
['10/30/2017', 3, 0, 1, 0, 53, 5, 0, 0, 0]
['10/31/2017', 3, 19, 0, 0, 30, 1, 0, 0, 0]
['11/01/2017', 1, 2, 1, 0, 106, 6, 0, 1, 0]
['11/02/2017', 0, 10, 9, 0, 74, 5, 0, 0, 0]