Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取空白列表-不知道为什么?_Python_List_For Loop_Enumerate - Fatal编程技术网

Python 获取空白列表-不知道为什么?

Python 获取空白列表-不知道为什么?,python,list,for-loop,enumerate,Python,List,For Loop,Enumerate,我使用以下代码: from __future__ import division from __future__ import print_function in_file = open("s:/Personal Folders/Andy/Python Projects/People Cancelled/Analyze Authorize Truncated.csv") text = in_file.readlines() in_file.close() header = text[0:1]

我使用以下代码:

from __future__ import division
from __future__ import print_function

in_file = open("s:/Personal Folders/Andy/Python Projects/People Cancelled/Analyze Authorize Truncated.csv")
text = in_file.readlines()
in_file.close()

header = text[0:1]
text = text[1:]

for index, line in enumerate(text):
    text[index] = line.split(",")

name = text
dates = text

for index, line in enumerate(name):
    name[index] = line[3:5]

for stuff, area in enumerate(dates):
     dates[stuff] = area[7:8]

print(name)
print(dates)
我得到这个输出:

 [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
 [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
知道为什么吗?出于某种原因,两个For循环之间似乎存在某种干扰-如果我单独执行其中一个,我将得到我想要的结果


谢谢

看看这两行:

name = text
date = text

这些行不复制要指向的名称和日期文本;相反,它们将名称和日期本质上都设置为文本的备用名称。因此,在第三个for循环(body
name[index]=line[3:5]
之后,
text
中的每一行都有两个项目长(因为
text
name
是相同的列表)。事实上,我甚至不明白为什么您最初将
名称
日期
设置为
文本
;只需将
名称
日期
设置为新的空列表,并根据
文本
向其添加项目,即可实现预期的结果。您能给我们一个输入数据的示例吗?啊,doh,这很有意义!我必须想办法如何将旧列表中的值附加到新列表中,但这应该不会太难。实际上,我还可以获取一段文本(即文本[:])而且工作列表应该有一个方法
append
——看起来应该是完美的。或者,您可以使用列表理解来根据旧列表定义新列表,比如
name=[line[3:5]表示文本中的行]