亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在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)]我需要輸出是一個鍵列表,其中第二個元素從大到?。簅utput: [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條經驗 獲得超13個贊

我只是想添加另一種(但類似)方法,讓您在找到排名后訪問元組列表。


最后得到一個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 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號