在Python中将文件输入行读取到多个列表中

时间:2019-03-08 22:33:44

标签: python list file input formatting

我正在尝试将以下文件中的行读入3个列表,其中第二列(读取到小数点后2位)包含x值,第三列包含y值。当行号(第一列)从头开始时,我想要一个新列表。

1 0.05 0
2 0.1 0
3 0.15 0
4 0.2 0
5 0.25 0
6 0.3 0
7 0.35 0
8 0.4 1
9 0.45 1
10 0.5 2
11 0.55 3
12 0.6 4
13 0.65 5
14 0.7 6
15 0.75 7
16 0.8 8
17 0.85 8
18 0.9 9
19 0.95 10
20 1 11
1 0.047619 0
2 0.0952381 1
3 0.142857 1
4 0.190476 1
5 0.238095 1
6 0.285714 1
7 0.333333 1
8 0.380952 1
9 0.428571 1
10 0.47619 1
11 0.52381 2
12 0.571429 3
13 0.619048 4
14 0.666667 5
15 0.714286 5
16 0.761905 5
17 0.809524 6
18 0.857143 7
19 0.904762 8
20 0.952381 8
21 1 9
1 0.0526316 0
2 0.105263 0
3 0.157895 0
4 0.210526 0
5 0.263158 1
6 0.315789 1
7 0.368421 1
8 0.421053 1
9 0.473684 2
10 0.526316 3
11 0.578947 3
12 0.631579 3
13 0.684211 4
14 0.736842 5
15 0.789474 5
16 0.842105 6
17 0.894737 7
18 0.947368 8
19 1 9

我有以下代码。当我绘制它们时,仅绘制x1和y1。最初,虽然我的绘图代码存在问题,但是当我分别绘制它们时,我发现列表x2,y2,x3,y3是空的,这使我相信列表未正确读取值。

import numpy as np
import matplotlib.pyplot as plt

with open('kmt_oa.txt') as f:
    lines1 = f.readlines()[0:20]
    x1 = [line.split()[1][:4] for line in lines1]
    y1 = [line.split()[2] for line in lines1]
    lines2 = f.readlines()[21:40]
    x2 = [line.split()[1][:4] for line in lines2]
    y2 = [line.split()[2] for line in lines2]
    lines3 = f.readlines()[41:60]
    x3 = [line.split()[1][:4] for line in lines3]
    y3 = [line.split()[2] for line in lines3]
    
ax1 = plt.subplot()
ax1.scatter(x1, y1, color = 'r', label = 'Table size of 20')
ax1.scatter(x2, y2, color = 'g', label = 'Table size of 21')
ax1.scatter(x3, y3, color = 'b', label = 'Table size of 19')

ax1.set_xlabel('Load Factor')
ax1.set_ylabel('Number of Collisions')

ax1.set_title("Key Mod Tablesize & Open Addressing")
ax1.set_ylabel('Number of Collisions')

plt.legend(loc = 'upper left')

plt.show()

3 个答案:

答案 0 :(得分:1)

使用以下代码,您将在lists对象中获得列表列表。

with open(path, 'r') as f:
  lists = []
  current = [[], []]
  card = 0
  for line in f.readlines():
    idx, x, y = line.split()
    if idx < card:
      card = idx
      lists.append(current)
      continue

    card = idx
    current[0].append(int(x * 100) / 100)
    current[1].append(y)

答案 1 :(得分:1)

您可以使用csv.reader读取并生成序列行,使用enumerate函数生成行索引,然后将itertools.groupby与具有返回差异的键函数一起使用在行索引和行号(第一个字段)之间,以便可以按连续的行号序列对列表进行分组(假设您的文件对象f已经打开):

from itertools import groupby
import csv
[[[round(float(r[1]), 2), int(r[2])] for _, r in g] for _, g in groupby(enumerate(csv.reader(f, delimiter=' ')), key=lambda t: t[0] - int(t[1][0]))]

这将返回:

[[[0.05, 0],
  [0.1, 0],
  [0.15, 0],
  [0.2, 0],
  [0.25, 0],
  [0.3, 0],
  [0.35, 0],
  [0.4, 1],
  [0.45, 1],
  [0.5, 2],
  [0.55, 3],
  [0.6, 4],
  [0.65, 5],
  [0.7, 6],
  [0.75, 7],
  [0.8, 8],
  [0.85, 8],
  [0.9, 9],
  [0.95, 10],
  [1.0, 11]],
 [[0.05, 0],
  [0.1, 1],
  [0.14, 1],
  [0.19, 1],
  [0.24, 1],
  [0.29, 1],
  [0.33, 1],
  [0.38, 1],
  [0.43, 1],
  [0.48, 1],
  [0.52, 2],
  [0.57, 3],
  [0.62, 4],
  [0.67, 5],
  [0.71, 5],
  [0.76, 5],
  [0.81, 6],
  [0.86, 7],
  [0.9, 8],
  [0.95, 8],
  [1.0, 9]],
 [[0.05, 0],
  [0.11, 0],
  [0.16, 0],
  [0.21, 0],
  [0.26, 1],
  [0.32, 1],
  [0.37, 1],
  [0.42, 1],
  [0.47, 2],
  [0.53, 3],
  [0.58, 3],
  [0.63, 3],
  [0.68, 4],
  [0.74, 5],
  [0.79, 5],
  [0.84, 6],
  [0.89, 7],
  [0.95, 8],
  [1.0, 9]]]

答案 2 :(得分:1)

您也可以使用不带任何模块的纯Python来做到这一点:

outList = []
with open('data.txt', 'r') as inFile:
   # Create small lists of each individual record.
   content = [elem.split() for elem in inFile.read().split('\n')]
   # Find the indices of these elements whose first number is 1.
   indices = [i for i in range(len(content)) if content[i][0]=='1'] + [len(content)]
   # Remove the first element of each record.
   content = [elem[1:] for elem in content]
   # Create lists, based on the indices above.
   outList = [sum([],content[indices[i]:indices[i+1]]) for i in range(0,len(indices)-1,1)]

for elem in outList:
    for item in elem:
        print(item)
    print()

结果:

['0.05', '0']
['0.1', '0']
['0.15', '0']
['0.2', '0']
['0.25', '0']
['0.3', '0']
['0.35', '0']
['0.4', '1']
['0.45', '1']
['0.5', '2']
['0.55', '3']
['0.6', '4']
['0.65', '5']
['0.7', '6']
['0.75', '7']
['0.8', '8']
['0.85', '8']
['0.9', '9']
['0.95', '10']
['1', '11']

['0.047619', '0']
['0.0952381', '1']
['0.142857', '1']
['0.190476', '1']
['0.238095', '1']
['0.285714', '1']
['0.333333', '1']
['0.380952', '1']
['0.428571', '1']
['0.47619', '1']
['0.52381', '2']
['0.571429', '3']
['0.619048', '4']
['0.666667', '5']
['0.714286', '5']
['0.761905', '5']
['0.809524', '6']
['0.857143', '7']
['0.904762', '8']
['0.952381', '8']
['1', '9']

['0.0526316', '0']
['0.105263', '0']
['0.157895', '0']
['0.210526', '0']
['0.263158', '1']
['0.315789', '1']
['0.368421', '1']
['0.421053', '1']
['0.473684', '2']
['0.526316', '3']
['0.578947', '3']
['0.631579', '3']
['0.684211', '4']
['0.736842', '5']
['0.789474', '5']
['0.842105', '6']
['0.894737', '7']
['0.947368', '8']
['1', '9']