タプルのタプルとして値とキーを指定して辞書を作成する関数をコーディングしようとしています。例えば
long2wide ( ((" apple ","red "),(" banana "," yellow "),(" banana "," green "),
(" apple "," green "),(" cherry "," red "))
, (" fruit ", " colour ") )
戻り値
{'fruit': ['apple', 'banana', 'banana', 'apple', 'cherry'], 'colour': ['red', 'yellow', 'green', 'green', 'red']}
これが私が今持っている(間違った)コードです:
def long2wide(data, headers):
dictionary = {}
for entry in headers:
for i in range(len(data)): #Iterate through each row of data
for j in range(len(data[0])): #Iterate through each column of data
dictionary[(headers)] = data[i][j]
return dictionary
私の現在の出力は {'fruit': 'red', 'color': 'red'} です
:(
誰かがこれをデバッグ/解決するのを手伝ってくれたら本当にありがたいです. ありがとう!!