How to sort dictionaries by keys in Python

Can anyone tell me how I can sort this: {'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']} into {'a': [1, 2, 3], 'b': ['blah', 'bhasdf', 'asdf'], 'c': ['one', 'two'],'d': ['asdf', 'wer', 'asdf', 'zxcv']} ? Thanks! UPDATE 1, code sample: So I am doing linguistics. One article is broken down to words that are stored in a database a

如何用Python中的键对字典进行排序

任何人都可以告诉我如何排序: {'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']} 成 {'a': [1, 2, 3], 'b': ['blah', 'bhasdf', 'asdf'], 'c': ['one', 'two'],'d': ['asdf', 'wer', 'asdf', 'zxcv']} ? 谢谢! 更新1,代码示例: 所以我在做语言学。 一篇文章被分解为存储在数据库中的单词,并具有各种属性,包括段ID和句子ID。 任务:试图重建原文

python dictionary values sorting

I have 2 dictionaries, dict1 and dict2 which contain the same keys, but different values for the keys. What I want to do is for each dictionary, sort the values from largest to smallest, and then give each value a rank 1-N, 1 being the largest value. From here, I want to get the difference of the ranks for the values in each dictionary for the same key. For example: dict1 = {a:0.6, b:0.3, c:0

python字典值排序

我有2个字典, dict1和dict2 ,它们包含相同的密钥,但密钥的值不同。 我想要做的是为每个字典,将值从大到小排序,然后给每个值排序1-N,1是最大的值。 从这里,我想获得每个字典中相同键的值的等级差异。 例如: dict1 = {a:0.6, b:0.3, c:0.9, d:1.2, e:0.2} dict2 = {a:1.4, b:7.7, c:9.0, d:2.5, e:2.0} # sorting by values would look like this: dict1 = {d:1.2, c:0.9, a:0.6, b:0.3, e:0.2} dict2 = {c:9.0, b:7.7

How do I sort a dictionary by value?

I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary. I can sort on the keys, but how can I sort based on the values? Note: I have read Stack Overflow question How do I sort a list of dictionaries by values of the dictionary in Python? and probably could change my code to have a

如何按价值对字典进行排序?

我有一个从数据库中的两个字段中读取值的字典:一个字符串字段和一个数字字段。 字符串字段是唯一的,所以这是字典的关键。 我可以对键进行排序,但是如何根据这些值进行排序? 注意:我已阅读Stack Overflow问题如何按Python中字典的值对字典列表进行排序? 并可能可以改变我的代码有一个字典的列表,但因为我真的不需要一个字典的列表,我想知道是否有一个更简单的解决方案。 对字典进行排序是不可能的,只能得到已排

What are "named tuples" in Python?

Reading the changes in Python 3.1, I found something... unexpected: The sys.version_info tuple is now a named tuple : I never heard about named tuples before, and I thought elements could either be indexed by numbers (like in tuples and lists) or by keys (like in dicts). I never expected they could be indexed both ways. Thus, my questions are: What are named tuples? How to use them?

什么是Python中的“命名元组”?

阅读Python 3.1的变化,我发现了一些......意想不到的事情: sys.version_info元组现在是一个命名元组 : 我以前从来没有听说过命名元组,并且我认为元素可以通过数字(如元组和列表)或按键(如在字典中)进行索引。 我从未想到他们可以通过两种方式进行索引。 因此,我的问题是: 什么是命名元组? 如何使用它们? 为什么/何时应该使用命名元组而不是普通元组? 为什么/何时应该使用普通的元组而不是命名的元组

What is the most "pythonic" way to iterate over a list in chunks?

I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples. Currently, I'm iterating over it this way: for i in xrange(0, len(ints), 4): # dummy op for example code foo += ints[i] * ints[i + 1] + ints[i + 2] * in

在区块中迭代列表的最“pythonic”方法是什么?

我有一个Python脚本,它需要输入一个整数列表,我需要一次处理四个整数。 不幸的是,我没有对输入的控制,或者我将它作为四元组元素列表传入。 目前,我正在以这种方式迭代它: for i in xrange(0, len(ints), 4): # dummy op for example code foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3] 但它看起来很像“C-think”,这让我怀疑是否有更多的pythonic方式来处理这种情况。 该列表在迭代后被丢弃,所

How do I use Python's itertools.groupby()?

I haven't been able to find an understandable explanation of how to actually use Python's itertools.groupby() function. What I'm trying to do is this: Take a list - in this case, the children of an objectified lxml element Divide it into groups based on some criteria Then later iterate over each of these groups separately. I've reviewed the documentation, and the examples

我如何使用Python的itertools.groupby()?

我一直无法找到如何真正使用Python的itertools.groupby()函数的可理解的解释。 我想要做的是这样的: 列出一个列表 - 在这种情况下,一个客体化的lxml元素的孩子 根据一些标准将它分成组 然后稍后分别遍历每个这些组。 我已经查阅了文档和示例,但是我试图将它们应用到一个简单的数字列表之外时遇到了麻烦。 那么,我该如何使用itertools.groupby() ? 我应该使用另一种技术吗? 指出良好的“先决条件”阅读也将不胜

Removing coordinates from list on python

This question already has an answer here: How to remove items from a list while iterating? 18 answers You cannot change something while you're iterating it. The results are weird and counter-intuitive, and nearly never what you want. In fact, many collections explicitly disallow this (eg sets and dicts). Instead, iterate over a copy (for e in a[:]: ...) or, instead of modifying an ex

从python列表中删除坐标

这个问题在这里已经有了答案: 如何在迭代时从列表中删除项目? 18个答案 迭代时你不能改变某些东西。 结果很奇怪,反直觉,几乎从来没有你想要的。 事实上,许多收集明确地禁止这(例如集和字典)。 相反,迭代一个副本(对于[:]中的e),或者,而不是修改现有列表,过滤它以获得包含您想要的项目的新列表([e for e in a if ..] 。])。 请注意,在许多情况下,您不必再次迭代过滤,只需将过滤与数据生成合并即可。

What is the quickest way to HTTP GET in Python?

What is the quickest way to HTTP GET in Python if I know the content will be a string? I am searching the docs for a quick one-liner like: contents = url.get("http://example.com/foo/bar") But all I can find using Google are httplib and urllib - and I am unable to find a shortcut in those libraries. Does standard Python 2.5 have a shortcut in some form as above, or should I write a function u

在Python中使用HTTP GET最快的方法是什么?

如果我知道内容将是一个字符串,那么在Python中使用HTTP GET的最快捷方式是什么? 我正在寻找一个快速的单行文件,如: contents = url.get("http://example.com/foo/bar") 但我可以找到使用谷歌的是httplib和urllib - 我无法在这些库中找到快捷方式。 标准Python 2.5是否具有上述某种形式的快捷方式,还是应该编写一个函数url_get ? 我宁愿不捕获对wget或curl进行脱壳的输出。 Python 2.x: import urllib2 contents

Line Dictionary to Single

This question already has an answer here: How to merge two dictionaries in a single expression? 48 answers 假设你要求多个字典(不是多行字典)到一个字典。 a = {1: 1, 2:2} b = {2:2, 3:3} c = {2:3} {**a, **b, **c} Out: {1: 1, 2: 3, 3: 3} Assuming that your initial data is actually a list of dictionaries and your keys are unique accross all your dictionaries I would use something like - exampl

线字典到单

这个问题在这里已经有了答案: 如何在单个表达式中合并两个字典? 48个答案 假设你要求多个字典(不是多行字典)到一个字典。 a = {1: 1, 2:2} b = {2:2, 3:3} c = {2:3} {**a, **b, **c} Out: {1: 1, 2: 3, 3: 3} 假设你的初始数据实际上是一个字典列表,并且你的密钥在所有的字典中都是唯一的,我会使用类似 - example = [{'a':1}, {'b':2}, {'c':3}] objOut = {} for d in example: for k,v in d.iteritems():

Merge a dict in Python using 1 dict as base

This question already has an answer here: How to merge two dictionaries in a single expression? 48 answers "Pythonicness" is a hard measure to assess, but here is my take on it: def merge_dicts(base_dict, other_dict): """ Merge two dicts Ensure that the base_dict remains as is and overwrite info from other_dict """ if other_dict is None: return base_dict

以1字典为基础合并Python中的字典

这个问题在这里已经有了答案: 如何在单个表达式中合并两个字典? 48个答案 “Pythonicness”是一个难以评估的措施,但这是我的承诺: def merge_dicts(base_dict, other_dict): """ Merge two dicts Ensure that the base_dict remains as is and overwrite info from other_dict """ if other_dict is None: return base_dict t = type(base_dict) if type(other_dict) != t: rai