python:遍历文件,每行都有列表列表,在每行找到列表的长度

时间:2019-02-20 20:56:07

标签: python

我有一个包含每一行作为列表列表的文件。我想从文件中读取每一行,并找到列表的长度,并获取相同数量的列表的计数。

输入文件。dat

2

输出:

[['11', '42'], ['11', '46'], ['46', '47']]

[['48', '49'], ['48', '50']]

[['12', '22'], ['33', '46'], ['41', '42']]

[['48', '42']]

我试图用numpy数组读取文件并找到长度。但是我不知道如何逐行读取文件并反复查找长度。有什么办法吗?

3 个答案:

答案 0 :(得分:1)

该示例可以使用ast literal_eval(比简单的eval安全得多)和Counter来计算给定长度的行。

import ast
from collections import Counter

lengths = Counter()

with open("file.dat") as fp:
    for cnt, line in enumerate(fp.readlines()):
        ll = ast.literal_eval(line)
        print(f"length of list {cnt+1} = {len(ll)}")
        lengths.update([len(ll)])

for lng, num in lengths.items():
    print(f"number of list with length {lng} = {num}")

输出:

  

列表1的长度= 3
  清单2的长度= 2
  清单3的长度= 3
  清单4的长度= 1
  长度为3 = 2的列表数
  长度为2 = 1的列表数
  长度为1 = 1的列表数

答案 1 :(得分:0)

def main():
    sizeIndex = {}
    with open("/tmp/file.dat") as f:
        count = 1
        while True:
            line = f.readline()
            if not line:
                break
            lineList = eval(line)
            listLength = len(lineList)
            print "length of list @line{} = {}".format(count, listLength)
            if listLength not in sizeIndex:
                sizeIndex[listLength] = []
            sizeIndex[listLength].append(listLength)
            count += 1
    for x in sorted(sizeIndex.keys()):
        print "number of list with length {} = {}".format(x, len(sizeIndex[x]))

main()

输出:

length of list @line1 = 3
length of list @line2 = 2
length of list @line3 = 3
length of list @line4 = 1
number of list with length 1 = 1
number of list with length 2 = 1
number of list with length 3 = 2

答案 2 :(得分:0)

假设数据文件的每一行都有正确格式的列表,即每行的开头是一个[[],而结尾是一个[]。再加上一行中的每个列表也正确使用了'['和']',那么一个更简单的解决方案是只计算每行中[[]的数量(并减少1)来确定该行中的列表数量。 / p>

以下是基于上述假设的代码:

filename = 'file.dat'
rst = {}

with open(filename, 'r') as f:
    for i, l in enumerate(f):
        count = l.count('[') - 1
        if count in rst:
            rst[count] += 1
        else:
            rst[count] = 1
        print('length of list @line%d = %d' % (i + 1, count))

for k in sorted(rst):
    print('number of list with length %s = %d' % (k, rst[k]))

输出:

length of list @line1 = 3
length of list @line2 = 2
length of list @line3 = 3
length of list @line4 = 1
number of list with length 1 = 1
number of list with length 2 = 1
number of list with length 3 = 2