用于读取文件并显示输出的Python脚本

用于读取文件并显示输出的Python脚本,python,python-3.x,filereader,Python,Python 3.x,Filereader,编写了一个python脚本,可从文件input.txt读取数据 input.txt 2 //number of test cases 2 4 //testcase 1 ->'2' is size of 1st array and 4 is size of 2nd array 6 10 6 7 8 9 //testcase 1 -> 1st array is [6,10

编写了一个python脚本,可从文件input.txt读取数据

input.txt

    2                     //number of test cases
    2 4                   //testcase 1 ->'2' is size of 1st array and 4 is size of 2nd array
    6 10 6 7 8 9          //testcase 1 -> 1st array is [6,10] and 2nd array is [6,7,8,9]
    1 3                   //testcase 2 ->'1' is size of 1st array and 3 is size of 2nd array
    7 7 8 14              //testcase 2 -> 1st array is [7] and 2nd array is [7,8,14]
文件中的第一行表示测试用例的数量。在这个例子中,我们有两个测试用例每个测试用例有2行要处理-其中第一行表示第一个数组的大小和第二个数组的大小。第二行表示两个阵列的详细信息

即,在上面的示例中,第2行表示testcase1的第一个数组和第二个数组的大小。第3行表示testcase1中提到的2个数组的大小。第4行表示testcase2中第一个数组和第二个数组的大小。第5行表示testcase2中提到的2个数组大小

对于每个测试用例,我需要检查第一个数组的元素是否存在于第二个数组中。我写了下面的程序,但它只对1个测试用例执行(即,我通过给出检查I==0来手动检查第2行和第3行)

有人能帮我吗

输出将类似于

The result is : yes
The result is : no
The result is : yes
试试这个

first_array=[1,2,3]
sec_array=[4,5,3]
print(any(i in sec_array for i in first_array)) # prints True

我会逐行阅读文件并进行处理

# read a line and convert to int() for num_cases.
for i in range(0, 2 * num_cases, 2):
  # read a line for split_list
  # check length of split_list
  # read a line for list values all_nums
  # check length of all_nums to be equal to sum of split_list
  # split the list into arr1 and arr2.
  # You can use slicing notation.
  # For example:
  # arr[:5] are the first 5 elements of arr. i.e. indexes 0, 1, 2, 3, 4
  # arr[:-5] are the last 5 elements of arr. i.e. indexes len(arr) - 5, ... , len(arr) - 1
  # arr[5:] are elements 5 and on from arr. i.e. indexes 5, 6, ...
  for value in arr1:
    if value in arr2:
      # output
    else:
      # output
输入错误时,函数int(x)将引发ValueError

通过检查列表的长度,可以确保在每行上获得预期数量的值

如果文件的行数不足,readline()将返回一个空字符串,在拆分时将生成一个长度为0的列表,长度检查将失败


您可以使用try、catch编写代码来处理错误,例如int()中的ValueError。

您知道abt any()函数吗?这有助于正确搜索?该程序只适用于1个测试用例。也就是说,它将只处理第二和第三行。我想要的是在超过1个测试用例的情况下,我必须处理第4个、第5个。。。线。目前,我正在检查(I==0)->是否有任何方法来检查每一行?您的预期输出是什么?使用预期输出修改问题。对于testcase1,它将打印结果为:是结果为:否(在数组[6,7,8,9]>6存在,而10不存在)。对于TestCase2,它将打印结果为:是(在数组[7,8,14]>7存在)是否需要检查输入文件中是否存在错误?例如,缺少行?没有足够数组值的行?感谢您的帮助。搜索功能我可以使用此功能进行修改。我要做的是处理文件中与第一行相关的所有行(如果第一行=2,则有2个测试用例。我必须通过{2,3}和{4,5}行进行处理。)在这种情况下。您的if语句即
if i==0
if i==1
正在显式地查看它读取的前两行。您可以将它们更改为
if i%2==0
if i%2==1
以查看奇偶数行。
# read a line and convert to int() for num_cases.
for i in range(0, 2 * num_cases, 2):
  # read a line for split_list
  # check length of split_list
  # read a line for list values all_nums
  # check length of all_nums to be equal to sum of split_list
  # split the list into arr1 and arr2.
  # You can use slicing notation.
  # For example:
  # arr[:5] are the first 5 elements of arr. i.e. indexes 0, 1, 2, 3, 4
  # arr[:-5] are the last 5 elements of arr. i.e. indexes len(arr) - 5, ... , len(arr) - 1
  # arr[5:] are elements 5 and on from arr. i.e. indexes 5, 6, ...
  for value in arr1:
    if value in arr2:
      # output
    else:
      # output