file2_dict = {}
with open("file2.txt") as fo:
file_contents = fo.read()
for line in file_contents.split("\n"):
if not line.strip():
continue
key, value = line.strip().split(" ", 1)
if key not in file2_dict:
file2_dict[key] = []
file2_dict[key].append(int(value))
output_string = ""
with open("file1.txt") as fo:
file_contents = fo.read()
for line in file_contents.split("\n"):
if not line.strip():
continue
name, id, min, max = line.strip().split(" ")
for value in file2_dict.get(id, []):
if int(min) < value < int(max):
output_string += line.replace(id, "%s %d" % (id, value))
output_string += "\n"
print output_string
import sys
def process_files(one, two):
for line in [line.strip().split(" ") for line in open(two, 'r').readlines()]:
for x in filter(lambda x: x[1] == line[0], [z.strip().split(" ") for z in open(one, 'r').readlines()]):
if int(x[2]) <= int(line[1]) <= int(x[3]):
print(" ".join(x))
if __name__ == "__main__":
process_files(sys.argv[1], sys.argv[2])
我将使用
dict
获取文件2中的所有可用索引,然后遍历文件1。你知道吗我会使用像Math这样的解决方案,除了用于文件读取的
with
语句。你知道吗尝试以下操作:
相关问题 更多 >
编程相关推荐