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

时间:2015-02-04 17:27:02

标签: python list loops matrix

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

结束了。

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

8 个答案:

答案 0 :(得分:3)

对我来说就像你真正需要的是:

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方法会处理使用newline自动传入的readlines()个字符。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您可以尝试下面的内容。

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

    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]

答案 5 :(得分:0)

让我们说我们通过考虑有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()

答案 6 :(得分:0)

看起来您正好读取文件,所以我们只需指定

即可
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是您发布的所需输出

答案 7 :(得分:0)

你可以使用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()

处理多条线路。