ディクショナリとセット

投稿者: | 2023-02-18

目次

辞書の作り方6種

a = { 'one': 1, 'two': 2, 'three': 3 }

b = dict( one=1, two=2, three=3 ) 

c = dict( zip( ['one', 'two', 'three'], [1, 2, 3] ) )

d = dict( [ ('two', 2), ('one', 1), ('three', 3) ] )

e = dict( {'three':3, 'one': 1, 'two': 2} )

a == b == c == d == e
>> True

ディクショナリ内包表記

DIAL_CODES = [
    (86, 'China'),
    (91, 'India'),
    (1, 'United States'),
    (62, 'Indonesia'),
    (55, 'Brazil'),
    (92, 'Pakistan'),
    (880, 'Bangladesh'),
    (234, 'Nigeria'),
    (7, 'Russia'),
    (81, 'Japan'),
]

country_code = {country: code for code, country in DIAL_CODES}
country_code
{code: country.upper() for country, code in country_code.items() \
    if code < 66}