《利用python進行資料分析》 第7章 資料轉換這一節中提到一個例子(有部分刪減),如下:
data=https://bbs.csdn.net/topics/DataFrame({'food':['bacon','pulled pork','honey ham'],'ounce':[4,3,12]})
meat_to_animal={'bacon':'pig','pulled pork':'cow','honey ham':'pig'}
作者的意思是將肉來自于哪種動物追加上去,于是給出了:
data['animal']=data['food'].map(lambda x:meat_to_animal[x]);
map的用法是 map(function,iterable ....),一般的做法是:
a = [('a',1),('b',2),('c',3),('d',4)]
a_1 = list(map(lambda x:x[0],a))
我不明白的地方是:
1、map的用法
map(lambda x:meat_to_animal[x]) 為啥是meat_to_animal[x], 難道不應該是 map(lambda x:x.values,meat_to_animal ) // 將字典中的values進行傳遞。
2、meat_to_animal[x] 不明白這種用法
本人小白,剛學不久,請各位大佬不吝賜教。
再次感謝各位。
uj5u.com熱心網友回復:
剛剛搜索了一下,別人的用法是:a = dict(addr='china', name='samy')
>>> map(lambda x: x.upper(), a.values())
['CHINA', 'SAMY']
但我執行的時候,出錯了:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/96564.html
上一篇:求助解決Turtle畫圖問題
下一篇:python入門
