在python中查找两个整数之间的值

2024-04-19 01:00:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下两个文件。你知道吗

  1. 如果文件1的第二列与文件2的第一列匹配
  2. 如果文件2的第二列位于文件1的第三列和第四列的间隔内,则按输出中的方式打印。你知道吗

我试图为此编写一个python脚本,但我不知道如何表达它。你知道吗

File 1:

a2 sca4 15 20
b3 sca4 22 30 
c4 sca6 45 65

File 2:

sca4 17
sca4 18
sca4 19
sca6 46

output:

a2 sca4 17 15 20
a2 sca4 18 15 20
a2 sca4 19 15 20
c4 sca6 46 45 65

Tags: 文件脚本a2output间隔方式fileb3
3条回答

我将使用dict获取文件2中的所有可用索引,然后遍历文件1。你知道吗

ff = file("file2", "r")
file2 = ff.readlines()
ff.close()
dict_f2 = dict()
for line in file2:
   ll = line.strip().split(" ")
   if (ll[0] in dict_f2):
       dict_f2[ll[0]].append(int(ll[1]))
   else:
       dict_f2[ll[0]] = list()
       dict_f2[ll[0]].append(int(ll[1]))

ff = open("file1", "r")
file1 = ff.readlines()
ff.close()
for line in file1:
    ll = line.strip().split(" ")
    if (ll[1] in dict_f2):
        sup = int(ll[4])
        inf = int(ll[3])
        for (comp in dict_f2[ll[1]]):
            if (comp >= inf and comp <= sup):
                print(line[0:2] + str(comp) + line[2:])

我会使用像Math这样的解决方案,除了用于文件读取的with语句。你知道吗

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])

相关问题 更多 >