V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
fenglala
V2EX  ›  Python

Python 新手语法问题,为什么用 str split 后新建 dict 爆黄呢?

  •  
  •   fenglala ·
    code4lala · 2022-11-16 11:36:24 +08:00 · 3284 次点击
    这是一个创建于 498 天前的主题,其中的信息可能已经有所发展或是发生改变。
    str_x = 'int_value=233/str_value=hello'
    dict_x = dict(element.split('=') for element in str_x.split('/'))
    print(dict_x)
    print(int(dict_x['int_value']))
    print(dict_x['str_value'])
    

    这段代码可以正常运行,但是为什么放到 pycharm 里爆黄呢,请问怎么改才能不爆黄呢?

    dict()里边爆黄:

    Unexpected type(s): (Generator[List[str], Any, None]) Possible type(s): (SupportsKeysAndGetItem[List[str], _VT]) (Iterable[Tuple[Any, Any]]) 
    
    18 条回复    2022-11-17 11:53:26 +08:00
    aladdinding
        1
    aladdinding  
       2022-11-16 11:53:16 +08:00
    dict_x = dict(tuple(element.split('=')) for element in str_x.split('/'))
    fenglala
        2
    fenglala  
    OP
       2022-11-16 11:58:37 +08:00
    @aladdinding 还是爆黄,爆黄信息变了

    Unexpected type(s): (Generator[Tuple[str, ...], Any, None]) Possible type(s): (SupportsKeysAndGetItem[Tuple[str, ...], _VT]) (Iterable[Tuple[Any, Any]])
    westoy
        3
    westoy  
       2022-11-16 11:59:11 +08:00
    dict(element.split('=', 1) for element in str_x.split('/'))


    不过不建议直接搞, 去看看 pyparsing 之类的
    aloxaf
        4
    aloxaf  
       2022-11-16 12:01:20 +08:00
    element.split('=') 可能产生有长度超过 2 的 list ,理论上加上 maxsplit=1 就能满足要求,可惜 pycharm 还没有这么高级

    你只能选择这样干,嫌太长可以用海象运算符简化
    dict_x = dict((element.split('=')[0], element.split('=')[1]) for element in str_x.split('/'))

    当然我更建议
    # noinspection PyTypeChecker
    dict_x = dict(element.split('=') for element in str_x.split('/'))
    fenglala
        5
    fenglala  
    OP
       2022-11-16 12:01:21 +08:00
    @westoy 也爆黄,提示信息变成:
    Unexpected type(s): (Generator[List[str], Any, None]) Possible type(s): (SupportsKeysAndGetItem[List[str], _VT]) (Iterable[Tuple[Any, Any]])
    fenglala
        6
    fenglala  
    OP
       2022-11-16 12:02:44 +08:00
    @aloxaf 感谢!原来只要让 linter 认为这里确定有两个元素就可以了!
    fenglala
        7
    fenglala  
    OP
       2022-11-16 12:04:40 +08:00
    @aloxaf 按照你的思路,这样写也不爆黄了,不用 split 两遍
    dict_x = dict((element.split('=')[0:2]) for element in str_x.split('/'))
    hellojay
        8
    hellojay  
       2022-11-16 12:04:53 +08:00
    dict_x = {element.split('=')[0]: element.split('=')[1] for element in str_x.split('/')}
    fenglala
        9
    fenglala  
    OP
       2022-11-16 12:05:47 +08:00
    啊不行,我大意了,是我前边有 noinspection PyTypeChecker 导致的,我 7 楼的回复还是爆黄
    fenglala
        10
    fenglala  
    OP
       2022-11-16 12:08:30 +08:00
    dict_x = dict((kv[0], kv[1]) for kv in (element.split('=') for element in str_x.split('/')))
    这个可以了!不用多一次 split !
    hellojay
        11
    hellojay  
       2022-11-16 12:16:06 +08:00
    @fenglala #10

    这个让代码没有任何可读性,你少了一个 split 却多了一个 for

    dict_x = {element.split('=')[0]: element.split('=')[1] for element in str_x.split('/')}

    这个也不爆黄
    xuboying
        12
    xuboying  
       2022-11-16 13:13:43 +08:00   ❤️ 1
    能不能说文雅一点的告警或者出错。

    爆黄,在一众标题里显得很黄很暴力。。。
    hsfzxjy
        13
    hsfzxjy  
       2022-11-16 13:47:24 +08:00 via Android
    不要太在意这个,告警是静态检查器蠢,不是你写的有问题
    luckyx
        14
    luckyx  
       2022-11-16 13:58:01 +08:00
    text = 'int_value=233/str_value=hello'

    print({k: v for k,v in [e.split('=') for e in text.split('/')]})

    Leetcode playground 能过 🤔
    yernsun
        15
    yernsun  
       2022-11-16 16:05:08 +08:00   ❤️ 1
    import urllib.parse
    dict(urllib.parse.parse_qsl('int_value=233/str_value=hello', separator='/'))
    zhanglintc
        16
    zhanglintc  
       2022-11-17 00:16:10 +08:00
    @xuboying #12 其实应该是“报黄”,哈哈。
    llsquaer
        17
    llsquaer  
       2022-11-17 09:26:37 +08:00
    怎么就那么喜欢一行写代码...

    {key:value for key,value in map(lambda item:item.split('='),str_x.split('/'))}
    u823tg
        18
    u823tg  
       2022-11-17 11:53:26 +08:00   ❤️ 1
    是 pycharm 的问题吧, 建议别用-_-。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5119 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.