Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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_Matrix - Fatal编程技术网

列表初始化和打印中的python列表

列表初始化和打印中的python列表,python,list,matrix,Python,List,Matrix,我试图将一个元素附加到一个列表中,该列表每次都有一个递增的值: def get_data(file): matrix = [ ['one','two','three'] ] #list of lists test_count = 0 line_count = 0 #keep track of which line we are on for line in file: if line.find('example') != -1: #test for example st

我试图将一个元素附加到一个列表中,该列表每次都有一个递增的值:

def get_data(file):
  matrix = [ ['one','two','three'] ] #list of lists
  test_count = 0
  line_count = 0 #keep track of which line we are on

  for line in file:
    if line.find('example') != -1: #test for example string
      temp_a = re.findall(r"\'(.+?)\'",line)[0]
      print matrix[test_count][0] #should print 'one'
      matrix[test_count][0].insert(temp_a) #should insert temp_a instead of 'one'
      test_count += 1 #go to next "new" list in the matrix
      line_count += 1 #go to next line
我想要的是findall的结果进入temp_a,然后从那里将它插入到列表中第一个列表的索引0中。下一次findall为true时,我想在第二个列表的索引0中插入temp_a

例如,如果第一个temp_a值为9,我希望矩阵中的第一个列表为: [9,y,z]] 如果在第二个芬德尔,我的温度是4,我希望矩阵变成: [9,y,z],[4,y,z]]

以上代码是我迄今为止最好的尝试。 我有两个问题:

1如果列表数量不固定,如何初始化“列表列表”


2列表['1'、'2'、'3']是用来测试打印正在发生的事情。如果我尝试打印出矩阵[test_count][0],我会得到一个索引超出范围的错误,但当我将其更改为打印出矩阵[0][0]时,它会正确打印“1”。我在这里遗漏了一些关于范围的内容吗?

回答您的问题:

1如下所示:矩阵=[]

简单地说,这只会创建一个空列表,您可以在其中添加任何您想要的内容,包括更多列表。所以matrix.append[1,2,3]会给你这样一个列表:[[1,2,3]]

2所以您的索引超出范围错误来自这样一个事实:您将test_count增加到1,但您的矩阵剩余长度为1,这意味着它只有0索引,因为您从未附加任何内容。为了获得您想要的输出,您需要进行一些更改:

def get_data(file):
  example_list = ['one','two','three']
  matrix = [] #list of lists
  test_count = 0
  line_count = 0 #keep track of which line we are on

  for line in file:
    if line.find('example') != -1: #test for example string
      temp_a = re.findall(r"\'(.+?)\'",line)[0]
      new_list = example_list[:]
      new_list[0] = temp_a
      matrix.append(new_list)
      test_count += 1 #go to next "new" list in the matrix
      line_count += 1 #go to next line

print matrix #[['boxes', 'two', 'three'], ['equilateral', 'two', 'three'], ['sphere', 'two', 'three']]

对于2,您是否尝试打印测试计数?因为您的test_count+=1在if语句中,所以如果不打印它,它就不应该超出范围

对于1,您可以在插入之前执行以下操作:

if test_count == len(matrix):
    matrix.append([])
如果测试计数超出矩阵范围,则添加一个新的空列表

编辑:

第[0]行的temp\u a=re.findallr\'.+?\'导致超出范围,因为它找不到任何内容。所以它是一个空列表,[0]超出范围

def get_data(file):
  matrix = [ ['one','two','three'] ] #list of lists
  test_count = 0
  line_count = 0 #keep track of which line we are on

  for line in file:
    if line.find('example') != -1: #test for example string
      temp_a = re.findall(r"\'(.+?)\'",line)
      if temp_a:
        temp_a = temp_a[0]
      else:
        continue # do something if not found
      print(matrix[test_count][0]) #should print 'one'
      new_list = matrix[test_count][:]
      new_list[0] = temp_a
      matrix[test_count].append(new_list) #should insert temp_a instead of 'one'
      test_count += 1 #go to next "new" list in the matrix
      line_count += 1 #go to next line

如果您发布一个示例输入文件和预期的输出,这会有所帮助。列表中没有任何内容,除非您在其中添加了内容。因此,如果矩阵[0]中没有任何内容,则访问该矩阵可能会失败。但是,matrix.appendtemp_a肯定会在列表的末尾添加一些内容。是的,我知道matrix.append会添加到末尾,但我想添加到matrix[var][0]。append示例输入文件是简单的逐行文本,问题不在于文件输入,而在于矩阵定义和附加……您从中读取的文件到底是什么样子的?我制作了一个测试文件,我得到的索引越界错误出现在你的正则表达式上,所以我假设我的文件是错误的。我尝试过,但它仍然存在索引范围问题。我还尝试在没有if的情况下添加matrix.append[],但仍然有相同的错误…我只是运行了您的代码。超出范围不是来自矩阵,而是来自temp\u a=re.findallr\'.+?\',第[0]行。什么也找不到,我照你说的做了。唯一不同的是,我做的是.insert而不是.append,但应该是相同的,对吗?在这两种情况下,它仍然会出错。@YafimSimanovsky对于其中一种情况,当您在字符串而不是列表上调用insert时,问题中的insert会产生错误。第二,insert需要两个参数,而append不需要。L.insertindex,object-在indexI按照您所说的方式尝试之前插入object。打印行上的同一索引超出范围时出错,应该打印'one'@YafimSimanovsky真的…你能从你正在解析的文件中发布几行内容吗,这样我就可以准确地看到你想要什么了?啊。看起来这真的很有必要:示例行:001=正方形“长方体”,$,$\n 002=三角形‘等边’,$,$\n 003=圆形“球体”、$、$\n输出矩阵应为[[box][equilateral][sphere]]@YafimSimanovsky,因此输出的列表大小仅为1?那么为什么不列一个字符串列表呢?['box'、'equilateral'、'sphere']@YafimSimanovsky不管怎样,我的编辑应该会产生您想要的输出。