0

我有一个包含省数据的元组。

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)
            )
        )
    )

如果选择了加拿大省,是否有一种基于省代码获取号码的优雅方法。例子。如果我将省设置为 ON,是否可以使用元组操作来获取值 1.13。

谢谢。

4

2 回答 2

3

这些评论突出了您的主要问题——不幸的数据结构。但是,您可以轻松地将嵌套元组转换为字典。在 Python >=2.7 上:

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

注意:这将使choices['United States']dict 包含单元素元组作为值。

于 2012-11-06T16:17:06.197 回答
0
   Canada={CHOICES[2][1][x][0]:CHOICES[2][1][x][2]for x in range(len(a[2][1]))}
   Canada["ON"]
于 2012-11-06T17:18:28.113 回答