今天面試某公司時遇到的乙個題目
想想其實很簡單的,但是操作起來還是有點難度滴,回來驗證了一下自己的**,沒問題
table =[['month
', '
day', '
itemname
', '
numberofitems
', '
price
', '
retailprice
', '
tax', '
code
', '
suppliername'],
['01
', '
01', '
corporate creditcard usage
', '', '', '
10,364
', '
1,036
', '
00140
', '
koreamcdgangnam'],
['01
', '
01', '
corporate creditcard usage
', '', '', '
10,999
', '
1,101
', '
00406
', '
spicychickenbundang'],
['01
', '
01', '
corporate creditcard usage
', '', '', '
1,818
', '
182', '
00237
', '
porkbbqitaewon']
]second_dict =dict()
first_dict=dict()
keys =table.pop(0)
for i in range(len(table)):
for y in range(len(keys)):
second_dict[keys[y]]=table[i][y]
first_dict[str(i)]=str(second_dict)
print(first_dict)
**結果是
",'1': "",
'2': ""}
需要特別注意的點其實就是python中字典的value其實是乙個引用~~~~~~~~~~~
因此在最後儲存為大字典時
first_dict[str(i)]=str(second_dict)
不轉一下格式的話,因為value是乙個引用,它會被後面的資料覆蓋,只會得到一組資料,要注意了哦~~~~
python 列表轉字典
雖然元組 列表不可以直接轉化為字典,但下面的確是可行的,因為經常用python從資料庫中讀出的是元組形式的資料。cc 1 1 2 jiqw 3 1372 dict cc 12 3雖然列表不可轉字典,然而,兩個列表,就可以 需要zip 如下 a 1,2,3 b 4,5,6 dict a,b trace...
python 列表 字典轉json
一 dictionary 轉為json 將dict轉為json,這裡利用包json import json aitem aitem id 2203 aitem title title aitem subtitle sub title bitem bitem id 2842 bitem title b...
python 列表 字典轉json
一 dictionary 轉為json 將dict轉為json,這裡利用包json import json aitem aitem id 2203 aitem title title aitem subtitle sub title bitem bitem id 2842 bitem title b...