为了账号安全,请及时绑定邮箱和手机立即绑定

在Python中通过自定义规则从字典列表中创建有序列表

在Python中通过自定义规则从字典列表中创建有序列表

子衿沉夜 2023-09-26 16:15:10
编辑:我需要更改规则。我有一个这样的字典列表:A=[{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)}]首先,我需要计算每个字典中的元素数量。其次,将每个列表中的一个元素的最后两个元素相乘(因为在每个列表中最后两个元素是相同的),如下所示:M=[(0,2,2) ,(1,3,1) , (2,4,1) , (3,4,3)]第一个元素是键,第二个元素是最后两个元素的乘积,第三个元素是该列表中的元素数量。(我不需要打印这部分)然后为每个列表计算乘法/选项,如下所示:B=[(0,2/2), (1,3/1), (2,4/1), (3,4/3)]我需要输出是一个键列表,其中第二个元素从大到小:output: [2, 1, 3, 0]有谁有任何想法可以提供帮助吗?
查看完整描述

3 回答

?
慕田峪4524236

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]]


查看完整回答
反对 回复 2023-09-26
?
慕的地8271018

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]]


查看完整回答
反对 回复 2023-09-26
?
红颜莎娜

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 ..


查看完整回答
反对 回复 2023-09-26
  • 3 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信