简体   繁体   English

如何用一系列值替换 Python 字典的所有值?

[英]How can I replace all the values of a Python dictionary with a range of values?

I have the following dictionary:我有以下字典:

mydict = {('a', 'b'): 28.379,
          ('c', 'd'): 32.292,
          ('e', 'f'): 61.295,
          ('g', 'h'): 112.593,
          ('i', 'j'): 117.975}

And I would like to replace all the values with a range from 1 to 5, but keep the order of the keys.我想用 1 到 5 的范围替换所有值,但保持键的顺序。 As a result, I would get this:结果,我会得到这个:

mydict = {('a', 'b'): 1,
          ('c', 'd'): 2,
          ('e', 'f'): 3,
          ('g', 'h'): 4,
          ('i', 'j'): 5}

The length of the dictionary is actually 22000, so I need a range from 1 to 22000. How can I do it?字典的长度实际上是 22000,所以我需要一个从 1 到 22000 的范围。我该怎么做呢?

Thanks in advance.提前致谢。

Using enumerate to iterate on the keys, you can do:使用enumerate对键进行迭代,您可以执行以下操作:

mydict = {('a', 'b'): 28.379,
          ('c', 'd'): 32.292,
          ('e', 'f'): 61.295,
          ('g', 'h'): 112.593,
          ('i', 'j'): 117.975}

for i, key in enumerate(mydict):  # iterates on the keys
    mydict[key] = i


print(mydict)
# {('a', 'b'): 0, ('c', 'd'): 1, ('e', 'f'): 2, ('g', 'h'): 3, ('i', 'j'): 4}

Important note: dicts are only officially ordered since Python 3.7 (and in the CPython implementation since 3.6), so this would n't make much sense with older versions of Python.重要提示:dicts 仅在 Python 3.7 之后才正式订购(并且在 3.6 之后的 CPython 实现中),所以这对于旧版本的 Python 没有多大意义。


To answer your comment: enumerate takes an optional second parameter start (that defaults to 0)回答您的评论: enumerate采用可选的第二个参数start (默认为 0)

So, if you want to start at 1, just do:因此,如果您想从 1 开始,只需执行以下操作:

for i, key in enumerate(mydict, start=1):  # iterates on the keys
    mydict[key] = i

The most simple is to create another dictionary from the keys of the previous one.最简单的方法是从前一个字典的键创建另一个字典。

mydict2=dict()
for i,key in enumerate(mydict):
    mydict2[key]=i+1

You can do this with a one-liner which is more compact:您可以使用更紧凑的单线来做到这一点:

mydict = {('a', 'b'): 28.379,
          ('c', 'd'): 32.292,
          ('e', 'f'): 61.295,
          ('g', 'h'): 112.593,
          ('i', 'j'): 117.975}

{k: i for i, (k, v) in enumerate(mydict.items())}

Pandas solution for this:对此的熊猫解决方案:

import pandas as pd
a = pd.DataFrame(mydict, index=[0]).T
a[0] = list(range(0,len(a)))
a.to_dict()[0]

#  {('a', 'b'): 0, ('c', 'd'): 1, ('e', 'f'): 2, ('g', 'h'): 3, ('i', 'j'): 4}

This can be done gracefully with dict.update and itertools.count , and explicit loops can be avoided:这可以通过dict.updateitertools.count优雅地完成,并且可以避免显式循环:

>>> mydict = {('a', 'b'): 28.379,
...           ('c', 'd'): 32.292,
...           ('e', 'f'): 61.295,
...           ('g', 'h'): 112.593,
...           ('i', 'j'): 117.975}
>>> from itertools import count
>>> mydict.update(zip(mydict, count(1)))
>>> mydict
{('a', 'b'): 1, ('c', 'd'): 2, ('e', 'f'): 3, ('g', 'h'): 4, ('i', 'j'): 5}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM