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

Python 将数据读取到内部列表

Python 将数据读取到内部列表,python,list,Python,List,在文本文件中,数据为: # 2.1,-3.1 -0.7,4.1 # 3.8,1.5 -1.2,1.1 我想将其读入嵌套列表,该列表应具有如下格式的数据: [[[2.1,-3.1],[-0.7,[4.1]],[[3.8,1.5],[-1.2,1.1]]] 也就是说, 每个#开始一个内部列表 每一行都会启动一个内部列表。 下一个#关闭上一个内部列表并创建新的内部列表 数据也是字符串格式,需要转换为浮点值。我编写了以下代码: def read_weights(file_name): # 1 Ma

在文本文件中,数据为:

#
2.1,-3.1
-0.7,4.1
#
3.8,1.5
-1.2,1.1
我想将其读入嵌套列表,该列表应具有如下格式的数据:

[[[2.1,-3.1],[-0.7,[4.1]],[[3.8,1.5],[-1.2,1.1]]]
也就是说,
每个#开始一个内部列表
每一行都会启动一个内部列表。
下一个#关闭上一个内部列表并创建新的内部列表

数据也是字符串格式,需要转换为浮点值。我编写了以下代码:

def read_weights(file_name):  # 1 Mark
    file = open(file_name)
    res = []
    for line in file:
        m = []
        if '#' in line.strip():
            pass
        else:
            x = line.split()
            for i in x:
                y = i.split(',')
                i = 0
                for c in y:
                    m.insert(i, float(c))
                    i = i + 1
        if m != []:
            res .append(m)
    file.close()
    return res
w_example = read_weights('example_weights.txt')
print(w_example)
输出为:

[[2.1, -3.1], [-0.7, 4.1], [3.8, 1.5], [-1.2, 1.1]]

每次#之后,我都无法创建内部列表。

这是一种使用负索引访问最后插入列表的方法

Ex:

result = []
with open(filename) as infile:
  for line in infile:
    line = line.strip()
    if line == "#":
      result.append([])
    else:
      line = list(map(float, line.split(",")))
      result[-1].append(line)
print(result)
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
输出:

result = []
with open(filename) as infile:
  for line in infile:
    line = line.strip()
    if line == "#":
      result.append([])
    else:
      line = list(map(float, line.split(",")))
      result[-1].append(line)
print(result)
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]

这是一种使用负索引访问上次插入列表的方法

Ex:

result = []
with open(filename) as infile:
  for line in infile:
    line = line.strip()
    if line == "#":
      result.append([])
    else:
      line = list(map(float, line.split(",")))
      result[-1].append(line)
print(result)
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
输出:

result = []
with open(filename) as infile:
  for line in infile:
    line = line.strip()
    if line == "#":
      result.append([])
    else:
      line = list(map(float, line.split(",")))
      result[-1].append(line)
print(result)
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]

根据拉凯什先生的建议,我做了一些修改 我的代码也能工作。 def读取权重(文件名): 文件=打开(文件名) res=[] 对于文件中的行: line=line.strip(); m=[] 如果行中有“#”: res.append([]) 其他: x=行。拆分() 对于x中的i: y=i.split(',') i=0 对于y中的c: m、 插入(i,浮动(c)) i=i+1 如果m!=[]: res[-1]。追加(m) file.close()文件 返回res


谢谢你,拉凯什先生我根据拉凯什先生的建议做了一些修改 我的代码也能工作。 def读取权重(文件名): 文件=打开(文件名) res=[] 对于文件中的行: line=line.strip(); m=[] 如果行中有“#”: res.append([]) 其他: x=行。拆分() 对于x中的i: y=i.split(',') i=0 对于y中的c: m、 插入(i,浮动(c)) i=i+1 如果m!=[]: res[-1]。追加(m) file.close()文件 返回res


谢谢你,拉凯什先生

非常感谢。我在考虑地图,但避免更改代码。。这段代码很紧凑,更有意义。此外,在打开(文件名)时,无需关闭file.def read_weights(file_name):#1标记file=open(file_name)res=[]文件中的行:line=line.strip();m=[]如果行中的“#”为:res.append([])否则:x=line.split()对于x中的i:y=i.split(',')对于y中的c:m.insert(i,float(c))i=i+1如果m!=[]:res[-1].append(m)file.close()返回大量restanks。我在考虑地图,但避免更改代码。。这段代码很紧凑,更有意义。此外,在打开(文件名)时,无需关闭file.def read_weights(file_name):#1标记file=open(file_name)res=[]文件中的行:line=line.strip();m=[]如果行中的“#”为:res.append([])否则:x=line.split()对于x中的i:y=i.split(',')对于y中的c:m.insert(i,float(c))i=i+1如果m!=[]:res[-1]。追加(m)文件。关闭()返回res