-2

私はフォローリストを持っています

lines = 
    [['client', 'name', 'phone', 'address', 'email'],

     ['client2', 'name2', 'phone2', 'address2', 'email2'],

     ['client3', 'name3', 'phone3', 'address3', 'email3']]

リストをdicのリストに変えることが可能かどうか知りたいです。

お気に入り:

lines = 
    [{Client: client, Name:name, Phone:phone, Address:address, Email:email},

     {Client: client2, Name:name2, Phone:phone2, Address:address2, Email:email2},

     {Client: client3, Name:name3, Phone:phone3, Address:address3, Email:email3}]
4

5 に答える 5

0

スタート:

lines = [client, name, phone, address, email,
         client2, name2, phone2, address2, email2,
         client3, name3, phone3, address3, email3]

linesD = {entry: entry for entry in lines}
于 2013-10-04T15:25:51.683 に答える
0

あなたのデータはきれいにフォーマットされていないので、少し変更します...

import pandas as pd
...
lines = [
    [client, name, phone, address, email],
    [client2, name2, phone2, address2, email2],
    [client3, name3, phone3, address3, email3]]
fields=["Client", "Name", "Phone", "Address", "Email"]
s = pd.DataFrame(lines, columns=fields)
# choose one u want:
print(list(s.T.to_dict().values())
print(s.to_dict(outtype='list'))
print(s.T.to_dict())
于 2013-10-04T16:02:11.860 に答える
0

それらすべてを使って 1 つの辞書を作成することはできませんが、辞書のリストを作成することはできます。

lines = [
    client, name, phone, address, email,
    client2, name2, phone2, address2, email2,
    client3, name3, phone3, address3, email3,
]

keys = ('Client', 'Name', 'Phone', 'Address', 'Email')

clients = [dict(zip(keys, lines[i:i+5])) for i in xrange(0, len(lines), 5)]
于 2013-10-04T15:48:00.450 に答える