'''定义数据结构,存储以下数据。 (1) 多个人的多个爱好 {qtx:[编码,看书,跑步] 悟空:[刷抖音,飞翔[ 八戒:[美食,美女,睡觉]} ''' dict01 = {} while True: name = input('请输入姓名') if name == '': break list01 = [] while True: taste01 = input('请输入爱好') if taste01 == '': break list01.append(taste01) dict01[name]=list01 print(dict01)
''' 多个城市的多个景区与美食 "北京": "景区":"故宫","天安门","长城" "美食":"烤鸭","豆汁" "四川": "景区":"九寨沟","峨眉山" "美食":"火锅","老妈兔头" -- 打印北京美食(一行一个) -- 打印所有城市(一行一个) -- 打印所有城市的景区(一行一个) ''' dict01={ "北京":{"景区":["故宫","天安门","长城"],"美食":["烤鸭","豆汁"]}, "四川": {"景区":["九寨沟","峨眉山"],"美食":["火锅","老妈兔头"]} } # 1 for i in dict01['北京']['美食']: print(i) # 2 for item in dict01: print(item) print() # 3 for item in dict01: for i in dict01[item]['景区']: print(i)
"列表升序" list01 = [3, 8, 6, 5, 2] for i in range(len(list01) - 1): for j in range(i, len(list01)): if list01[j] < list01[i]: list01[i], list01[j] = list01[j], list01[i] print(list01)