简体   繁体   中英

Python Tuple Operation - Get Value

I have a tuple that contains province data.

CHOICES = (
        ('', ''),
        ('United States', (
                ('AL', 'Alabama'),
                ('AK', 'Alaska'),
                ('AZ', 'Arizona'),
                ('AR', 'Arkansas'),
                ('CA', 'California'),
                ('CO', 'Colorado'),
                ('CT', 'Connecticut'),
                ('DE', 'Delaware'),
                ('DC', 'District of Columbia'),
                ('FL', 'Florida'),
                ('GA', 'Georgia'),
                ('GU', 'Guam'),
                ('HI', 'Hawaii'),
                ('ID', 'Idaho'),
                ('IL', 'Illinois'),
                ('IN', 'Indiana'),
                ('IA', 'Iowa'),
                ('KS', 'Kansas'),
                ('KY', 'Kentucky'),
                ('LA', 'Louisiana'),
                ('ME', 'Maine'),
                ('MD', 'Maryland'),
                ('MA', 'Massachusetts'),
                ('MI', 'Michigan'),
                ('MN', 'Minnesota'),
                ('MS', 'Mississippi'),
                ('MO', 'Missouri'),
                ('MT', 'Montana'),
                ('NE', 'Nebraska'),
                ('NV', 'Nevada'),
                ('NH', 'New Hampshire'),
                ('NJ', 'New Jersey'),
                ('NM', 'New Mexico'),
                ('NY', 'New York'),
                ('NC', 'North Carolina'),
                ('ND', 'North Dakota'),
                ('OH', 'Ohio'),
                ('OK', 'Oklahoma'),
                ('OR', 'Oregon'),
                ('PA', 'Pennsylvania'),
                ('PR', 'Puerto Rico'),
                ('RI', 'Rhode Island'),
                ('SC', 'South Carolina'),
                ('SD', 'South Dakota'),
                ('TN', 'Tennessee'),
                ('TX', 'Texas'),
                ('UT', 'Utah'),
                ('VT', 'Vermont'),
                ('VA', 'Virginia'),
                ('VI', 'Virgin Islands'),
                ('WA', 'Washington'),
                ('WV', 'West Virginia'),
                ('WI', 'Wisconsin'),
                ('WY', 'Wyoming')
            )
        ),
        ('Canada', (
                ('AB', 'Alberta' , 1.05),
                ('BC', 'British Columnbia', 1.12),
                ('MB', 'Manitoba', 1.05),
                ('NB', 'New Brunswick', 1.13),
                ('NL', 'Newfoundland and Labrador', 1.13),
                ('NT', 'Northwest Territories', 1.05),
                ('NS', 'Nova Scotia', 1.15),
                ('NU', 'Nunavut', 1.05),
                ('PE', 'Prince Edward Island', 1.05),
                ('SK', 'Saskatchewan', 1.05),
                ('ON', 'Ontario', 1.13),
                ('QC', 'Quebec', 1.05),
                ('YT', 'Yukon', 1.05)
            )
        )
    )

Is there an elegant way of getting the the number based on the province code if a Canadian province is selected. example. If I have the province ON is there a tuple operation that I can use to get the value 1.13.

Thanks.

The comments highlight your main problem -- the unfortunate data structure. You can, however, easily convert your nested tuple into a dict. On Python >=2.7:

>>> choices = {country: {t[0]: t[1:] for t in st} for country, st in CHOICES}
>>> choices['Canada']['ON'][1]
1.13

Note: This will make choices['United States'] dict contain one-element tuples as values.

   Canada={CHOICES[2][1][x][0]:CHOICES[2][1][x][2]for x in range(len(a[2][1]))}
   Canada["ON"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM