iT邦幫忙

0

python dict

大家好:
小弟剛進入python的世界,以下資料為每個購買品項(key),分別出現在哪幾筆帳單(value)中,想請教如何把每個帳單的購買物品,由以上的格式依照子集方式列出~
目前資料:
https://ithelp.ithome.com.tw/upload/images/20191003/20116986hPfmjQJReY.png

目標長相:
https://ithelp.ithome.com.tw/upload/images/20191003/201169869TZseiIcyW.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
ccutmis
iT邦高手 2 級 ‧ 2019-10-03 15:05:58
最佳解答

感覺這個跟早上問另一個Dict問題的好相似,是學校作業嗎... /images/emoticon/emoticon04.gif

old_dict={
    '1':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001},
    '2':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003},
    '3':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001},
    '4':{'1':1001,'2':1002,'3':1001,'4':1001,'5':1002},
    '5':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001},
    '6':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1231,'7':1330,'8':1231}
}
new_dict={}

for rowNum in range(0,len(old_dict)):
    tmp_dict={}
    curr_dict=old_dict[str(rowNum+1)]
    print(curr_dict)
    for i in range(0,len(curr_dict)):
        tmp_dict.setdefault(curr_dict[str(i+1)],[]).append(str(i+1))
    new_dict[str(rowNum+1)]=tmp_dict

print('原始DICT物件:')
for row in old_dict:
    print(row,type(old_dict[row]),len(old_dict[row]),old_dict[row])

print('\n修改DICT物件:')    
for row in new_dict:
    print(row,type(new_dict[row]),len(new_dict[row]),new_dict[row])

輸出結果:

原始DICT物件:
1 <class 'dict'> 7 {'1': 1001, '2': 1002, '3': 1003, '4': 1001, '5': 1003, '6': 1005, '7': 1001}
2 <class 'dict'> 5 {'1': 1001, '2': 1002, '3': 1003, '4': 1001, '5': 1003}
3 <class 'dict'> 7 {'1': 1001, '2': 1002, '3': 1003, '4': 1001, '5': 1003, '6': 1005, '7': 1001}
4 <class 'dict'> 5 {'1': 1001, '2': 1002, '3': 1001, '4': 1001, '5': 1002}
5 <class 'dict'> 7 {'1': 1001, '2': 1002, '3': 1003, '4': 1001, '5': 1003, '6': 1005, '7': 1001}
6 <class 'dict'> 8 {'1': 1001, '2': 1002, '3': 1003, '4': 1001, '5': 1003, '6': 1231, '7': 1330, '8': 1231}

修改DICT物件:
1 <class 'dict'> 4 {1001: ['1', '4', '7'], 1002: ['2'], 1003: ['3', '5'], 1005: ['6']}
2 <class 'dict'> 3 {1001: ['1', '4'], 1002: ['2'], 1003: ['3', '5']}
3 <class 'dict'> 4 {1001: ['1', '4', '7'], 1002: ['2'], 1003: ['3', '5'], 1005: ['6']}
4 <class 'dict'> 2 {1001: ['1', '3', '4'], 1002: ['2', '5']}
5 <class 'dict'> 4 {1001: ['1', '4', '7'], 1002: ['2'], 1003: ['3', '5'], 1005: ['6']}
6 <class 'dict'> 5 {1001: ['1', '4'], 1002: ['2'], 1003: ['3', '5'], 1231: ['6', '8'], 1330: ['7']}

tmp_dict_arr=[
['Key','Type','Size','Value'],
[1,'dict',7,{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001}],
[2,'dict',5,{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003}],
[3,'dict',7,{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001}],
[4,'dict',5,{'1':1001,'2':1002,'3':1001,'4':1001,'5':1002}],
[5,'dict',7,{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001}],
[6,'dict',5,{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1231,'7':1330,'8':1231}]
]

print('Key','Type','Size','Value')
for rowNum in range(1,len(tmp_dict_arr)):
    tmp_dict={}
    curr_dict=tmp_dict_arr[rowNum][3]
    for i in range(1,len(curr_dict)):
        tmp_dict.setdefault(curr_dict[str(i)],[]).append(str(i))
    print(tmp_dict_arr[rowNum][0],tmp_dict_arr[rowNum][1],len(tmp_dict),'{'+str(tmp_dict)+'}')

輸出結果:

Key Type Size Value
1 dict 4 {{1001: ['1', '4'], 1002: ['2'], 1003: ['3', '5'], 1005: ['6']}}
2 dict 3 {{1001: ['1', '4'], 1002: ['2'], 1003: ['3']}}
3 dict 4 {{1001: ['1', '4'], 1002: ['2'], 1003: ['3', '5'], 1005: ['6']}}
4 dict 2 {{1001: ['1', '3', '4'], 1002: ['2']}}
5 dict 4 {{1001: ['1', '4'], 1002: ['2'], 1003: ['3', '5'], 1005: ['6']}}
6 dict 5 {{1001: ['1', '4'], 1002: ['2'], 1003: ['3', '5'], 1231: ['6'], 1330: ['7']}}

你的目標長相 裡的value那個內容結構會有問題,{ } 是字典物件,
裡面的KEY:VALUE用 '1705170060':1,3,5,7 會有問題,
用 '1705170060':[1,3,5,7] 不會有問題,問題原因自己想...


dict 轉 json 不管是存.json或剖析我是覺得還好 主要看用你怎麼處理
舉個栗子...

import json

old_dict={
    '1':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001},
    '2':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003},
    '3':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001},
    '4':{'1':1001,'2':1002,'3':1001,'4':1001,'5':1002},
    '5':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1005,'7':1001},
    '6':{'1':1001,'2':1002,'3':1003,'4':1001,'5':1003,'6':1231,'7':1330,'8':1231}
}

old_json=json.loads(json.dumps(old_dict))
print(old_json["2"])
#印出 {'1':1001,'2':1002,'3':1003,'4':1001,'5':1003}
print(old_json["2"]["1"])
#印出 1001

比較大的問題在於,拿連續數字作dict的key值,這樣用真的不如直接把{....}存到串列了,不過樓主若是新手當作一個練習題也好。dict比較好的用法還是在key:value設有意義的值,舉個例某交易所的api獲得的json格式數據,這個長的就比較正常:

MTSL={"stat":"OK","title":"9102年07月23日某表格","fields":["股票代號","股票名稱","前日餘額","賣出","買進","現券","今日餘額","限額","前日餘額","當日賣出","當日還券","當日調整","當日餘額","次一營業日可限額","備註"],"groups":[{"start":2,"span":6,"title":"融券"},{"start":8,"span":6,"title":"借券賣出"}],"date":"91020723","data":[["0001","05大元灣台","22,000","0","0","0","22,000","381,000,000","254,000","98,000","0","0","352,000","2,436,103",""],["0002","001大元型中","0","0","0","0","0","2,250,000","0","0","0","0","0","7,085","X"],...........}

如果你要取這是幾月幾日的數據可以用 MTSL["date"],要取全市場的當日融券data可以用 MTSL["data"] 再跑廻圈,特定某股票用 MTSL["data"]["0001"] 之類的,這種方式才是比較正常人的用法^^"
看更多先前的回應...收起先前的回應...
小魚 iT邦大師 1 級 ‧ 2019-10-03 19:25:22 檢舉

dict是dictionary的意思嗎?

ccutmis iT邦高手 2 級 ‧ 2019-10-03 19:58:53 檢舉

就是字典沒錯...

不是作業啦哈哈哈,是想把公司的資料由R上面轉移到python上玩玩看,看來我應該要先了解dict & json 格式的要點,非常感謝您的指導與建議。

ccutmis iT邦高手 2 級 ‧ 2019-10-04 10:47:22 檢舉

不客氣^^

1
froce
iT邦大師 1 級 ‧ 2019-10-03 20:10:26

dict是無序的,我個人是不建議你這樣用鍵值來記順序。
你要的東西完全可以這樣表記:

[
    [{}, {}],
    [{}, {}, {}],
    [{}, {}],
]

然後你可以靠enumerate()去取得順序資訊。
為什麼要這樣跟你說呢,因為這樣你以後轉換到JSON可以無痛轉換。

ccutmis iT邦高手 2 級 ‧ 2019-10-03 21:21:30 檢舉

完全同意!

好的,非常感謝您的建議~

我要發表回答

立即登入回答