How do I sort a list of dictionaries by values of the dictionary in Python?

I got a list of dictionaries and want that to be sorted by a value of that dictionary. This [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] sorted by name, should become [{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}] It may look cleaner using a key instead a cmp: newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) or as JFSebastian and others suggested, from op

如何根据Python中字典的值对字典列表进行排序?

我收到了一个字典列表,并希望按字典的值进行排序。 这个 [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] 按名称排序,应该成为 [{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}] 它可能看起来更清洁使用一个键,而不是一个cmp: newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 或者像JFSebastian和其他人所建议的那样, from operator import itemgetter newlist = sorted(list_t

comprehensions and functional functions faster than "for loops"?

In terms of performance in Python, is a list-comprehension, or functions like map(), filter() and reduce() faster than a for loop? Why, technically, they "run in a C speed", while "the for loop runs in the python virtual machine speed"?. Suppose that in a game that I'm developing I need to draw complex and huge maps using for loops. This question would be definitely re

理解和功能函数比“for循环”更快?

就Python的性能而言,它是一种列表理解,还是比for循环更快的功能,如map(),filter()和reduce()? 为什么在技术上他们“以C速度运行”,而“for循环以python虚拟机速度运行”? 假设在我正在开发的游戏中,我需要使用for循环绘制复杂而庞大的地图。 这个问题肯定是相关的,因为如果列表理解确实比较快,那么为了避免滞后(尽管代码的视觉复杂性),这将是一个更好的选择。 以下是基于经验的粗略指导和受过教育的猜测。

Django form.save step by step

Let's say I have a form for adding/editing products (with field 'user' being a foreign key to my User) triggered from two separate view functions - add/edit : def product_add(request): userprofile = UserProfile.objects.get(user=request.user) if request.method == 'POST': form = ProductAddForm(request.POST, request.FILES,) if form.is_valid(): form.sa

Django form.save一步一步

假设我有一个用于添加/编辑产品的窗体(使用两个单独的视图函数触发的字段“用户”是我用户的外键) - 添加/编辑: def product_add(request): userprofile = UserProfile.objects.get(user=request.user) if request.method == 'POST': form = ProductAddForm(request.POST, request.FILES,) if form.is_valid(): form.save(user=request.user) else: form = ProductAddForm()

Programmatically interrupting raw

Is there a way to programmatically interrupt Python's raw_input? Specifically, I would like to present a prompt to the user, but also listen on a socket descriptor (using select, for instance) and interrupt the prompt, output something, and redisplay the prompt if data comes in on the socket. The reason for using raw_input rather than simply doing select on sys.stdin is that I would like t

以编程方式中断原始

有没有一种方法来以编程方式中断Python的raw_input? 具体来说,我想向用户提示一个提示,但还要监听一个套接字描述符(例如,使用select)并中断提示,输出内容,并在数据流入套接字时重新显示提示。 使用raw_input而不是简单地在sys.stdin上进行选择的原因是我想使用readline模块为提示提供行编辑功能。 据我所知...“排序”。 raw_input被阻塞,所以我能想到的唯一方法是产生一个子进程/线程来检索输入,然后简单地与线

Can we run multiprocessing Pool in GAE?

Is it possible to run multiprocessing Pool in gae? If yes, how come my code runs fine in my local machine but pops out the following err msg with django remote api: File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py", line 148, in map return self.map_async(func, iterable, chunksize).get() File "/System/Library/Frameworks/Pytho

我们可以在GAE中运行多处理池吗?

是否有可能在gae中运行多处理池? 如果是的话,我的代码如何在我的本地机器上运行良好,但用django remote api弹出下面的错误信息: 文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py”,第148行,在map中返回self.map_async(func,iterable,chunksize).get() 文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/po

Multiprocessing vs Threading Python

I am trying to understand the advantages of multiprocessing over threading. I know that multiprocessing gets around the Global Interpreter Lock, but what other advantages are there, and can threading not do the same thing? The threading module uses threads, the multiprocessing module uses processes. The difference is that threads run in the same memory space, while processes have separate mem

多进程与线程Python

我想了解多线程处理的优点。 我知道多处理技术可以解决Global Interpreter Lock问题,但是还有什么其他的优势,并且可以通过线程处理而不是做同样的事情? threading模块使用线程, multiprocessing模块使用进程。 不同之处在于线程运行在相同的内存空间中,而进程具有单独的内存。 这使得在多进程进程之间共享对象变得更加困难。 由于线程使用相同的内存,因此必须采取预防措施,或者两个线程同时写入同一内​​存。 这是

Calling a function of a module by using its name (a string)

What is the best way to go about calling a function given a string with the function's name in a Python program. For example, let's say that I have a module foo , and I have a string whose contents are "bar" . What is the best way to go about calling foo.bar() ? I need to get the return value of the function, which is why I don't just use eval . I figured out how to do

通过使用其名称调用模块的功能(字符串)

在Python程序中给出带有函数名字符串的函数的最佳方法是什么? 例如,假设我有一个模块foo ,并且我有一个字符串,其内容是"bar" 。 什么是最好的方式去调用foo.bar() ? 我需要获得函数的返回值,这就是为什么我不使用eval 。 我想通过使用eval来定义一个返回该函数调用结果的临时函数的方法,但我希望有一种更优雅的方式来实现这一点。 假设模块foo带有方法bar : import foo method_to_call = getattr(foo,

Static methods in Python?

是否有可能在Python中有静态方法,所以我可以在不初始化类的情况下调用它们,如: ClassName.StaticMethod ( ) Yep, using the staticmethod decorator class MyClass(object): @staticmethod def the_static_method(x): print x MyClass.the_static_method(2) # outputs 2 Note that some code might use the old method of defining a static method, using staticmethod as a function rather than a decor

Python中的静态方法?

是否有可能在Python中有静态方法,所以我可以在不初始化类的情况下调用它们,如: ClassName.StaticMethod ( ) 是的,使用staticmethod装饰器 class MyClass(object): @staticmethod def the_static_method(x): print x MyClass.the_static_method(2) # outputs 2 请注意,某些代码可能使用静态方法的旧方法,使用staticmethod作为函数而不是装饰器。 这应该只用于如果你必须支持古代版本的Python(2.2和2.3

Is there a way to substring a string in Python?

Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string? Maybe like myString[2:end] ? If leaving the second part means 'till the end', if you leave the first part, does it start from the start? >>> x = "Hello World!" >>> x[2:] 'llo World!' >>> x[:2] 'He' >>> x[:-2] 'Hello Worl' >>>

有没有办法在Python中对字符串进行子串处理?

有没有办法在Python中对字符串进行子串处理,以便从第三个字符到字符串的末尾得到一个新的字符串? 也许像myString[2:end] ? 如果离开第二部分意味着'直到结束',如果离开第一部分,它是否从一开始就开始? >>> x = "Hello World!" >>> x[2:] 'llo World!' >>> x[:2] 'He' >>> x[:-2] 'Hello Worl' >>> x[-2:] 'd!' >>> x[2:-2] 'llo Worl' Python将这个

`if

I am new to Ruby. I'm looking to import functions from a module that contains a tool I want to continue using separately. In Python I would simply do this: def a(): ... def b(): ... if __name__ == '__main__': a() b() This allows me to run the program or import it as a module to use a() and/or b() separately. What's the equivalent paradigm in Ruby? From the Ruby I

如果`

我是Ruby的新手。 我正在寻找从包含我想要单独继续使用的工具的模块导入函数。 在Python中,我只需要这样做: def a(): ... def b(): ... if __name__ == '__main__': a() b() 这使我可以运行程序或将其作为模块导入,以单独使用a()和/或b() 。 Ruby中的等效范例是什么? 从我在野外见过的红宝石(授予,不是一吨),这不是标准的Ruby设计模式。 模块和脚本应该保持独立,所以我不会感到惊讶,如果没有