如何在python中使用字符串引用列表 [英] how to reference a list using a string in python

查看:72
本文介绍了如何在python中使用字符串引用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有8个列表,其中包含一堆名为erik1,erik2,...,erik8的数据.我的代码是:

I have 8 lists that have a bunch of data in them named erik1,erik2,...,erik8. The code I have is:

while (y<9):#go through all the arrays
    y=y+1
    array="erik"+str(y)
    print (array)


    for z in array:

在for循环中,我希望数组对应于在其上方的while循环中生成的字符串.现在,它认为数组是一个字符串,而不是我想要的列表对象.我该如何进行这项工作?

In the for loop, I want array to correspond to the string that is generated in the while loop above it. Right now it thinks of array as a string rather than the list object I want. How can i make this work?

推荐答案

变量名称是代码,而不是数据.请改用列表列表.

Variable names are code, not data. Use a list of lists instead.

eriks = [erik1, erik2, erik3, erik4, erik5, erik6, erik7, erik8]
for erik in eriks:
    for z in erik:
     ....

退后一步,无论创建 8 个类似命名的列表是什么,首先应该创建一个列表(或列表字典).

Taking a step back, whatever is creating the 8 similarly named lists should be creating a single list of lists (or a dictionary of lists) in the first place.

这篇关于如何在python中使用字符串引用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆