Reference) I made a Python text> 2.3.4 Dictionary type http://qiita.com/KatsunoriNakamura/items/b465b0cf05b1b7fd4975 http://www.k-techlabo.org/www_python/python_main.pdf#page=20
dic = {'apple':'Apple','orange':'Mandarin orange','lemon':'Lemon'}
dicOut=dic['apple']
print('-----------------------------------------------------')
print(type('apple'),'apple')
print(type(dic) ,dic)
print(type(dicOut),dicOut)
#<class 'str'> apple
#<class 'dict'> {'apple': 'Apple', 'orange': 'Mandarin orange', 'lemon': 'Lemon'}
#<class 'str'>Apple
dic = {'apple':50,'orange':60,'lemon': 70}
list = dic.values()
gokei= sum(list)
print('-----------------------------------------------------')
print(type(list),list)
print(type(gokei),gokei)
#<class 'dict_values'> dict_values([50, 60, 70])
#<class 'int'> 180
(Reference) I have defined list with a variable name of list in Python. http://qiita.com/k-nakamura/items/e43338e0007d79316c02
investigating
Create dictionary type from Listing 1 and Listing 2
def list2dic(lst1,lst2):
n=len(lst1)
i=0
dict={}
while i<n:
dict[lst1[i]]=lst2[i]
i += 1
else:
return dict
lst1= ['apple' ,'orange', 'lemon']
lst2= ['Apple', 'Mandarin orange', 'Lemon']
lst12=list2dic(lst1,lst2)
print(type(lst12),lst12)
#<class 'dict'> {'apple': 'Apple', 'orange': 'Mandarin orange', 'lemon': 'Lemon'}
(Reference) Changes to the built-in dict type If a list of values is absolutely necessary, cast the returned dict object. https://www.ibm.com/developerworks/jp/linux/library/l-python3-1/
myDict = {'apple':50,'orange':60,'lemon': 70}
myList = myDict.values()
myCast = list(myList)
myGokei= sum(myList)
print('-----------------------------------------------------')
print(type(myList),myList)
print(type(myCast),myCast)
print(type(myGokei),myGokei)
# <class 'dict_values'> dict_values([50, 60, 70])
# <class 'list'> [50, 60, 70]
# <class 'int'> 180
Recommended Posts