Python入门学习(4)

来源:互联网 发布:淘宝上卖内衣的货源 编辑:程序博客网 时间:2024/05/18 09:06
  1. 删除包含特定值得所有列表元素
pets = ['cat','dog','goldfish','cat','rabit','cat']print(pets)while 'cat' in pets:    pets.remove('cat')print(pets)

运行结果如下:

[‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabit’, ‘cat’]
[‘dog’, ‘goldfish’, ‘rabit’]

2 . 使用用户输入来填充字典

responses = {}# 设置一个标志,指出调查是否继续polling_active = Truewhile polling_active:    name = input("你叫什么名字?")    response = input("将来有一天你想去哪个城市?")    # 将答案存储在字典中    responses[name] = response    # 看看是否还有人要参与调查    repeat = input("还有没有人要回答?")    if repeat == 'no':        polling_active = False# 调查结束,显示结果print("\n--------调查结果--------")for name,response in responses.items():    print(name + "想去" + response + "。")