2 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以使用列表理解:
listOfDicts = [{k:v for k,v in dictionary.items() if k%10==i} for i in range(10)]
或循環(huán):
listOfDicts = [ dict() for _ in range(10) ]
for key,value in dictionary.items():
listOfDicts[key%10].update({key:value})

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果要根據(jù)鍵將字典拆分為兩個(gè)列表。我認(rèn)為以下可以工作
dict #The dictionary returned from function crairB
a = [] #List to save keys and values for key%10==0
b = [] #List for key%10!=0
for x in dict:
if x%10==0:
a.append([x,dict[x]]) #Here I have saved both keys and values in the list, you can edit according to your need.
else:
b.append([x,dict[x]])
編輯:
如果您必須將數(shù)據(jù)拆分為行等于鍵 % 10 的二維列表,那么我認(rèn)為以下方法可以工作。
list = [[] for _ in range(10)]
for x in dict:
i = x%10
list[i].append([x,dict[x]])
添加回答
舉報(bào)