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

Python 将列表转换为字典时密钥被洗牌

Python 将列表转换为字典时密钥被洗牌,python,machine-learning,nlp,tf-idf,Python,Machine Learning,Nlp,Tf Idf,我正试图从字典中提取关键字。提取后,我将它们存储在一个列表中,并将它们转换回dict。在这样做的同时,我将得到一个无序的键输出。顺序是不守恒的。使用Python3.8。请帮忙。谢谢大家! output = {'aimless': 6.922918004572872, 'distressed': 7.922918004572872, 'drifting': 64.922918004572872, 'nearly': 16.922918004572872, 'attempting':

我正试图从字典中提取关键字。提取后,我将它们存储在一个列表中,并将它们转换回dict。在这样做的同时,我将得到一个无序的键输出。顺序是不守恒的。使用Python3.8。请帮忙。谢谢大家!

output = {'aimless': 6.922918004572872,
  'distressed': 7.922918004572872,
  'drifting': 64.922918004572872,
  'nearly': 16.922918004572872,
  'attempting':856.922918004572872,
  'artiness': 3.922918004572872,
  'existent': 2.922918004572872,
         }
sorted_freq = dict(sorted(output.items(), key=lambda x: x[1]))
nocab = list(sorted_freq.keys())
nocab = nocab[::-1]
brocab = {key for key in nocab}
我得到的输出:

{'aimless',
 'artiness',
 'attempting',
 'distressed',
 'drifting',
 'existent',
 'nearly'}
{'attempting',
 'drifting',
 'nearly',
 'distressed',
 'aimless',
 'artiness',
 'existent'}
from collections import OrderedDict

output = {'aimless': 6.922918004572872,
  'distressed': 7.922918004572872,
  'drifting': 64.922918004572872,
  'nearly': 16.922918004572872,
  'attempting':856.922918004572872,
  'artiness': 3.922918004572872,
  'existent': 2.922918004572872,
         }
sorted_freq = OrderedDict(sorted(output.items(), key=lambda x: x[1], reverse=True))
我想要的输出(根据输出中的值排序):

{'aimless',
 'artiness',
 'attempting',
 'distressed',
 'drifting',
 'existent',
 'nearly'}
{'attempting',
 'drifting',
 'nearly',
 'distressed',
 'aimless',
 'artiness',
 'existent'}
from collections import OrderedDict

output = {'aimless': 6.922918004572872,
  'distressed': 7.922918004572872,
  'drifting': 64.922918004572872,
  'nearly': 16.922918004572872,
  'attempting':856.922918004572872,
  'artiness': 3.922918004572872,
  'existent': 2.922918004572872,
         }
sorted_freq = OrderedDict(sorted(output.items(), key=lambda x: x[1], reverse=True))

Python
dict
的键在3.7之前是无序的

如果需要订购密钥,请尝试
collections.OrderedDict

参考:

编辑:

{'aimless',
 'artiness',
 'attempting',
 'distressed',
 'drifting',
 'existent',
 'nearly'}
{'attempting',
 'drifting',
 'nearly',
 'distressed',
 'aimless',
 'artiness',
 'existent'}
from collections import OrderedDict

output = {'aimless': 6.922918004572872,
  'distressed': 7.922918004572872,
  'drifting': 64.922918004572872,
  'nearly': 16.922918004572872,
  'attempting':856.922918004572872,
  'artiness': 3.922918004572872,
  'existent': 2.922918004572872,
         }
sorted_freq = OrderedDict(sorted(output.items(), key=lambda x: x[1], reverse=True))
如果您只需要钥匙:

> list(sorted_freq.keys())

['attempting',
 'drifting',
 'nearly',
 'distressed',
 'aimless',
 'artiness',
 'existent']

您应该尝试提供一个解决方案,这里的大部分代码都与您的问题无关,只会混淆问题。您能检查一下您的python版本吗?>从Python版本3.7开始,字典是有序的。在Python3.6和更早版本中,字典是无序的。很抱歉,看起来是这样的。请勾选后面的粗体部分。这就是我想要的输出。“我的输出”部分已经指定了它的含义。@Thierrylahuille请检查编辑的问题。希望它遵循指南。我检查了我的版本,它是3.8.6。使用
orderedict
会给我一个
value错误:太多的值无法解包(预计为2个)
请将带有
orderedict
的代码传递给我们好吗?我在没有
orderedict
的情况下也得到了相同的输出。我想要的是列表中这些值的字典等等。。。您希望输出为
dict
还是
set
?因为您的示例输出是一个集合。如果您只需要一个排序的
dict
,那么
sorted\u freq
已经存在了。我想要一个
dict
,只包含
sorted\u freq
中的键。您的最后一个代码片段就是我想要的,但它是以
dict
的形式出现的,甚至不是
dict\u keys()