3 回答
TA贡献1875条经验 获得超5个赞
根据编辑的新解决方案
t=[{0:[(0,0,2,1),(0,1,2,1)]},{1:[(0,1,1,3)]},{2:[(1,2,2,2)]},{3:[(0,0,1,4),(0,1,1,4),(1,0,1,4)]}]
step_one=[]
for i in t:
key_list=list(i.keys())
value_list=list(i.values())
first=key_list[0]
second=value_list[0][0][-1]*value_list[0][0][-2]
third=len(value_list[0])
step_one.append((first,second,third))
print(step_one) # [(0, 2, 2), (1, 3, 1), (2, 4, 1), (3, 4, 3)]
step_two=[(i,j/k) for i,j,k in step_one]
step_three=[i for i,j in sorted(step_two,key=lambda x:-x[1])]
print(step_three) # [2, 1, 0, 3]
这行得通吗?使用key参数来执行排序
t=[{0:[(0,0,1,3),(0,1,1,3)]},{1:[(0,1,1,3)]},{3:[(0,0,1,3),(0,1,1,3),(1,0,4,1)]}]
sorted_t=sorted(t,key=lambda x:len(list(x.values())[0]))
result=[list(i.keys())[0] for i in sorted_t]
print(result) # [1, 0, 3]
分解
sorted_t=sorted(t,key=lambda x:len(list(x.values())[0]))
这将根据每个元素的“字典中第一个值的长度”对列表进行排序
结果 :[{1: [(0, 1, 1, 3)]}, {0: [(0, 0, 1, 3), (0, 1, 1, 3)]}, {3: [(0, 0, 1, 3), (0, 1, 1, 3), (1, 0, 4, 1)]}]
然后迭代sorted_t并获取“排序列表中每个字典的键”,[0]用于从“键列表”中索引元素。否则你将以这样的列表结束[[1], [0], [3]]
TA贡献1796条经验 获得超4个赞
如果您的词典将有多个键,那么您可以使用此方法
from collections import defaultdict
A = [{0: [(0, 0, 1, 3), (0, 1, 1, 3)], 2: [(0, 1, 0, 3)]},
{1: [(0, 1, 1, 3)], 4: [(0, 1, 3, 3), (1, 1, 1, 2), (1, 1, 4, 1)]},
{3: [(0, 0, 1, 3), (0, 1, 1, 3), (1, 0, 4, 1)]}
]
dd = defaultdict(list)
for dictionary in A:
for k, v in dictionary.items():
dd[len(v)].append(k)
result = [k[1] for k in sorted(dd.items(), key=lambda x: x[0])]
print(result)
输出:
[[2, 1], [0], [4, 3]]
TA贡献1842条经验 获得超12个赞
我只是想添加另一种(但类似)方法,让您在找到排名后访问元组列表。
最后得到一个keys按其排名排序的列表,即[1,0,3]您丢失了元组列表所在位置的信息。
要恢复元组列表,您需要A再次迭代并搜索keys,但如果A包含使用相同键的多个字典,则可能会出现问题。
因此,以下方法保留了index_of_A在 中找到具有某个等级的每个键的A。
# I've added missing brackets in the element of A, {3:[..]}
A=[{0:[(0,0,1,3),(0,1,1,3)]},{1:[(0,1,1,3)]},{3:[(0,0,1,3),(0,1,1,3),(1,0,4,1)]}]
# A is a list of dicts
# Each dict has only one (numerical) key,
# -> resolving to a list of tuples
# run over A and create a new Rank dict, where keys are
# the keys from the dicts in A, and values are tuples of
# the ranks and and index in A for which to locate the lists again.
ranks = {}
for index_of_A, dictionary in enumerate(A):
for key, list_of_tuples in dictionary.items():
ranks[key] = (len(list_of_tuples), index_of_A)
# https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value
sorted_ranks = {k: v for k, v in sorted(ranks.items(), key=lambda item: item[1])}
# Get Ranks, key and the list of tuples, and perform some work on them..
for key_of_A, (rank, index_of_A) in sorted_ranks.items():
print("Do some work on: Rank %d | Key %d | List %s" % (rank, key_of_A, str(A[index_of_A])))
do_some_work(index_of_A)
# The Keys sorted by their ranks
print ("keys sorted by rank: " + str([key for key in sorted_ranks]))
输出:
Rank 1 | Key 1 | List {1: [(0, 1, 1, 3)]}
Rank 2 | Key 0 | List {0: [(0, 0, 1, 3), (0, 1, 1, 3)]}
Rank 3 | Key 3 | List {3: [(0, 0, 1, 3), (0, 1, 1, 3), (1, 0, 4, 1)]}
keys sorted by rank: [1, 0, 3]
编辑:注释中要求显示如何对数据进行一些工作,因此我添加了下面的函数,并在上面的工作循环中添加了对它的调用。
# Added a function for doing some work as requesed in comments
def do_some_work(index_of_A):
# A is a global variable
a_dict = A.pop(index_of_A)
# do whatever with the data ..
添加回答
举报