Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较两个整数列表_Python_String_Comparison - Fatal编程技术网

Python 比较两个整数列表

Python 比较两个整数列表,python,string,comparison,Python,String,Comparison,我有两个txt文件,如下所示: fileA: fileB: 0 0 5 0 0 80 20 10 600 34 我需要比较这两个列表中的数字在列表中的位置。我需要生成一个输出文件,对每一行进行比较,如: Output: E A B A A 我试过这样的方法: lineA = [lineb.rstrip('\n') for lineb in open("fileA.txt")] l

我有两个txt文件,如下所示:

fileA:      fileB:
0           0   
5           0 
0           80
20          10 
600         34
我需要比较这两个列表中的数字在列表中的位置。我需要生成一个输出文件,对每一行进行比较,如:

Output:
E
A
B
A
A
我试过这样的方法:

lineA = [lineb.rstrip('\n') for lineb in open("fileA.txt")]
lineB = [lineb.rstrip('\n') for lineb in open("fileB.txt")]
for i in lineA:
    for u in lineB:
        if lineA[i] > lineB[i]:
           print("A")
        elif lineA[i] < lineB[i]:
           print("B")
        elif lineA[i] == lineB[i]:
           print("E")
但我无法解决问题…

使用内置函数并行迭代两个列表:

# use `with` to automatically close the files after reading
with open("fileA.txt") as file_a, open("fileB.txt") as file_b:
    # use `int` in the list comprehension
    # and, use `.rstrip()` if you just want to remove whitespace
    lineA = [int(line.rstrip()) for line in file_a]
    lineB = [int(line.rstrip()) for line in file_b]

for i, u in zip(lineA, lineB):
    if i > u:
       print("A")
    elif i < u:
       print("B")
    else:
       print("E")
lineA=[lineb.rstrip('\n'),用于打开的lineb(“fileA.txt”)]
lineB=[lineB.rstrip('\n'),用于打开的lineB(“fileB.txt”)]
对于拉链中的a、b(直线a、直线b):
如果a>b:
打印(“A”)
如果a
您还可以避免一次比较,因为比较两个数字时有三种情况
a
b

  • a
    大于
    b
  • a
    小于
    b
  • a
    等于
    b
lineA=[lineb.rstrip('\n'),用于打开的lineb(“fileA.txt”)]
lineB=[lineB.rstrip('\n'),用于打开的lineB(“fileB.txt”)]
对于拉链中的a、b(直线a、直线b):
如果a>b:
打印(“A”)
如果a

也可以考虑使用<代码> 按其他答案建议打开文件。

< p>可以使用<代码> zip 并行地循环这两个列表:

# use `with` to automatically close the files after reading
with open("fileA.txt") as file_a, open("fileB.txt") as file_b:
    # use `int` in the list comprehension
    # and, use `.rstrip()` if you just want to remove whitespace
    lineA = [int(line.rstrip()) for line in file_a]
    lineB = [int(line.rstrip()) for line in file_b]

for i, u in zip(lineA, lineB):
    if i > u:
       print("A")
    elif i < u:
       print("B")
    else:
       print("E")
#使用`with`在读取后自动关闭文件
打开(“fileA.txt”)作为文件,打开(“fileB.txt”)作为文件:
#在列表中使用'int'
#如果只想删除空白,请使用“.rstrip()”
lineA=[int(line.rstrip())表示文件_a中的行]
lineB=[int(line.rstrip())表示文件_b中的行]
对于拉链中的i,u(直线A,直线B):
如果i>u:
打印(“A”)
如果i
为什么不直接使用
zip
将每个数字相互比较,并将
映射到每个文件:

with open("fileA.txt") as file_a, open("fileB.txt") as file_b:
    for a, b in zip(file_a, file_b):
        a, b = map(int, (a, b))
        if a > b:
            print("A")
        elif a < b:
            print("B")
        else:
            print("E")
打开(“fileA.txt”)作为文件,打开(“fileB.txt”)作为文件:
对于zip中的a、b(文件a、文件b):
a、 b=映射(int,(a,b))
如果a>b:
打印(“A”)
如果a
您可以将
地图
邮政编码一起使用:

def compare(z):
    a, b = z

    if a == b:
        return 'E'

    if a > b:
        return 'A'

    return 'B'

with open("fileA.txt") as file_a, open("fileB.txt") as file_b:
    a_nums = map(int, map(str.rstrip, file_a.readlines()))
    b_nums = map(int, map(str.rstrip, file_b.readlines()))


    for greater in map(compare, zip(a_nums, b_nums)):
        print(greater)
输出:

E
A
B
A
A
要写入输出文件,您可以使用:

with open("fileA.txt") as file_a, open("fileB.txt") as file_b, open("output.txt", 'w') as output:
    a_nums = map(int, map(str.rstrip, file_a.readlines()))
    b_nums = map(int, map(str.rstrip, file_b.readlines()))

    for greater in map(compare, zip(a_nums, b_nums)):
        print(greater, file=output)

在两个列表上并行使用
zip
函数循环