0

有没有更有效的方法来做下面发生的事情?我非常愿意认为有。该脚本没有特别的用途,但如果知道一种更有效的方法来执行此操作,仍然会非常好。

# Divides text into 10 lists.. you'll see what I mean.
# 5/21/2012

filename = "test.txt"

FILE = open(filename,"r")

# READ FILE WORD-BY-WORD:
f = open(filename,"r")
lines = f.readlines()
for i in lines:
    thisline = i.split(" ")

FILE.close()

# DIVIDE INTO 10 LISTS:
list1 = []
list2 = []
list3 = []
list4 = []
list5 = []
list6 = []
list7 = []
list8 = []
list9 = []
list10 = []

j = 0
while j < len(thisline):
    x = thisline[j]
    list1.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list2.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list3.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list4.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list5.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list6.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list7.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list8.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list9.append(x)
    j+=1
    if j >= len(thisline):
        break

    x = thisline[j]
    list10.append(x)
    j+=1
    if j >= len(thisline):
        break

print "list 1 = "," ".join(list1)
print "list 2 = "," ".join(list2)
print "list 3 = "," ".join(list3)
print "list 4 = "," ".join(list4)
print "list 5 = "," ".join(list5)
print "list 6 = "," ".join(list6)
print "list 7 = "," ".join(list7)
print "list 8 = "," ".join(list8)
print "list 9 = "," ".join(list9)
print "list 10 = "," ".join(list10)

# EOF
4

3 回答 3

3

如果这个程序的目的是读取文件的行,将它们分解成单词,然后将单词附加到列表中,这样第 N 个列表包含从第 N 个开始的每 10 个单词,那么这就是以下内容做:

from itertools import izip, cycle

filename = "test.txt"
f = open(filename,"r")

lsts = list([] for _ in range(10))
oracle = cycle(lsts)

for line in f:
    parts = line.split(" ")

    for lst, part in izip(oracle, parts):
        lst.append(part)

f.close()

for index, lst in enumerate(lsts):
    print "list %u = " % (index+1,)," ".join(lst)
于 2012-05-22T05:26:08.727 回答
1

这会填充较短的列表None,如果您愿意,您可以轻松地将它们过滤掉

with open("test.txt") as f:
    result = zip(*map(None, *[(word for line in f for word in line.split())]*10))
于 2012-05-22T06:31:39.367 回答
0

如果您有一个包含十行条目的文件,这将起作用:

line1, line2, line3, line4, line5, line6, line7, line8, line9, line10 = open('test.txt','r').readlines()

您可以将open().readlines()调用替换为执行所需拆分的任何函数。向我们展示您的输入文件的示例将对此有所帮助。

但是,既然可以使用索引序列,为什么还要使用 10 个变量呢?

lines = open('test.txt', 'r').readlines()
assert line1 == lines[0] # etc
于 2012-05-22T05:41:30.497 回答