파이썬 관련 질문입니다. 정보
개발자 파이썬 관련 질문입니다.본문
음 input1.txt 파일과 input2.txt 파일이 있는데요
input1.txt ------------------
chr1 a1 b1 c1
chr2 a2 b2 c2
chr3 a3 b3 c3
. . . .
. . . .
chr10 a10 b10 c10
--------------------------
input2.txt ------------------
chr1 d1 e1 f1
chr11 d2 e2 f2
chr2 d3 e3 f3
chr22 d4 e4 f4
. ...
. . . .
chr55 d10 e10 f10
---------------------------
각 파일의 내용은 이렇습니다.
제가 하고자 하는 작업은
두개의 파일의 배열[0]값을 서로 비교해서
같은값만을 출력하고 나머지
input1의 배열[1]과, 위에서 출력한 값에 해당하는 input2의 배열 [2],[3]을 차례대로 출력하고자 하는것인데요.
그러니까
output ---------------------------
chr1 a1 e1 f1
chr2 a2 e3 f3
chr3 a3 e5 f5
... .
. . . .
chr10 a10 e10 f10
---------------------------------
결과는 이렇게 나와야겠죠
이거는 제가 해본건데요.
그냥 출력하는것밖에 못했습니다..
---------------------------------
f=open("input1.txt")
q=open("input2.txt')
for line in f:
index1=line.strip().split(" ");
for line in q:
index2=line.strip().split(" ");
print index1[0], index1[1],index2[0],index2[3];
f.close();
------------------------------------
if문을 써서 index1[0] 값과 index2[0]값을 비교해서
같으면 같은값만 출력하게하면 될거같은데..... 잘모르겠네요
어떻게 해야될까요??
고수분들의 도움 부탁드립니다..
0 비추천
0
댓글 2개

def table(lines):
def f(x): return (x[0], x[1:])
return dict(f(line.split()) for line in lines)
data1 = table(open('input1.txt').readlines())
data2 = table(open('input2.txt').readlines())
for key in sorted(set(data1) & set(data2)):
print key, data1[key][0], data2[key][1], data2[key][2]