2015-10-15 131 views
1

我試圖從字典中提取某些值並以特定方式顯示它。我會告訴你我的代碼示例如下:Python - 以特定方式從字典中提取信息

dict = [{'Titel': 'Rush', 'Name': 'Floris', 'Starttime': '20:30', 'Email': '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Cake', 'Code': 'ABC123'}, 
{'Titel': 'Rush', 'Voornaam': 'Jaron', 'Starttime': '20:30', 'Email': '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Pie', 'Code': 'XYZ123'}, 
{'Titel': 'Underneath', 'Name': 'Klaas', 'Starttime': '04:00', 'Email': '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Klassie', 'Code': 'fbhwuq8674'}] 

這是我的字典。我想作爲輸出是:

Titel, Starttime, 
Surname, Name, Email 

因此,這將是這樣的:

Rush, 20:30, 
Cake, Floris, [email protected] 
Pie, Jaron, [email protected] 

Underneath, 04:00, 
Klassie, Klaas, [email protected] 
+0

您是否知道手前的字典鍵\ – The6thSense

+0

是的,它們都是在CSV文件中定義的。他們都遵循相同的格式。字典的鍵是:'titel','name','starttime','email','supplier','surname','code' – Cake

+0

你是否還需要titel組合 – The6thSense

回答

3

我用VKS的代碼來構建它感謝VKS:P

使用groupylist comprehensionsting formatting

代碼:

from itertools import groupby 
k = [{'Titel': 'Rush', 'Name': 'Floris', 'Starttime': '20:30', 'Email':  '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Cake', 'Code': 'ABC123'}, 
{'Titel': 'Rush', 'Voornaam': 'Jaron', 'Starttime': '20:30', 'Email': '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Pie', 'Code': 'XYZ123'}, 
{'Titel': 'Underneath', 'Name': 'Klaas', 'Starttime': '04:00', 'Email': '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Klassie', 'Code': 'fbhwuq8674'}] 

lst=[(i["Titel"],i["Starttime"],i["Surname"],i.get("Name","None"), i["Email"]) for i in k] 
lst.sort(key=lambda x:(x[0],x[1])) 
for key,groups in groupby(lst,key=lambda x:(x[0],x[1])): 
    print "{}\t{}".format(key[0],key[1]) 
    for value in groups: 
     print "{}\t{}\t{}".format(value[2],value[3],value[4]) 
    print "" 

輸出:

Rush 20:30 
Cake Floris [email protected] 
Pie  None [email protected] 

Underneath  04:00 
Klassie Klaas [email protected] 
+0

.. ..是...華麗!正是我需要的!非常感謝Vignesh。 – Cake

+0

@Cake高興地幫忙,但看起來它groupby和字符串格式化它將是有用的 – The6thSense

+0

你的意思是我應該看看模塊,以便我可以使用它的未來努力?如果是這樣,我當然會! – Cake

1
k = [{'Titel': 'Rush', 'Name': 'Floris', 'Starttime': '20:30', 'Email':  '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Cake', 'Code': 'ABC123'}, 
{'Titel': 'Rush', 'Voornaam': 'Jaron', 'Starttime': '20:30', 'Email': '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Pie', 'Code': 'XYZ123'}, 
{'Titel': 'Underneath', 'Name': 'Klaas', 'Starttime': '04:00', 'Email': '[email protected]', 'Supplier': 'RTL8', 'Surname': 'Klassie', 'Code': 'fbhwuq8674'}] 

print [(i["Titel"],i["Starttime"],i["Surname"], i["Email"]) for i in k] 

你可以使用這個,你會得到你想要的所有信息的tuple

+0

你的代碼看起來像Vignesh? – Cake

1
def print_my_d(d): 

    print(d[0]['Titel'], d[0]['Starttime']) 
    for l in d[:2]: 
     print(l['Surname'], l['Name'], l['Email']) 
    print() 
    print(d[2]['Titel'],d[2]['Starttime']) 
    print("{}, {}, {}".format(d[2]['Surname'],d[2]['Name'], d[2]['Email'])) 


print_my_d(d) 

Rush 20:30 
Cake Floris [email protected] 
Pie Jaron [email protected] 

Underneath 04:00 
Klassie, Klaas, [email protected] 
+0

比你回覆@LetzerWille。你非常接近,但不是我想要的。我希望Titel + Time只能打印一次,並且所有與該字體相匹配的值在字典中將被打印。 – Cake

+0

@Cake。改變,以反映你所需要的 – LetzerWille

+0

@LetzerWille仍然有些東西丟失 – The6thSense