Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.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_Sorting_Dictionary_Key - Fatal编程技术网

Python 重新排列字典,使值成为下一个键

Python 重新排列字典,使值成为下一个键,python,python-3.x,sorting,dictionary,key,Python,Python 3.x,Sorting,Dictionary,Key,我有一本字典如下: {1: 3, 2: 4, 3: 5, 4: 1, 5: 2} 我想重新排列它,使当前对的值成为下一对的键,如下所示: {1: 3, 3: 5, 5: 2, 2: 4, 4: 1} 如何实现这一点?使用以下代码可以实现这一点-自(Python 3.6 for CPython)起的字典是按插入顺序排列的,因此我们从d中的第一个键开始,然后迭代,更新下一个键以使用最后一个键的值进行检查,直到我们新字典的长度和旧字典一样 代码: d = {1: 3, 2: 4, 3: 5, 4:

我有一本字典如下:

{1: 3, 2: 4, 3: 5, 4: 1, 5: 2}
我想重新排列它,使当前对的值成为下一对的键,如下所示:

{1: 3, 3: 5, 5: 2, 2: 4, 4: 1}

如何实现这一点?

使用以下代码可以实现这一点-自(Python 3.6 for CPython)起的字典是按插入顺序排列的,因此我们从
d
中的第一个键开始,然后迭代,更新下一个键以使用最后一个键的值进行检查,直到我们新字典的长度和旧字典一样

代码:

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
>>> d_new
{1: 3, 3: 5, 5: 2, 2: 4, 4: 1}
from collections import OrderedDict

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = 1

d_new = OrderedDict()

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
d = {1: 3, 2: 4, 3: 1, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    if curr not in d_new:
        d_new[curr] = curr = d[curr]
    else:
        curr = next(x for x in d.keys() if x not in d_new)
>>> d_new
{1: 3, 3: 1, 2: 4, 4: 1, 5: 2}
输出:

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
>>> d_new
{1: 3, 3: 5, 5: 2, 2: 4, 4: 1}
from collections import OrderedDict

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = 1

d_new = OrderedDict()

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
d = {1: 3, 2: 4, 3: 1, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    if curr not in d_new:
        d_new[curr] = curr = d[curr]
    else:
        curr = next(x for x in d.keys() if x not in d_new)
>>> d_new
{1: 3, 3: 1, 2: 4, 4: 1, 5: 2}
在Python 3.7下面,您可以使用:

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
>>> d_new
{1: 3, 3: 5, 5: 2, 2: 4, 4: 1}
from collections import OrderedDict

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = 1

d_new = OrderedDict()

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
d = {1: 3, 2: 4, 3: 1, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    if curr not in d_new:
        d_new[curr] = curr = d[curr]
    else:
        curr = next(x for x in d.keys() if x not in d_new)
>>> d_new
{1: 3, 3: 1, 2: 4, 4: 1, 5: 2}
根据下面的注释,如果键/值包含循环,我们希望移动到下一个键-值对。这可以通过以下方式实现:

{1: 3, 2: 4, 3: 5, 4: 1, 5: 2}
代码:

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
>>> d_new
{1: 3, 3: 5, 5: 2, 2: 4, 4: 1}
from collections import OrderedDict

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = 1

d_new = OrderedDict()

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
d = {1: 3, 2: 4, 3: 1, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    if curr not in d_new:
        d_new[curr] = curr = d[curr]
    else:
        curr = next(x for x in d.keys() if x not in d_new)
>>> d_new
{1: 3, 3: 1, 2: 4, 4: 1, 5: 2}
输出:

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
>>> d_new
{1: 3, 3: 5, 5: 2, 2: 4, 4: 1}
from collections import OrderedDict

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

curr = 1

d_new = OrderedDict()

while len(d) > len(d_new):
    d_new[curr] = curr = d[curr]
d = {1: 3, 2: 4, 3: 1, 4: 1, 5: 2}

curr = list(d.keys())[0]

d_new = {}

while len(d) > len(d_new):
    if curr not in d_new:
        d_new[curr] = curr = d[curr]
    else:
        curr = next(x for x in d.keys() if x not in d_new)
>>> d_new
{1: 3, 3: 1, 2: 4, 4: 1, 5: 2}

您可以使用itertools中的累积来完成此操作:

d = {1: 3, 2: 4, 3: 5, 4: 1, 5: 2}

from itertools import accumulate
od = {k:d[k] for k in accumulate(d,lambda k,_:d[k]) }

#od : {1: 3, 3: 5, 5: 2, 2: 4, 4: 1}

就我所记得的,python dict没有指定要排序,因此在键值对中没有排序。打印词典时看到的顺序是任意的。编辑:似乎是这样的:)我认为您必须创建一个新的字典,并从一开始就按正确的顺序插入键,因为我认为它们不可重新排序。查看OrderedDict结构类型:检查实际上,从Python 3.6开始,
{k:d[k]for k in accumulate(d,d.get)}
也可以工作。