如何在python中创建未知数量的变量 [英] How to create an unknown amount of variables in python

查看:678
本文介绍了如何在python中创建未知数量的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个读取电子表格的程序.对于每一列,我的程序都会创建该列每一行中所有值的列表.为了确定我需要多少个列表,我有变量columnBound,它是电子表格中的列总数.我该如何制作一个程序,如果有3列,有时会创建3个列表,如果有8列,有时会创建8个列表?

I am making a program that reads a spread sheet. For each column, my program creates a list of all the values in each row of that column. To decide how many lists I need, I have the variable columnBound which is the total amount of columns in the spread sheet. How can I make a program that will sometimes create 3 lists if there are 3 columns and will sometimes create 8 if there are 8 columns?

例如,如果总是有3列,我知道我可以轻松地拥有list1,list2,list3并根据需要构建它们,但是我如何让我的程序基于columnBound构建动态数量的列表?

If there were always 3 columns, for example, I know I could easily have list1, list2, list3, and build them as needed, but how can I have my program build a dynamic number of lists based on columnBound?

就像我想要的

for x in range (0, columnBound):
    listx = [] 

在哪里有list1,list2,....一直到listx(或listcolumnBound)

Where I would have list1, list2, .... all the way to listx (or listcolumnBound)

我对编程非常陌生,并且会喜欢概念性的帮助,这是朝着正确方向发展的方向.我不知道该如何搜索这个问题,因为它非常抽象.

I am very new to programming and would love conceptual help, a point in the right direction where. I don't exactly know how to google this question because it is very abstract.

谢谢!

其他信息:

我的程序将使用电子表格作为输入.每列包含5位数的参考编号,对应于特定的公司地址.然后,将使用不同的电子表格,其中每行都有参考代码,但需要在最后一列中插入一个地址.我将查询每个列表,以查看其是否具有匹配的参考代码,然后在电子表格中输入相应的地址.有时我会有5个地址列,有时可能会有8个.我知道制作一个显式键入的程序(在这里我专门创建列表1-8,如果有9个地址列,则第9个将被忽略)是不好的实践.我想学习如何使我的程序适应有多少列.

My program will use the spreadsheet as an input. Each column contains 5 digit reference numbers that correspond to a specific business address. Then, it will take a different spreadsheet where each row has a reference code but needs an address inserted into the last column. I will query each list to see if it has the matching ref code and enter in the respective address into the spreadsheet. Sometimes I will have 5 address columns, sometimes I might have 8. I know that making a program that is explicitly typed (where I specifically create list 1-8 and if there were 9 address columns, the 9th would be left out) is bad practice. I want to learn how to make my program adapt to how many columns there are.

推荐答案

列表(或实际上是生成器依次为您提供元组)的列表是您将从csv模块接收的数据类型.可能是您要使用的.

A list of lists (or in fact a generator giving you tuples in turn) is the data type you would receive from the csv module. Which is probably what you want to use.

请参阅: http://docs.python.org/dev/library/csv

这篇关于如何在python中创建未知数量的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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