两层词典的嵌套

person_0 = {'surname': 'kaihu', 'given_name': 'biangeng', 'age':'18','city': 'kaixiaobian'}
person_1 = {'surname': 'shebaoka', 'given_name': 'chongzuoka', 'age':'19', 'city': 'ka'}
person_2 = {'surname': 'gongjijin', 'given_name': 'guiji', 'age':'20', 'city': 'tiqu'}
persons = [person_2,person_1,person_0]

for person in persons:
    print(person)

pet_0 = {'type': 'kaihu','owner':'lixueqing',}
pet_1 = {'type': 'biangeng', 'owner':'hounaonao'}
pet_2 = {'type': 'xiaohu', 'owner':'xiaotiantian'}

pets = [[pet_2,pet_1,pet_0]]

for pet in pets:
    print(pet)

favorite_places = {
    'lixueqing' :['kaihu','xiaohu'],
    'hounaonao' :['biangeng'],
    'xiaotiantian':['shebaoka','chongzuoka','tunka'],
    }

for person, favorite_place in favorite_places.items():
    print(f"{person.title()}'s favorite place is:")
    for place in favorite_place:
        print(f"\t{place.title()}")

favorite_numbers = {
    'kaihu':[1,2],
    'xiaohu':[2,3,4],
    'biangeng':[5],
    'shebaoka':[4,7,8],
    'chongzuoka':[5,7,9,0],
    }

for person, numbers in favorite_numbers.items():
    print(f"{person.title()}'s favorite number is:")
    for number in numbers:
        print(number)

cities = {
    'kaihu': {
        'country': 'china',
        'population': 100,
        'fact': 'kaihushu',
    },
    'biangeng':{
        'country':  'us',
        'population': 200,
        'fact': 'biangengshu',
    },
    'xiaohu': {
        'country': 'uk',
        'population': 300,
        'fact': 'xiaohushu',
    }
}

for city, city_info in cities.items():
    print(f"City: {city}")
    country = f"{city_info['country']}"
    population = f"{city_info['population']}"
    fact = f"{city_info['fact']}"

    print(f"{city.title()},located in {country.title()}.There is {population} people in this city,happened {fact.upper()}")

关于两层嵌套可以用在有关用于属性的方面,例如万叶的攻伤暴精通等属性以及风伤加成。通过词典的二级嵌套我可以创建出来一个RPG人物的属性。更复杂的可以存在三层嵌套,但是不如另外创建一个二级嵌套系统然后实施互相影响。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值