【问题标题】:python list within list initialization and printing列表初始化和打印中的python列表
【发布时间】:2017-12-13 05:40:23
【问题描述】:

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

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为真时,我想将temp_a插入到第二个列表的索引0中。

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

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

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

2) 列表 ['one','two','three'] 用于测试打印正在发生的事情。如果我尝试打印出 matrix[test_count][0],我会收到“index out of range”错误,但是当我将它更改为打印出 matrix[0][0] 时,它会正确打印“one”。有没有我在这里遗漏的范围内的东西?

【问题讨论】:

  • 如果您发布一个示例输入文件和预期输出会有所帮助。
  • 列表没有任何内容,除非您在其中添加内容。因此,如果那里什么都没有,访问matrix[0] 可能会失败。但是,matrix.append(temp_a) 肯定会在列表末尾添加一些内容。
  • 是的,我知道 matrix.append 会添加到末尾,但我想添加到 matrix[var][0].append 示例输入文件是简单的逐行文本,问题是t 带有文件输入,但带有矩阵定义和附加...
  • 您正在读取的这个文件到底是什么样的?我做了一个测试文件,我得到的index out of bounds 错误出现在你的正则表达式上,所以我认为我的文件是错误的。

标签: python list matrix


【解决方案1】:

回答您的问题:

1) 像这样:matrix = []

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

2) 所以你是 index out of range 错误是因为你将 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']]

【讨论】:

  • 我按你说的方法试了。它在应该打印“一个”的打印行上出现相同的索引超出范围的错误
  • @YafimSimanovsky 真的......你能从你正在解析的文件中发布几行,这样我就可以确切地看到你想要什么?
  • 啊。看起来这真的很有必要:) 示例行:#001= squares('boxes',$,$); \n #002= 三角形('等边',$,$); \n #003= 圆('球体',$,$); \n 输出矩阵应该是 [ [boxes] [equilateral] [sphere] ]
  • @YafimSimanovsky 所以输出是只有大小为 1 的列表?那么为什么不是字符串列表呢? ['boxes','equilateral','sphere']
  • @YafimSimanovsky 无论如何,我的编辑应该会产生您想要的输出。
【解决方案2】:

对于 2),您是否尝试打印出 test_count?由于您的 test_count+=1 在 if 语句中,因此如果不打印“one”,它不应该超出范围。

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

if test_count == len(matrix):
    matrix.append([])

如果 test_count 超出矩阵范围,则添加一个新的空列表。

编辑:

temp_a = re.findall(r"\'(.+?)\'",line)[0] 行引起的“超出范围”,因为它找不到任何东西。所以它是一个空列表,并且 [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

【讨论】:

  • 我试过了,但它仍然有索引范围的问题。我也尝试了不带if的matrix.append([]),但它仍然有同样的错误......
  • 我刚刚运行了你的代码。超出范围不是来自矩阵,而是来自 temp_a = re.findall(r"\'(.+?)\'",line)[0]。它找不到任何东西。
  • 我按你说的试过了。我唯一不同的是 .insert 而不是 .append 但应该是一样的吧?在这两种情况下,它仍然会出错..
  • @YafimSimanovsky 一方面,当您在字符串而不是列表上调用 insert 时,问题中的插入会产生错误。其次insert 需要两个参数,而append 不需要。
  • L.insert(index, object) -- 在索引之前插入对象
猜你喜欢
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多