Sort list by attribute of list

This question already has an answer here: How do I sort a list of dictionaries by values of the dictionary in Python? 17 answers Use a lambda expression , lambda expression would get the dictionary as parameter, and then you can return back the name element from the dictionary - lst.sort(key=lambda x: x['name']) Also, please do not use the list as name of the variable, it will overwrite th

按列表的属性排序列表

这个问题在这里已经有了答案: 如何根据Python中字典的值对字典列表进行排序? 17个答案 使用lambda表达式,lambda表达式将得到字典作为参数,然后您可以返回字典中的name元素 - lst.sort(key=lambda x: x['name']) 另外,请不要使用list作为变量的名称,它会覆盖内置list函数,这可能会在尝试使用list(..)函数时导致问题。

How can I sort a list of dictionaries by a value in the dictionary?

Possible Duplicate: In Python how do I sort a list of dictionaries by values of the dictionary? I'm writing a Python 3.2 app and I have a list of dictionaries containing the following: teamlist = [{ "name":"Bears", "wins":10, "losses":3, "rating":75.00 }, { "name":"Chargers", "wins":4, "losses":8, "rating":46.55 }, { "name":"Dolphins", "wins":3, "losses":9, "ratin

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

可能重复: 在Python中,我如何根据字典的值对字典列表进行排序? 我正在编写一个Python 3.2应用程序,我有一个包含以下内容的字典列表: teamlist = [{ "name":"Bears", "wins":10, "losses":3, "rating":75.00 }, { "name":"Chargers", "wins":4, "losses":8, "rating":46.55 }, { "name":"Dolphins", "wins":3, "losses":9, "rating":41.75 }, { "name":"Patriots", "wins":9, "loss

Sorting associative arrays in Python

This question already has an answer here: How do I sort a list of dictionaries by values of the dictionary in Python? 17 answers Use the sorted function's key parameter: sorted(people, key=lambda dct: dct['name']) There is an excellent Sorting HOWTO which explains how this works. >>> people = [ {'name' : 'Bob', 'number' : '123'}, {'name' : 'Bill', 'number' : '234'},

在Python中排序关联数组

这个问题在这里已经有了答案: 如何根据Python中字典的值对字典列表进行排序? 17个答案 使用sorted函数的key参数: sorted(people, key=lambda dct: dct['name']) 有一个很好的排序HOWTO解释了这是如何工作的。 >>> people = [ {'name' : 'Bob', 'number' : '123'}, {'name' : 'Bill', 'number' : '234'}, {'name' : 'Dave', 'number' : '567'}, ] >>> sorted(people, key=lambda

How to sort a Python dictionary by value?

Possible Duplicate: In Python how do I sort a list of dictionaries by values of the dictionary? Sorting Python dictionary based on nested dictionary values I have dictionary of the form as mentioned below: a_dict = { 'e': (1, 100), 'a': (2, 3) } I am unable to sort it by the second element of the tuple. The resultant dictionary will appear as below: a_dict = { 'a': (2, 3), 'e': (1, 100)

如何按值排序Python字典?

可能重复: 在Python中,我如何根据字典的值对字典列表进行排序? 基于嵌套字典值对Python字典进行排序 我有如下所述的表格字典: a_dict = { 'e': (1, 100), 'a': (2, 3) } 我无法通过元组的第二个元素对它进行排序。 结果字典将显示如下: a_dict = { 'a': (2, 3), 'e': (1, 100) } 字典不能按照这种方式排序,但可以对其内容进行排序: sorted(a_dict.items(), key=lambda (k, (v1, v2)): v2) sorted(a_dict.items

How to use a custom comparison function in Python 3?

In Python 2.x , I could pass custom function to sorted and .sort functions >>> x=['kar','htar','har','ar'] >>> >>> sorted(x) ['ar', 'har', 'htar', 'kar'] >>> >>> sorted(x,cmp=customsort) ['kar', 'htar', 'har', 'ar'] Because, in My language, consonents are comes with this order "k","kh",....,"ht",..."h",...,"a" But In Python 3.x , looks like I coul

如何在Python 3中使用自定义比较函数?

在Python 2.x中 ,我可以将自定义函数传递给排序函数和.sort函数 >>> x=['kar','htar','har','ar'] >>> >>> sorted(x) ['ar', 'har', 'htar', 'kar'] >>> >>> sorted(x,cmp=customsort) ['kar', 'htar', 'har', 'ar'] 因为,在我的语言中,配件都是这个订单 "k","kh",....,"ht",..."h",...,"a" 但在Python 3.x中 ,看起来像我无法通过cmp关键字 >>> sorted(x,cmp=cu

Sort a list by multiple attributes?

I have a list of lists: [[12, 'tall', 'blue', 1], [2, 'short', 'red', 9], [4, 'tall', 'blue', 13]] If I wanted to sort by one element, say the tall/short element, I could do it via s = sorted(s, key = itemgetter(1)) . If I wanted to sort by both tall/short and colour, I could do the sort twice, once for each element, but is there a quicker way? A key can be a function that returns a tuple:

按多个属性对列表进行排序?

我有一个列表清单: [[12, 'tall', 'blue', 1], [2, 'short', 'red', 9], [4, 'tall', 'blue', 13]] 如果我想按一个元素排序,说高/短元素,我可以通过s = sorted(s, key = itemgetter(1)) 。 如果我想根据高/短和颜色进行排序,我可以进行两次排序,每个元素排序一次,但有没有更快的方法? 一个键可以是一个返回元组的函数: s = sorted(s, key = lambda x: (x[1], x[2])) 或者你可以使用itemgetter (这是更快,并避

How to determine the type of credit card?

For a school assignment I'm trying to determine the type of credit card a user has when they type in their card number. A master card starts with 51-55. Extra zeros in front of that number would be considered valid. I've reviewed other threads on how to integrate this into python and although I could just copy and paste the code I still don't really understand whats going on or how

如何确定信用卡的类型?

对于一个学校任务,我试图确定用户输入卡号时所用的信用卡类型。 主卡以51-55开头。 该号码前面的额外零将被认为是有效的。 我已经回顾了关于如何将它集成到python中的其他线程,尽管我可以复制和粘贴代码,但我仍然不能真正理解正在发生的事情或者他们如何绕过前面的零? 下面是一个Master卡代码的例子: ^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$ 有谁能

How to check if all elements of a list matches a condition?

I have a list consisting of like 20000 lists. I use each list's 3rd element as a flag. I want to do some operations on this list as long as at least one element's flag is 0, it's like: my_list = [["a", "b", 0], ["c", "d", 0], ["e", "f", 0], .....] In the beginning all flags are 0. I use a while loop to check if at least one element's flag is 0: def check(lista): for item

如何检查列表中的所有元素是否与条件匹配?

我有一个包含20000个列表的列表。 我使用每个列表的第三个元素作为标志。 只要至少有一个元素的标志是0,我想在这个列表上做一些操作,就像: my_list = [["a", "b", 0], ["c", "d", 0], ["e", "f", 0], .....] 在开始时,所有标志都是0.我使用while循环来检查至少一个元素的标志是否为0: def check(lista): for item in lista: if item[2] == 0: return True return False 如果check(my_lis

Fastest way to check if a value exist in a list

I'm searching for the fastest way to know if a value exists in a list (a list with millions of values in it) and what its index is? I know that all values in the list are unique like my example. The first method I try is (3.8sec in my real code): a = [4,2,3,1,5,6] if a.count(7) == 1: b=a.index(7) "Do something with variable b" The second method I try is (2x faster:1.9sec on my r

检查列表中是否存在值的最快方法

我正在寻找最快的方法来了解列表中是否存在一个值(一个包含数百万个值的列表)以及它的索引是什么? 我知道列表中的所有值都是独特的,就像我的例子。 我尝试的第一种方法是(在我的真实代码中为3.8秒): a = [4,2,3,1,5,6] if a.count(7) == 1: b=a.index(7) "Do something with variable b" 我尝试的第二种方法是(快两倍:我的真实代码为1.9秒): a = [4,2,3,1,5,6] try: b=a.index(7) except ValueErr

Is there a simple way to delete a list element by value?

a=[1,2,3,4] b=a.index(6) del a[b] print a The above shows the following error: Traceback (most recent call last): File "D:zjm_codea.py", line 6, in <module> b=a.index(6) ValueError: list.index(x): x not in list So I have to do this: a=[1,2,3,4] try: b=a.index(6) del a[b] except: pass print a But is there not a simpler way to do this? To remove an element's first

有没有一种简单的方法来按值删除列表元素?

a=[1,2,3,4] b=a.index(6) del a[b] print a 以上显示了以下错误: Traceback (most recent call last): File "D:zjm_codea.py", line 6, in <module> b=a.index(6) ValueError: list.index(x): x not in list 所以我必须这样做: a=[1,2,3,4] try: b=a.index(6) del a[b] except: pass print a 但是,有没有更简单的方法来做到这一点? 要在列表中删除元素的第一个匹配项,只需使用list.remove