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
awker
V2EX  ›  Python

Python dict 组合操作问题

  •  
  •   awker · 2017-11-27 18:48:35 +08:00 · 2064 次点击
    这是一个创建于 2334 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一个列表和一个字典
    port = [80, 443]
    value_port = {'TX_80': 0, 'RX_443': 0, 'RX_80': 0, 'TX_443': 0}

    如何得出下面的结果?
    port_80 = {'TX_80': 0, 'RX_80': 0}
    port_443 = {'RX_443': 0, 'TX_443': 0}
    18 条回复    2017-11-28 02:26:36 +08:00
    scriptB0y
        1
    scriptB0y  
       2017-11-27 19:06:45 +08:00   ❤️ 1
    result = defaultdict(list)
    for k,v in value_port.items():
    result[re.search(r"_(\d{1,5})").group(1)].append({k:v})

    随手写的,我觉得差不多
    est
        2
    est  
       2017-11-27 19:37:20 +08:00   ❤️ 1
    Python 多个列表如何合并成一个大列表?

    /t/409304


    LZ 还是要多练习基础哦。
    Thanks
        3
    Thanks  
       2017-11-27 19:39:06 +08:00   ❤️ 1
    可以根据字符串包含判断
    1. 将 port 转成字符串,然后判断是否被包含在某个键中;
    2. 或者将键根据下划线拆分,然后判断第二个位置的字符串是不是等于 port 元素的字符串形式。
    另外,如果需要动态创建 'port_80' 这种变量的话,可以看一下 locals() 函数。
    awker
        4
    awker  
    OP
       2017-11-27 19:46:12 +08:00
    @scriptB0y defaultdicts 是什么?
    awker
        5
    awker  
    OP
       2017-11-27 19:46:24 +08:00
    @est 共同学习
    cbiqih
        6
    cbiqih  
       2017-11-27 20:37:06 +08:00   ❤️ 1
    port_80, port_443 = tuple(list(filter(lambda x: x.endswith(str(i)), value_port)) for i in port)
    awker
        7
    awker  
    OP
       2017-11-27 21:03:35 +08:00
    @cbiqih 厉害 膜拜大佬 我在看看怎么动态创建 'port_80' 这种变量
    cbiqih
        8
    cbiqih  
       2017-11-27 21:05:48 +08:00   ❤️ 1
    貌似并非是你要的结果,你要生成的是字典,上面生成的是列表哇~
    Trim21
        9
    Trim21  
       2017-11-27 21:06:28 +08:00   ❤️ 1
    @awker #4 from collections import defaultdict
    awker
        10
    awker  
    OP
       2017-11-27 21:08:56 +08:00
    @cbiqih 没事 有思路就行
    jxie0755
        11
    jxie0755  
       2017-11-28 00:40:22 +08:00   ❤️ 1
    哥,怎么又是你~~~由于我也是新手,最近才学了几个月 python,所以权当自己练习了哈~~
    先感谢你的'感谢'

    # 动态生成你要的变量名
    port = [80, 443]
    new_port = []
    for i in port:
    new_port.append('port_' + str(i))
    print(new_port)

    for i in range(len(new_port)):
    vars()[new_port[i]] = temp[i]
    print(port_80)
    print(port_443)

    # 简写可以变成
    port = [80, 443]
    new_port = ['port_' + str(i) for i in port]

    # 最终结果
    port = [80, 443]
    value_port = {'TX_80': 0, 'RX_443': 0, 'RX_80': 0, 'TX_443': 0}
    new_port = ['port_' + str(i) for i in port]

    temp = []
    for i in port:
    new_dict = {}
    for keys, values in value_port.items():
    if str(i) in keys:
    new_dict[keys] = values
    temp.append(new_dict)

    for i in range(len(new_port)):
    vars()[new_port[i]] = temp[i]


    我只能帮你到这里了,这个把 string 变成变量名的方法我还真不会,stackoverlfow 上面都没有,是临时 google 找出来的.
    我看 stackoverflow 上面大多数人是反对这么做的, 觉得这么做很吃力不讨好的感觉? 我是新手也不太懂
    jxie0755
        12
    jxie0755  
       2017-11-28 00:42:16 +08:00   ❤️ 1
    补一下,不知道为什么缩进没了...

    port = [80, 443]
    value_port = {'TX_80': 0, 'RX_443': 0, 'RX_80': 0, 'TX_443': 0}
    new_port = ['port_' + str(i) for i in port]

    temp = []
    for i in port:
    new_dict = {}
    for keys, values in value_port.items():
    if str(i) in keys:
    new_dict[keys] = values
    temp.append(new_dict)

    for i in range(len(new_port)):
    vars()[new_port[i]] = temp[i]
    jxie0755
        13
    jxie0755  
       2017-11-28 00:44:39 +08:00   ❤️ 1
    擦,不管怎么整,缩进都被吞没, 用等号代替

    port = [80, 443]
    value_port = {'TX_80': 0, 'RX_443': 0, 'RX_80': 0, 'TX_443': 0}
    new_port = ['port_' + str(i) for i in port]

    temp = []
    for i in port:
    ==new_dict = {}
    ==for keys, values in value_port.items():
    ====if str(i) in keys:
    ======new_dict[keys] = values
    ==temp.append(new_dict)

    for i in range(len(new_port)):
    ==vars()[new_port[i]] = temp[i]

    print(port_80)
    print(port_443)
    awker
        14
    awker  
    OP
       2017-11-28 00:53:45 +08:00
    @jxie0755 兄弟 你这新手任务可以啊 我自愧不如啊
    awker
        15
    awker  
    OP
       2017-11-28 00:54:40 +08:00
    @jxie0755 上条去掉“任务”
    jxie0755
        16
    jxie0755  
       2017-11-28 01:49:00 +08:00   ❤️ 1
    @awker 都是瞎弄~~~高手都是一行代码完成任务的...
    jxie0755
        17
    jxie0755  
       2017-11-28 01:53:08 +08:00   ❤️ 1
    @awker 不过这些真的不是很难,你是不是应该找本基础点的教材过一道啊,其实也不花很多时间,几个星期就行了.
    awker
        18
    awker  
    OP
       2017-11-28 02:26:36 +08:00 via iPhone
    @jxie0755 带着问题学习 效率高点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1068 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:06 · PVG 03:06 · LAX 12:06 · JFK 15:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.