Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个列表按顺序合并到字典中?_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 如何将两个列表按顺序合并到字典中?

Python 如何将两个列表按顺序合并到字典中?,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我试图将两个列表的值合并到一个字典集中,但是在合并字典之后,键和值并没有按照列表中的原始顺序排列,而是与随机键和值配对,所以如何按顺序组合它们?请参见下面的输入和输出: data_ids = ['22630876', '22626950', '22624826', '22626159', '22616496', '22601480', '22611197', '22600498', '22605808', '22602601', '22602543', '22594071', '22595982

我试图将两个列表的值合并到一个字典集中,但是在合并字典之后,键和值并没有按照列表中的原始顺序排列,而是与随机键和值配对,所以如何按顺序组合它们?请参见下面的输入和输出:

data_ids = ['22630876', '22626950', '22624826', '22626159', '22616496', '22601480', '22611197', '22600498', '22605808', '22602601', '22602543', '22594071', '22595982', '22593725', '22591441', '22553315', '22584758']



tag_ids = ['WATCH - Stokes faces unpredictable balls', "'Bad position, but we're not out of it' - de Villiers", 'Who is Sandeep Lamichhane?', 'WATCH - All the action from the Super Over', "WATCH - Sammy's four-ball blitz", "Shubman Gill's red-hot run streak", 'Should bowlers start wearing helmets?', 'Keshav Maharaj could be key - Graeme Smith', "WATCH - Kevin Pietersen's match-winning 48", "'It was emotional walking off the pitch' - Stokes", "WATCH - India's gains from the South Africa tour", 'WATCH - Best of Kohli in South Africa', 'The Ashwin-Gibbs exchange: funny, or not?', "Mayank Agarwal's incredible run of domestic form", "'SA can't afford spicy pitches against Australia'", 'Ice Cricket: Legends play T20 in the Alps', 'Dhoni, Kohli and quirky on-field  chatter']


z = dict(zip(data_ids,tag_ids))

print(z)



{'22595982': 'The Ashwin-Gibbs exchange: funny, or not?', '22593725': "Mayank Agarwal's incredible run of domestic form", '22626159': 'WATCH - All the action from the Super Over', '22553315': 'Ice Cricket: Legends play T20 in the Alps', '22626950': "'Bad position, but we're not out of it' - de Villiers", '22624826': 'Who is Sandeep Lamichhane?', '22611197': 'Should bowlers start wearing helmets?', '22630876': 'WATCH - Stokes faces unpredictable balls', '22600498': 'Keshav Maharaj could be key - Graeme Smith', '22602601': "'It was emotional walking off the pitch' - Stokes", '22602543': "WATCH - India's gains from the South Africa tour", '22591441': "'SA can't afford spicy pitches against Australia'", '22601480': "Shubman Gill's red-hot run streak", '22594071': 'WATCH - Best of Kohli in South Africa', '22605808': "WATCH - Kevin Pietersen's match-winning 48", '22584758': 'Dhoni, Kohli and quirky on-field  chatter', '22616496': "WATCH - Sammy's four-ball blitz"}

正如您所看到的,
z
的输出以键值
'22595982'
开始,但它应该是
'22630876'
,即使与dictionary的值相同,我如何才能做到这一点,我搜索了类似的问题,但没有找到任何

当使用默认的
dict
时,即使它恰好符合某些期望,也不应该依赖元素的顺序。有一个专用的字典实现,从2.7开始保持插入顺序:

class collections.OrderedDict([items])
返回dict子类的实例,支持常用的dict方法。OrderedDict是一种记录键首次插入顺序的dict。如果新条目覆盖现有条目,则原始插入位置保持不变。删除条目并重新插入会将其移动到末尾

例如:

from collections import OrderedDict

d = OrderedDict()

d["1"] = 1
d["5"] = 5
d["2"] = 2
print(d) #OrderedDict([('1', 1), ('5', 5), ('2', 2)])

new_d = dict(d)

print(d) #{'1': 1, '5': 5, '2': 2}

试试这个,我想这就是你的答案

from collections import OrderedDict

d = OrderedDict(zip(data_ids, tag_ids))
for i, j in d.items():
    print(i)
    print(j)
产出:

22630876
观看-斯托克斯面对不可预测的球
22626950
“位置不好,但我们还没有摆脱它”-德维利尔斯
22624826
谁是桑德普·拉米切恩?
22626159
观看-超级明星的所有动作结束
22616496
观看-萨米的四球闪电战
22601480
舒曼·吉尔的红热跑步记录
22611197
投球手应该开始戴头盔吗?
22600498
克沙夫·马哈拉吉可能是关键人物——格雷姆·史密斯
22605808
观看-凯文·皮特森的比赛48胜
22602601
“走出球场时很激动人心”——斯托克斯
22602543
印度在南非之行中的收获
22594071
手表-南非最好的科利手表
22595982
阿什温·吉布斯交换:有趣还是不有趣?
22593725
马扬克·阿加瓦尔令人难以置信的国内表现
22591441
“南非无法负担与澳大利亚队的辛辣比赛”
22553315
冰上蟋蟀:传说在阿尔卑斯山玩T20
22584758
多尼、科利和离奇的现场聊天

由于列表是一个有序的集合,这对我来说很有用:

mydict = dict(zip(list1, list2))

使用Python3.7。它不适用于3.5.2吗@他们会正确配对,这就是最重要的。如果您需要保留原始顺序,那么这里的数据结构就错了。json.dumps()做什么?只需打印变量
d
的输出,您将得到的答案是
OrderedDict
而不是json格式的dict,因此只需使用
json.dumps
转换为纯json是的,但输出数据字符串,我无法在模板上呈现此错误,我得到jinja2.exceptions.UndefinedError:'str object'没有属性'items'错误我可以修复此错误吗@amazingthingsaround@steve对请稍等。。让我试试t。