处理两个列表,逐行迭代并连接值

Processing two lists, iterating line by line and join values

w = open("input1.txt", "r")
f = open("input2.txt", "r")
for line1 in w:
    words1 = line1.split()
    for line2 in f:
        words2 = line2.split()
        print (words1[0]+" "+words1[1]+" "+words1[2]+" "+words1[3]+" "+words1[4]+" "+words1[5]+";"+words2[0])

f.close()
w.close()

我在每个文本文件中都有一个列表:input1.txt 和 input2.txt

input1.txt: 1 2 3

input2.txt: a b c

我正在尝试加入每个元素与另一个元素配对的列表。 所以,输出应该是:

1a
1b
1c 
2a 
2b 
2c 
3a 
3b 
3c

使用上面的代码,我最多只能做到:

1a
1b
1c

结束。

我怎样才能让它选择下一行并重新做同样的事情?

在我看来你真正需要的是这个:

w = open("input1.txt", "r").read()
f = open("input2.txt", "r").read()

for number in w.split():
    for letter in f.split():
        print number, letter

f.close()
w.close()

只需拆分结果并正常迭代两者。您将获得配对的数字和字母,而无需您已经创建的所有额外 (?) 代码。

已编辑以反映来自 OP

的新信息

要执行您要求的操作,请像这样使用 readlines()

f = open("input2.txt", "r").read().split()
w = open("input1.txt", "r").readlines()

for timestamp in w:
    for letter in f:
        print timestamp.rstrip(), letter

rstrip 方法将处理使用 readlines().

自动传入的 newline 个字符
    w = open("input1.txt", "r").read().split()
    f = open("input2.txt", "r").read().split()
    final_list = []
    for number in w:
        final_list += map(lambda:x+number,f)

    f.close()
    w.close()

   print final_list
   >>> [1a,1b,1c,2a,2b,2c,3a,3b,3c]
s1="1 2 3"
s2 = "a b c"
s1 = s1.split()
s2 = s2.split()
for a in s1:
    for b in s2:
        print(a+b)
1a
1b
1c
2a
2b
2c
3a
3b
3c

您可以试试下面的方法。

w = open('input1', 'r')
f = open('input2', 'r')
for line1 in w:
    words1 = line1.split()
for line2 in f:
    words2 = line2.split()
for z in words1:
    for y in words2:
        print(z+y)
f.close()
w.close()

输出:

1a
1b
1c
2a
2b
2c
3a
3b
3c

假设我们通过考虑有 2 个字符串列表 ab 来简化问题,现在您想在这些列表中创建所有组合,那么您可以简单地使用 :

a = ["1", "2", "3"]
b = ["a", "b", "c"]
for i in a:
    for j in b:
        print i+j

现在的问题是从文本文件中读取列表内容。

w = open("input1.txt", "r")
f = open("input2.txt", "r")
line1 = w.read()
line2 = f.read()
for word1 in line1.split():
    for word2 in line2.split():
        print word1+word2

f.close()
w.close()
with open("input1.txt", "r") as f1, open("input2.txt", "r") as f2:
    a = f1.readline().split()
    b = f2.readline().split()

for i in a:
    for x in b:
        print "{}{}".format(i,x)

输出:

1a
1b
1c   
2a
2b 
2c
3a
3b
3c

看起来你读的文件很好所以让我们分配

f=['a','b','c']
w=[1,2,3]

那么像这样的东西应该可以工作:

out=[] #blank list
for num in w:
   for let in f:
      out.append(str(num)+let)

out 是您发布的所需输出

那么你可以使用 itertools 中的 izip,如下所示:-

import itertools
w = open("input1.txt", "r")
f = open("input2.txt", "r")

numbers_list = w.readlines()
letters_list = f.readlines()
result = []
for index, numbers in enumerate(numbers_list):
    numbers = numbers.strip('\n').split(' ')
    letters = letters_list[index]
    letters = letters.strip('\n').split(' ')
    for letter in letters:
        paired_tuples= itertools.izip_longest(numbers, '', fillvalue=letter)
        result += [a+b for a,b in paired_tuples]
print result

f.close()
w.close()

同时处理多行。