Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2024/11/02 17:34:23.235 (台灣標準時)

Python の dictionary の使い方

dictionary の簡単な使い方


#!/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


Frequently accessed files

  1. Computer___Python/20220518_0.html
  2. Computer___Network/20230726_00.html
  3. Computer___Network/20230516_00.html
  4. Misc___Taiwan/20240207_00.html
  5. Computer___FreeBSD/20220621_0.html
  6. Computer___Python/20220715_0.html
  7. Food___Taiwan/20220429_0.html
  8. Computer___Network/20230508_00.html
  9. Computer___NetBSD/20220817_3.html
  10. Computer___Python/20220410_0.html
  11. Computer___Network/20240416_00.html
  12. Computer___Debian/20210223_1.html
  13. Computer___Network/20240130_00.html
  14. Computer___Python/20210124_0.html
  15. Computer___NetBSD/20230119_00.html
  16. Computer___Python/20221013_0.html
  17. Computer___NetBSD/20220428_0.html
  18. Computer___NetBSD/20220818_1.html
  19. Science___Math/20220420_0.html
  20. Computer___NetBSD/20240101_02.html
  21. Computer___TeX/20230503_00.html
  22. Computer___NetBSD/20230515_00.html
  23. Computer___NetBSD/20220808_0.html
  24. Science___Astronomy/20220503_0.html
  25. Computer___NetBSD/20210127_0.html
  26. Computer___Python/20240101_00.html
  27. Computer___Network/20220413_1.html
  28. Computer___NetBSD/20210204_0.html
  29. Computer___Python/20220816_1.html
  30. Travel___Taiwan/20220809_2.html


HTML file generated by Kinoshita Daisuke.