我有一个从数据库中的两个字段读取的值字典:字符串字段和数字字段.字符串字段是唯一的,因此这是字典的键.
我可以对键进行排序,但是如何根据值进行排序?
注意:我已阅读Stack Overflow问题如何按Python中字典的值对字典列表进行排序?并且可能可以更改我的代码以获得字典列表,但由于我不需要字典列表,我想知道是否有更简单的解决方案.
可能重复:
如何将Python字典转换为元组列表?
我正在尝试将Python字典转换为Python列表,以便执行一些计算.
#My dictionary
dict = {}
dict['Capital']="London"
dict['Food']="Fish&Chips"
dict['2012']="Olympics"
#lists
temp = []
dictList = []
#My attempt:
for key, value in dict.iteritems():
aKey = key
aValue = value
temp.append(aKey)
temp.append(aValue)
dictList.append(temp)
aKey = ""
aValue = ""
Run Code Online (Sandbox Code Playgroud)
这是我的尝试......但我无法弄清楚什么是错的?
我有一个看起来有点像这样的柜台:
Counter: {('A': 10), ('C':5), ('H':4)}
Run Code Online (Sandbox Code Playgroud)
我想按字母顺序对键进行排序,而不是按字母顺序排序 counter.most_common()
有没有办法实现这个目标?
我试图按键排序dict但没有机会.这是我的词:
result={'1':'value1','2':'value2',...}
Run Code Online (Sandbox Code Playgroud)
我正在使用Python2.7,我发现了这一点
keys = result.keys()
keys.sort()
Run Code Online (Sandbox Code Playgroud)
但这不是我的预期,我有一个未分类的词典.
这是字典看起来像:
{'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40}
Run Code Online (Sandbox Code Playgroud)
我想按数字顺序对字典进行排序,结果应该是:
{'57480': 89, '57481': 50, '57482': 18, '57483': 110, '57484': 40, '57485': 82}
Run Code Online (Sandbox Code Playgroud)
我试过sorted(self.docs_info.items)
但它不起作用.
我创建了一个python字典,它具有以下形式的键:
11, 10, 00, 01, 20, 21, 31, 30
Run Code Online (Sandbox Code Playgroud)
键是字符串
我想按以下顺序维护字典:
00, 10, 20, 30, 01, 11, 21, 31
Run Code Online (Sandbox Code Playgroud)
这基于密钥的第二个值。
我试过了sorted(dict.items(), key = lambda s: s[1])
,得到了像这样的键:
20, 30, 21, 31, 01, 11, 10, 00
Run Code Online (Sandbox Code Playgroud)
有人可以指导我吗?
我想以“自然顺序”对字典键进行排序。如果我有一本带键的字典
d = {"key1" : object, "key11" : object, "key2" : object, "key22" : object", "jay1" : object, "jay2" : object}
Run Code Online (Sandbox Code Playgroud)
我想对这本字典进行排序,结果是:
d = { "jay1" : object, "jay2" : object, "key_1" : object, "key_2" : object, "key_11" : object, "key_22" : object"}
Run Code Online (Sandbox Code Playgroud) 好的,所以我有一个包含字符串中字符位置的字典,但是,我在字典中有单独的字符串,它看起来像这样:
{
'24-35': 'another word',
'10-16': 'content',
'17': '[',
'22': ']',
'23': '{',
'37': ')',
'0-8': 'beginning',
'36': '}',
'18-21': 'word',
'9': '(',
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试按键对这个数组进行排序,使其看起来像这样
{
'0-8': 'beginning',
'9': '(',
'10-16': 'content',
'17': '[',
'18-21': 'word',
'22': ']',
'23': '{',
'24-35': 'another word',
'36': '}',
'37': ')'
}
Run Code Online (Sandbox Code Playgroud)
字典是foreach
通过使用这个字符串来构建的:
' beginning(content[word]{another word})
',并在括号处拆分。
我正在尝试使用@Brian对这个问题的回答对字典进行排序,但是,它在测距过程中按字母顺序排序(因为它们已转换为字符串)(使它说 '0- 8')。
我的问题是:
我该如何转型:
class SortedDisplayDict(dict):
def __str__(self):
return "{" + ", …
Run Code Online (Sandbox Code Playgroud) 我已经将collections函数中的Counter函数应用到列表中.在我这样做之后,我不清楚新数据结构的内容将被表征为什么.我也不确定访问元素的首选方法是什么.
我做过类似的事情:
theList = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
newList = Counter(theList)
print newList
Run Code Online (Sandbox Code Playgroud)
返回:
Counter({'blue': 3, 'red': 2, 'yellow': 1})
Run Code Online (Sandbox Code Playgroud)
如何访问每个元素并打印出如下内容:
blue - 3
red - 2
yellow - 1
Run Code Online (Sandbox Code Playgroud) 我找了很长时间,没有找到实际的答案,因为我看不到任何以升序键开始然后以降序值开头的答案。
一个更清楚的例子:
d = {'banana':3, 'orange':5, 'apple':5}
out: [('apple', 5), ('orange', 5), ('banana', 3)]
Run Code Online (Sandbox Code Playgroud)
在做了一些研究之后,我得出了这样的结论:
sorted(d.items(), key=operator.itemgetter(1,0), reverse=True)
out: [('orange', 5), ('apple', 5), ('banana', 3)]
Run Code Online (Sandbox Code Playgroud)
这是因为它对值和键都进行了反向排序。我需要不倒转的钥匙。
我真的很感激这里的一些帮助。提前致谢!
python ×10
dictionary ×8
sorting ×6
list ×2
python-2.7 ×2
collections ×1
counter ×1
natural-sort ×1