推荐学习书目
› 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
u2gign
V2EX  ›  Python

如何在 for 循环中赋值给不同的变量

  •  
  •   u2gign · Oct 22, 2019 · 5702 views
    This topic created in 2571 days ago, the information mentioned may be changed or developed.
    例如我想生成很多的变量,a1=1,a2=2,a3=3,a4=4 ...
    Supplement 1  ·  Oct 22, 2019
    实际需求是需要生成一个配置文件,这个文件通过传进来的 list,生成很多信息
    格式类似如下,应该会有 100 多条这种信息
    xx1 = {
    'id':1,
    'hosts': '1.1.1.1',
    'port': 22222,
    'username': 'root',
    'pwd': 'aaaaaaaaaaaaaa'
    }
    xx2 = {
    'id':2,
    'hosts': '2.2.2.2',
    'username': 'root',
    'pwd': 'sssssssssssss'
    }
    ...
    20 replies  •  2019-10-23 00:41:47 +08:00
    qwerthhusn
    qwerthhusn
    Oct 22, 2019
    1
    OMG,list 不行?
    airfling
    airfling
    Oct 22, 2019
    2
    矩阵不行吗,数组不行吗,list 不行吗
    Joyboo
    Joyboo
    Oct 22, 2019
    3
    可变变量
    U7Q5tLAex2FI0o0g
    U7Q5tLAex2FI0o0g
    Oct 22, 2019
    4
    ```php
    <?php
    for ($i = 1; $i <= 4; $i++) {
    $k = 'a' . $i;
    $$k = $i;
    }
    var_dump($a1);
    var_dump($a2);
    var_dump($a3);
    var_dump($a4);
    ```

    python 不懂
    ipwx
    ipwx
    Oct 22, 2019 via Android
    5
    意义何在
    arrow8899
    arrow8899
    Oct 22, 2019
    6
    https://en.wikipedia.org/wiki/XY_problem
    直接说你的原始需求
    airdge
    airdge
    Oct 22, 2019
    7
    locals()['a1']=1
    JeffGe
    JeffGe
    Oct 22, 2019 via Android
    8
    可以用 locals() 或者 globals()
    但为什么要这么做?你是什么样的需求不能用 list 吗?
    u2gign
    u2gign
    OP
    Oct 22, 2019
    9
    @arrow8899 我要生成一个配置文件 根据传进来的 list 生成很多配置信息
    krixaar
    krixaar
    Oct 22, 2019
    10
    字典不行吗,和一堆变量用起来没啥区别啊
    JeffGe
    JeffGe
    Oct 22, 2019 via Android
    11
    @u2gign 那用 dict 更好吧

    config["a1"] = 1
    userff
    userff
    Oct 22, 2019
    12
    ```
    name_dict = {}
    for i in range(1, 5):
    k = 'a' + str(i)
    name_dict['%s' % k] = i

    print(name_dict)
    ```

    输出:

    {'a3': 3, 'a1': 1, 'a2': 2, 'a4': 4}
    wym7223645
    wym7223645
    Oct 22, 2019
    13
    //用 map,key 是变量名,value 是实际的值, key 动态生成
    for (int i = 0; i < list.size; i++) {
    map.put("a"+1,"实际的值");
    }

    大概这样应该可以吧
    userff
    userff
    Oct 22, 2019
    14
    #12 补充
    如需变量名将 name_dict = {} 改为 name_dict = locals()
    Trim21
    Trim21
    Oct 22, 2019 via Android
    15
    你把 xx1 xx2 什么的也放到一个 list 里面或者一个 dict 里面

    最后生成一个这个


    [{
    'id':1,
    'hosts': '1.1.1.1',
    'port': 22222,
    'username': 'root',
    'pwd': 'aaaaaaaaaaaaaa'
    },
    {
    'id':2,
    'hosts': '2.2.2.2',
    'username': 'root',
    'pwd': 'sssssssssssss'
    }]
    cyrbuzz
    cyrbuzz
    PRO
    Oct 22, 2019
    16
    以前我也有这么个需求,后来发现变量是保存在`locals()`和`globals()`里的,再后来发现用不着这样做。
    `locals()`和`globals()`也是个字典,直接用字典就好了。
    lanshee
    lanshee
    Oct 22, 2019
    17
    要变量名就 dict,不要就是 list
    u2gign
    u2gign
    OP
    Oct 22, 2019
    18
    多谢各位
    secondwtq
    secondwtq
    Oct 22, 2019
    19
    楼主以前不会是用 LISP 的吧 ...
    necomancer
    necomancer
    Oct 23, 2019
    20
    locals()
    这个字典里放的是命名的变量。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1448 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 22:57 · PVG 06:57 · LAX 14:57 · JFK 17:57
    ♥ Do have faith in what you're doing.