Python元组操作 - 获取价值

时间:2012-11-06 16:01:22

标签: python list dictionary tuples

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

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。

感谢。

2 个答案:

答案 0 :(得分:3)

评论突出了您的主要问题 - 不幸的数据结构。但是,您可以轻松地将嵌套元组转换为dict。在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包含单元素元组作为值。

答案 1 :(得分:0)

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