Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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 - Fatal编程技术网

Python:如何读取和存储列号不断变化的数据到列表中?

Python:如何读取和存储列号不断变化的数据到列表中?,python,Python,我的数据如下 ... 5 4 3 16 22 247 0 1.168 0.911 0.944 3.205 0.000 0.562 6 4 4 17 154 93 309 0 0.930 0.919 0.903 0.917 3.852 0.000 1.419 7 3 2 233 311

我的数据如下

...
 5 4 3 16 22 247 0         1.168         0.911         0.944         3.205         0.000         0.562
 6 4 4 17 154 93 309 0         0.930         0.919         0.903         0.917         3.852         0.000         1.419
 7 3 2 233 311 0         0.936         0.932         1.874         2.000        -0.807
...
数据是由整数和浮点数组成的,但我希望只收集整数,获取它们的元素并使用它们。但是,此数据的总列数正在更改。幸运的是,此数据的第3列是下一列的数量。例如,第1行第3列中有“3”,后面有3个整数。下一行在第3列中有“4”,所以第3行后面有4个整数。最后一行有“2”,因此该行有2个整数

以前,我编写了一个代码作为makeemptylist,并将数据放入列表中,例如

   at_index = [None]*nline
   at_type = [None]*nline
   num_of_bonds = [None]*nline
   neighbor_id1 = [None]*nline
   neighbor_id2 = [None]*nline
   neighbor_id3 = [None]*nline
   neighbor_id4 = [None]*nline
   neighbor_id5 = [None]*nline
   for i1 in nlines:
      ### Split each line based on spaces
          line = data_lines[i1].split()
          at_index[i1] = int(line[0])
          at_type[i1] = int(line[1])
          num_of_bonds[i1] = int(line[2])
          if num_of_bonds[i1] == 2:
             neighbor_id1[i1] = int(line[3])
             neighbor_id2[i1] = int(line[4])
          if num_of_bonds[i1] == 3:
             neighbor_id1[i1] = int(line[3])
             neighbor_id2[i1] = int(line[4])
             neighbor_id3[i1] = int(line[5])
          if num_of_bonds[i1] == 4:
             neighbor_id1[i1] = int(line[3])
             neighbor_id2[i1] = int(line[4])
             neighbor_id3[i1] = int(line[5])
             neighbor_id4[i1] = int(line[6])
但此尝试失败,因为“neighbor_id1”到“neighbor_id4”中的所有数据都被最后一个if循环“num_of bonds[i1]==4”条件覆盖。似乎我需要区分列表的名称,例如“neighbor1_id1”和“neighbor4_id3”,但这要求我在做某些事情之前先创建所有这些空数组

如何读取和存储具有“动态列数”的数据;以干净整洁的方式,我仍然可以使用每个列中的元素?多谢各位


最好的,只需将其分割、切片和骰子

事实上,你需要知道

  • 如何使用
    str.split
  • 切片和索引的工作原理
  • 实施

    for line in st.splitlines():
        line = line.split()
        line = line[:3+int(line[2])]
        print line
    

    将此想法扩展到您的问题

    这是您输出所需的吗:

    >>> lines = ['5 4 3 16 22 247 0 1.168 0.911 0.944 3.205 0.000 0.562',
         '6 4 4 17 154 93 309 0 0.930 0.919 0.903 0.917 3.852 0.000 1.419',
         '7 3 2 233 311 0 0.936 0.932 1.874 2.000 -0.807']
    >>> def getInt(lines):
        result = []
        for line in lines:
            items = line.split()
            for i in range(1,int(items[2])+1):
                result.append(items[2+i])
        return result
    
    >>> res = getInt(lines)
    >>> res
    ['16', '22', '247', '17', '154', '93', '309', '233', '311']
    >>> 
    
    要获取每行的详细信息值,可以按如下方式修改代码:

    >>> def getInt(lines):
        result = []
        for line in lines:
            row = []
            items = line.split()
            for i in range(1,int(items[2])+1):
                row.append(items[2+i])
            result.append(row)
        return result
    
    >>> res = getInt(lines)
    >>> res
    [['16', '22', '247'], ['17', '154', '93', '309'], ['233', '311']]
    >>> res[0]
    ['16', '22', '247']
    
    按照你的要求,, 1.我们需要迭代行/行和列中的每个项目,并手动执行,而不使用枚举函数。 2.保持行和列的位置,并比较值 3.在上一个脚本中,我忘记键入cast将值转换为int,请阅读下面代码中的注释

    >>> lines = ['5 4 3 16 22 247 0 1.168 0.911 0.944 3.205 0.000 0.562',
         '6 4 4 17 154 233 309 0 0.930 0.919 0.903 0.917 3.852 0.000 1.419',
         '7 3 2 233 311 0 0.936 0.932 1.874 2.000 -0.807']
    >>> def getInt(lines):
        result = []
        for line in lines:
            row = []
            items = line.split()
            for i in range(1,int(items[2])+1):
                row.append(int(items[2+i])) # old line row.append(items[2+i])
            result.append(row)
        return result
    
    >>> def getPos(result, item):
        row_pos = 0
        for i in result:
            row_pos +=1
            for j in range(len(i)):
                if i[j]==item:
                    print("Item %s found in position : (%s,%s)" % (item, row_pos,j))
    
    >>> res = getInt(lines)
    >>> getPos(res, 22)
    Item 22 found in position : (1,1)
    >>> getPos(res, 233)
    Item 233 found in position : (2,2)
    Item 233 found in position : (3,0)
    

    我希望它能有所帮助。

    谢谢,但我希望将每列数据都列在列表中。但这确实对我有帮助。非常感谢。我可以问最后一个问题吗?我如何接近“res”矩阵的每个元素?我希望为每一行和每一列迭代一些数据块。所以我需要在某些条件下获得特定的元素。谢谢,我将研究itI尝试过但无法获得的元素。还有什么提示吗?问题是,我的数据有整数和浮点数,列数也在变化。这两个事实伤害了我的代码。请帮帮我。你至少能告诉我我读专栏的方式可以吗?