![](/img/trans.png)
[英]How can I create column having key name from dictionary keys in same dataframe?
[英]How do I create lists out of dictionary, having same name as dictionary keys and same size as value of that key?
在Python中,假設有一個名為fruits的字典:
fruits={
"apple":5,
"orange":7,
"mango":9
}
在閱讀字典項時,它應創建3個與字典鍵名稱相同的列表。 蘋果,橙和芒果,論文列表的大小分別應為5、7和9。 這些列表的元素應由用戶通過控制台給出。
為此找到了解決方案-
fruit = {
"apple": 5,
"banana" : 10
}
for key in fruit.items():
key = list(fruit.get(key)*[None])
這將滿足我的需求。
d = { "apple":5, "orange":7, "mango":9 }
for k in d:
temp = []
for k2 in range(0,d[k]):
print "enter the next "+str(k)+":"
temp.append(raw_input())
exec(str(k)+"="+str(temp))
但是不要。 如何評估包含等號的字符串?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.