有一個有趣的問題,即存在以下格式的詞典:清單DF(不等長元組)
test = {'A': [(1,2),(3,4)],
'B': [(1,2),(5,6),(7,8)]}
對於字典中的每個關鍵,他們(第一次總是)共享一些值,但是每個列表中都有不同數量的元組。
最終,我的目標是代表了該格式的數據:
1 3 5 7
A 2 4 - -
B 2 - 6 8
有沒有在字典中的值,這些共同的元素轉換爲數據幀的一列一個聰明的辦法?
我想拿到名單的長度相等的是以下幾點:
#get all the unique first elements of the dictionary
unique=[]
for i in test.values():
for j in i:
unique.append(j[0])
unique = set(unique)
values_of_A = test['A']
#I thought this would loop through each tuple in the list and check if its
#first value is in the list of unique elements; otherwise, it will print
#0. However, it comes up with a blank list...
full_list = [0 for i, v in enumerate(values_of_A) if v[0] not in unique]
感謝一如既往!
你能解釋一下多一點關於你如何想'test'的數據進行轉換?目前還不清楚生產產出的邏輯是什麼。 – ASGM
是的 - 只是試圖掃描所有的元組,並將所有元組中的第一個元素作爲列標題放在第一個位置,然後將字典鍵作爲第一個列,並且表的值將是與比賽。不知道這是否有幫助,但也許上面的圖片有助於用詞。如果沒有,請告訴我。無論哪種方式,juanpa的解決方案都是完美的! – user6142489
很高興聽到@ juanpa.arrivillaga的解決方案奏效 - 但請注意,您指定的輸出與其不匹配。 (這不是對我提出的答案的批評)。 – ASGM