整體思想是可以先獲取每個文件的key,然後將相等的輸出到list中,隨後將第一個文件搞成dict,遍歷1、2文件合成的list,然後和dict的key對比,如果相等則輸出對應的value #!/usr/bin/python import json file = '/Users/jian.wang13/code/test/wenjian1.txt' file2 = '/Users/jian.wang13/code/test/wenjian2.txt' wenjian1_list = [] with open(file,'r') as f: for i in f: tmp = i.split(' ') # print(type(tmp)) # print(tmp[0]) # print(type(tmp)) wenjian1_list.append(tmp[0]) print('###################################') wenjian2_list = [] with open(file2,'r') as f2: for j in f2: tmp = j.strip() wenjian2_list.append(tmp) xiangdeng_list = [] for i in wenjian1_list: for j in wenjian2_list: if i == j: xiangdeng_list.append(i) # print(i) print('@@@@@@@@@@@@@@@@@@@@@@@@@@') #str、lsit、dict間的互相轉換 wenjian3_list = [] with open(file,'r') as f3: for i in f3: tmp = i.split(' ') # d_tmp = json.loads(tmp) wenjian3_list.append(tmp[1]) #print(wenjian3_list) d = zip(wenjian1_list,wenjian3_list) #print(dict(d)) dict1 = dict(d) for i in xiangdeng_list: for j in dict1.keys(): if i == j: print(dict1[j])