これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。
HTML ファイル生成日時: 2024/11/02 17:34:23.235 (台灣標準時)
実行してみると以下のようになる。#!/usr/pkg/bin/python3.9 # initialisation of a dictionary fruit_price = { 'apple': 50, 'banana': 10, 'cherry': 100, } # printing a whole dictionary print (fruit_price) # printing an element of a dictionary print ('price of banana =', fruit_price['banana'])
% ./test_dictionary_0.py {'apple': 50, 'banana': 10, 'cherry': 100} price of banana = 10
実行すると、次のようになる。#!/usr/pkg/bin/python3.9 # initialisation of a dictionary fruit_price = {} # printing a whole dictionary print (fruit_price) # adding an element to a dictionary fruit_price['apple'] = 50 # printing a whole dictionary print (fruit_price) # adding one more element to a dictionary fruit_price['banana'] = 10 # printing a whole dictionary print (fruit_price) # adding one more element to a dictionary fruit_price['cherry'] = 100 # printing a whole dictionary print (fruit_price)
% ./test_dictionary_1.py {} {'apple': 50} {'apple': 50, 'banana': 10} {'apple': 50, 'banana': 10, 'cherry': 100}
実行すると次のようになる。#!/usr/pkg/bin/python3.9 # initialisation of a dictionary fruit_price = { 'durian': 200, 'cherry': 100, 'apple': 50, 'grape': 150, 'banana': 10, } # printing a whole dictionary print (fruit_price) # looping dictionary print ('fruit prices 1:') for fruit in fruit_price: print (' ', fruit, ':', fruit_price[fruit]) print ('fruit prices 2:') for fruit in fruit_price.keys (): print (' ', fruit, ':', fruit_price[fruit]) print ('fruit prices 3:') for fruit, price in fruit_price.items (): print (' ', fruit, ':', price)
% ./test_dictionary_2.py {'durian': 200, 'cherry': 100, 'apple': 50, 'grape': 150, 'banana': 10} fruit prices 1: durian : 200 cherry : 100 apple : 50 grape : 150 banana : 10 fruit prices 2: durian : 200 cherry : 100 apple : 50 grape : 150 banana : 10 fruit prices 3: durian : 200 cherry : 100 apple : 50 grape : 150 banana : 10
実行してみる。#!/usr/pkg/bin/python3.9 # initialisation of a dictionary fruit_price = { 'durian': 200, 'cherry': 100, 'apple': 50, 'grape': 150, 'banana': 10, } # printing a whole dictionary print (fruit_price) # looping dictionary (sort by keys) print ('fruit prices 1:') for fruit in sorted (fruit_price): print (' ', fruit, ':', fruit_price[fruit]) print ('fruit prices 2:') for fruit in sorted (fruit_price.keys ()): print (' ', fruit, ':', fruit_price[fruit]) print ('fruit prices 3:') for fruit, price in sorted (fruit_price.items ()): print (' ', fruit, ':', price)
% ./test_dictionary_3.py {'durian': 200, 'cherry': 100, 'apple': 50, 'grape': 150, 'banana': 10} fruit prices 1: apple : 50 banana : 10 cherry : 100 durian : 200 grape : 150 fruit prices 2: apple : 50 banana : 10 cherry : 100 durian : 200 grape : 150 fruit prices 3: apple : 50 banana : 10 cherry : 100 durian : 200 grape : 150
実行してみる。#!/usr/pkg/bin/python3.9 # initialisation of a dictionary fruit_price = { 'durian': 200, 'cherry': 100, 'apple': 50, 'grape': 150, 'banana': 10, } # printing a whole dictionary print (fruit_price) # looping dictionary (sort by values) print ('fruit prices 1:') for fruit, price in sorted (fruit_price.items (), key=lambda x: x[1]): print (' ', fruit, ':', price) print ('fruit prices 2:') for fruit in sorted (fruit_price, key=fruit_price.get): print (' ', fruit, ':', fruit_price[fruit])
% ./test_dictionary_4.py {'durian': 200, 'cherry': 100, 'apple': 50, 'grape': 150, 'banana': 10} fruit prices 1: banana : 10 apple : 50 cherry : 100 grape : 150 durian : 200 fruit prices 2: banana : 10 apple : 50 cherry : 100 grape : 150 durian : 200
実行すると以下のようになる。#!/usr/pkg/bin/python3.9 # initialisation of a dictionary of dictionary data = { 'Taiwan': {'capital': 'Taipei', 'climate': 'tropical', 'currency': 'NT'}, 'Japan': {'capital': 'Tokyo', 'climate': 'temperate', 'currency': 'Yen'}, 'Korea': {'capital': 'Seoul', 'climate': 'temperate', 'currency': 'Won'}, 'Vietnam': {'capital': 'Hanoi', 'climate': 'tropical', 'currency': 'Dong'}, 'Thailand': {'capital': 'Bangkok', 'climate': 'tropical', 'currency': 'Baht'}, 'Malaysia': {'capital': 'Kuala Lumpur', 'climate': 'tropical', 'currency': 'Ringgit'}, 'Singapore': {'capital': 'Singapore', 'climate': 'tropical', 'currency': 'Dollar'}, 'Indonesia': {'capital': 'Jakarta', 'climate': 'tropical', 'currency': 'Rupiah'}, } # printing an element of a dictionary print ("Capital of Malaysia =", data['Malaysia']['capital']) print ("Currency of Thailand =", data['Thailand']['currency'])
% ./test_dictionary_5.py Capital of Malaysia = Kuala Lumpur Currency of Thailand = Baht
実行すると次のようになる。#!/usr/pkg/bin/python3.9 # initialisation of a dictionary of dictionary data = {} data['Taiwan'] = {} data['Taiwan']['capital'] = 'Taipei' data['Taiwan']['climate'] = 'tropical' data['Taiwan']['currency'] = 'NT' data['Japan'] = {} data['Japan']['capital'] = 'Tokyo' data['Japan']['climate'] = 'temperate' data['Japan']['currency'] = 'Yen' # printing a dictionary print ("data['Taiwan'] =", data['Taiwan']) print ("data['Japan'] =", data['Japan']) print ("data['Japan']['climate'] =", data['Japan']['climate'])
% ./test_dictionary_6.py data['Taiwan'] = {'capital': 'Taipei', 'climate': 'tropical', 'currency': 'NT'} data['Japan'] = {'capital': 'Tokyo', 'climate': 'temperate', 'currency': 'Yen'} data['Japan']['climate'] = temperate