Python 如何通过现有值重新分配字典的值?

Python 如何通过现有值重新分配字典的值?,python,Python,我有一个计数器,比如 Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4}) 我想按现有值的递增顺序重新分配每个元素的值,如 Counter({'the': 0, 'to': 1, 'of': 2, 'independence': 3, 'puigdemont': 4, 'mr': 5, 'a': 6, 'spain':

我有一个计数器,比如

Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})
我想按现有值的递增顺序重新分配每个元素的值,如

Counter({'the': 0, 'to': 1, 'of': 2, 'independence': 3, 'puigdemont': 4, 'mr': 5, 'a': 6, 'spain': 7, 'for': 8})
有什么可能的方法吗

提前谢谢

更新:

(我的英语不是很好,所以你可以跳过我的解释,滚下来看看下面的例子。) 对不起,我的问题好像没有说清楚。实际上,整个反对象要长得多。对象是从一段中获取的,每个单词的值是该段中的出现次数。我想建立一个字典,用字典中相应的值替换我段落中的单词。字典中的值是按我段落中单词的频率排序的,如果两个单词出现的频率相同,则按字母顺序排序

例如:

string=“有烟就有火” 字符串中每个单词的出现次数: 式中=1,式中=2,式中=2,烟=1,火=1。 所以我需要一本字典,比如:

{“is”: 0, “there”: 1, ”fire”:2 , “smoke”: 3, “where”:4}
最常见的单词是“is”和“there”,但按字母顺序,“i”在“t”前面,所以“is”是0,“there”是1

有什么好方法可以做到这一点吗

非常感谢!!

您需要:

输出:

Counter({'for': 8, 'spain': 7, 'a': 6, 'mr': 5, 'puigdemont': 4, 'independence': 3, 'of': 2, 'to': 1, 'the': 0})
这里有一个

您需要一个:

输出:

Counter({'for': 8, 'spain': 7, 'a': 6, 'mr': 5, 'puigdemont': 4, 'independence': 3, 'of': 2, 'to': 1, 'the': 0})

在这里,您可以访问每个键并更改其值:

from collections import Counter

a_dict = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})

n = 0
for d in a_dict:    
    a_dict[d] = n
    n += 1

>>> a_dict
Counter({'for': 8, 'spain': 7, 'a': 6, 'mr': 5, 'puigdemont': 4, 'independence': 3, 'of': 2, 'to': 1, 'the': 0})
如果可以使用元组的有序列表:

>>> sorted(a_dict.items(), key=lambda x: x[1])
[('the', 0), ('to', 1), ('of', 2), ('independence', 3), ('puigdemont', 4), ('mr', 5), ('a', 6), ('spain', 7), ('for', 8)]

访问每个键并更改其值:

from collections import Counter

a_dict = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})

n = 0
for d in a_dict:    
    a_dict[d] = n
    n += 1

>>> a_dict
Counter({'for': 8, 'spain': 7, 'a': 6, 'mr': 5, 'puigdemont': 4, 'independence': 3, 'of': 2, 'to': 1, 'the': 0})
如果可以使用元组的有序列表:

>>> sorted(a_dict.items(), key=lambda x: x[1])
[('the', 0), ('to', 1), ('of', 2), ('independence', 3), ('puigdemont', 4), ('mr', 5), ('a', 6), ('spain', 7), ('for', 8)]

正如我从你的评论中了解到的,你不需要排序计数器,所以

c = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})

for i, k in enumerate(c.most_common()):
    c[k[0]] = i
结果:

Counter({'spain': 8, 'for': 7, 'a': 6, 'puigdemont': 5, 'independence': 4, 'mr': 3, 'of': 2, 'the': 1, 'to': 0})
{'a': 6, 'spain': 8, 'of': 4, 'mr': 3, 'the': 0, 'for': 7, 'to': 1, 'independence': 2, 'puigdemont': 5}
更新:

m = c.most_common()
res = {k[0]: i for i, k in enumerate(sorted(m, key=lambda x: (-x[1], x[0])))}
结果:

Counter({'spain': 8, 'for': 7, 'a': 6, 'puigdemont': 5, 'independence': 4, 'mr': 3, 'of': 2, 'the': 1, 'to': 0})
{'a': 6, 'spain': 8, 'of': 4, 'mr': 3, 'the': 0, 'for': 7, 'to': 1, 'independence': 2, 'puigdemont': 5}

正如我从你的评论中了解到的,你不需要排序计数器,所以

c = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})

for i, k in enumerate(c.most_common()):
    c[k[0]] = i
结果:

Counter({'spain': 8, 'for': 7, 'a': 6, 'puigdemont': 5, 'independence': 4, 'mr': 3, 'of': 2, 'the': 1, 'to': 0})
{'a': 6, 'spain': 8, 'of': 4, 'mr': 3, 'the': 0, 'for': 7, 'to': 1, 'independence': 2, 'puigdemont': 5}
更新:

m = c.most_common()
res = {k[0]: i for i, k in enumerate(sorted(m, key=lambda x: (-x[1], x[0])))}
结果:

Counter({'spain': 8, 'for': 7, 'a': 6, 'puigdemont': 5, 'independence': 4, 'mr': 3, 'of': 2, 'the': 1, 'to': 0})
{'a': 6, 'spain': 8, 'of': 4, 'mr': 3, 'the': 0, 'for': 7, 'to': 1, 'independence': 2, 'puigdemont': 5}

要按频率和字母顺序对单词进行排序,然后根据字典为每个单词指定唯一关键字,请执行以下操作:

from collections import Counter

c = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})
res = {word: unique_id for unique_id, (_, word) in enumerate(
    sorted([(-freq, word) for word, freq in c.most_common()]))
}

print(res)
输出:

{'the': 0, 'to': 1, 'independence': 2, 'mr': 3, 'of': 4, 'puigdemont': 5, 'a': 6, 'for': 7, 'spain': 8}
请注意,结果是dict,因此不一定是有序的(在cpython 3.6中,结果是有序的,但这是一个不应该依赖的实现细节)

最内部的理解用于创建(-freq,word)元组,该元组将生成所需的排序顺序。 外部理解丢弃频率(解包键值,只保留单词),并使用枚举生成唯一id

编辑:如果输出中需要顺序,则使用:

from collections import Counter, OrderedDict

c = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})
res = OrderedDict((word, unique_id) for unique_id, (_, word) in enumerate(
    sorted([(-freq, word) for word, freq in c.most_common()]))
)

print(res)

要按频率和字母顺序对单词进行排序,然后根据字典为每个单词指定唯一关键字,请执行以下操作:

from collections import Counter

c = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})
res = {word: unique_id for unique_id, (_, word) in enumerate(
    sorted([(-freq, word) for word, freq in c.most_common()]))
}

print(res)
输出:

{'the': 0, 'to': 1, 'independence': 2, 'mr': 3, 'of': 4, 'puigdemont': 5, 'a': 6, 'for': 7, 'spain': 8}
请注意,结果是dict,因此不一定是有序的(在cpython 3.6中,结果是有序的,但这是一个不应该依赖的实现细节)

最内部的理解用于创建(-freq,word)元组,该元组将生成所需的排序顺序。 外部理解丢弃频率(解包键值,只保留单词),并使用枚举生成唯一id

编辑:如果输出中需要顺序,则使用:

from collections import Counter, OrderedDict

c = Counter({'the': 10, 'to': 10, 'of': 5, 'independence': 5, 'puigdemont': 5, 'mr': 5, 'a': 4, 'spain': 4, 'for': 4})
res = OrderedDict((word, unique_id) for unique_id, (_, word) in enumerate(
    sorted([(-freq, word) for word, freq in c.most_common()]))
)

print(res)


什么阻止了你?一个
计数器
不是有序的,所以你的建议没有什么意义,你可以创建一个有序的计数器,不过你以后将如何使用重新分配的计数器?@jonatan呃,我是python新手,我在谷歌上搜索了我的问题,没有任何有用的…@Chris_Rands,谢谢,我会检查它。什么阻止了你?一个
计数器
不是有序的,所以你的建议没有什么意义,你可以创建一个有序的计数器,不过你以后如何使用重新分配的计数器?@jonatan呃,我是python新手,我用谷歌搜索了我的问题,但没有得到任何有用的结果…@Chris_Rands谢谢,我会检查它。谢谢你回答我。但是,它对我不起作用……我已经更新了我的qu更多详细描述请点击estion。谢谢你回答我。但是,它不适用于我…我更新了我的问题以获得更详细的描述。谢谢你回答我。但是,它不适用于我…我更新了我的问题以获得更详细的描述。谢谢你回答我。但是,它不适用于我…我更新了我的答案问题获取更详细的描述。谢谢你回答我。但是,它对我不起作用…我更新了我的问题获取更详细的描述。谢谢你回答我。但是,它对我不起作用…我更新了我的问题获取更详细的描述。天哪…它起作用了!!难以置信!非常感谢!我会努力理解你的回答代码。天哪…它工作!!难以置信!非常感谢!我会尽力理解你的代码。