相关疑难解决方法(0)

如何将元素添加到OrderedDict的开头?

我有这个:

d1 = OrderedDict([('a', '1'), ('b', '2')])
Run Code Online (Sandbox Code Playgroud)

如果我这样做:

d1.update({'c':'3'})
Run Code Online (Sandbox Code Playgroud)

然后我明白了:

OrderedDict([('a', '1'), ('b', '2'), ('c', '3')])
Run Code Online (Sandbox Code Playgroud)

但我想要这个:

[('c', '3'), ('a', '1'), ('b', '2')]
Run Code Online (Sandbox Code Playgroud)

没有创建新词典.

python dictionary python-2.x python-3.x ordereddict

68
推荐指数
5
解决办法
8万
查看次数

标签 统计

dictionary ×1

ordereddict ×1

python ×1

python-2.x ×1

python-3.x ×1