推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
iszengmh
V2EX  ›  Python

Python 中 del users[user]没有删除,反倒另外一个元素被删除了,这是为什么

  •  
  •   iszengmh · May 18, 2020 · 4387 views
    This topic created in 2362 days ago, the information mentioned may be changed or developed.
    users = {'user':'admin','status':'inactive'}
    # Strategy:  Iterate over a copy
    for user, status in users.copy().items():
        if status == 'inactive':
            print('删除 key 为 user 的元素')
            del users[user]
    print(users) # {'user': 'admin'}
    

    输出了 删除 key 为 user 的元素 {'user': 'admin'}

    为什么 user 没有被删除,而 status 却被删除了。初学者请多指教。

    16 replies  •  2020-05-20 17:50:57 +08:00
    dbow
    dbow
    May 18, 2020
    1
    用 users.deep_copy()
    mayorbryant
    mayorbryant
    May 18, 2020
    2
    `dict().items()` 返回的是[(key, value),...]
    不删 status 删哪个?不要被变量名和字符串搞混了
    LincolnZh
    LincolnZh
    May 18, 2020 via iPhone
    3
    删除 users 中 user 对应元素引用时要加引号的吧?

    比如 del users[‘user’]

    del users[user]是无法编译通过的
    dbow
    dbow
    May 18, 2020
    4
    @dbow #1 草, 看错了, 原来是变量跟字符串不分导致的。
    n329291362
    n329291362
    May 18, 2020❤️1
    5
    我觉得你应该是想这样?
    users = [{'user':'admin','status':'inactive'}]
    # Strategy: Iterate over a copy
    for index in range(len(users)):
    user = users[index]
    if user['status'] == 'inactive':
    print('删除 key 为 user 的元素')
    del users[index]
    print(users) # {'user': 'admin'}
    wildestdream
    wildestdream
    May 18, 2020❤️1
    6
    user 和 status 在遍历的第一次的值分别是
    user=user
    status=admin
    不满足 if 中的条件 不会删除这个键值对
    在遍历的第二次中
    user=status
    status=inactive
    满足 if 中的条件,所以会删除此键值对
    最后输出 users,就会输出
    {'user': 'admin'}
    customsshen
    customsshen
    May 18, 2020
    7
    users = {'user':'admin','status':'inactive'}
    if users['status'] == 'inactive':
    del users['user']
    print(users)
    misaka19000
    misaka19000
    May 18, 2020❤️1
    8
    我看傻了,你把 'status':'inactive' 删了,剩下的就是 {'user': 'admin'}

    这样有什么问题吗????
    Yourshell
    Yourshell
    May 18, 2020
    9
    你先把循环中的 user 打印出来看看就懂了
    malone6
    malone6
    May 18, 2020 via Android
    10
    你这 for 解包命名不规范自己搞蒙了😰
    MoYi123
    MoYi123
    May 18, 2020 via Android
    11
    你在这秀操作呢
    aragakiyuii
    aragakiyuii
    May 19, 2020 via Android
    12
    工作中这么写怕不是要被人打死😂
    cominghome
    cominghome
    May 19, 2020
    13
    一脸懵逼地进来,一脸懵逼地出去。。。不会用断点好歹 print 一下啊
    jacklin96
    jacklin96
    May 19, 2020
    14
    让我想起了那个姓万的学写字的故事
    如果 users 里有一万个 key...
    nekochyan
    nekochyan
    May 19, 2020
    15
    搞清楚 del users[user]中的 user 到底是什么就知道了,你这里的 user 是变量名不是字符串吧
    HashV2
    HashV2
    May 20, 2020
    16
    边遍历边删除遍历的对象? 确定没问题吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2721 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 13:17 · PVG 21:17 · LAX 05:17 · JFK 08:17
    ♥ Do have faith in what you're doing.