【问题标题】:Sorting multiple lists using position of data from first list使用第一个列表中的数据位置对多个列表进行排序
【发布时间】:2017-11-17 18:15:06
【问题描述】:

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

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 排序的例子。欢迎任何想法。

【问题讨论】:

  • 另一个列表在哪里?你只展示了一个。
  • @cᴏʟᴅsᴘᴇᴇᴅ OP 大概是指提供的巨型列表中的两个列表。不过我不太确定这里的目标是什么。
  • 它是一个嵌套列表,外部列表为空 [ 和内部列表 [ 因此 [[ 并且内部列表中包含其他列表
  • 美化您的数据。你能显示你的尝试代码吗?
  • 我试图做其他人所做的事情,这个例子非常接近,除了我犯了一个错误link 我知道我需要从索引 1 开始,但这是一个硬编码列表,我的是动态的填充,所以我不能使用他们在那里的命名列表。谢谢亚伦,它看起来更好。

标签: python sorting


【解决方案1】:

如你所愿:

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

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

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2013-10-10
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    相关资源
    最近更新 更多