Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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_Unix_Awk - Fatal编程技术网

Python 比较列并打印更小和更大的行

Python 比较列并打印更小和更大的行,python,unix,awk,Python,Unix,Awk,我有两个这样的文件 文件1有1行: 6 4 13 25 35 50 65 75 and so on..... 文件2中有一行 24 45 76 and so on..... 我希望获取file2中的每个值(一次一个),并与file1进行比较,如果file1的值小于该值,则获取这些值并将其保留在列表中,然后根据数字对其进行排序并打印最大值 例如: 我在文件2中取了24个数字,并与文件1进行比较,发现6、4和13都低于该数字,然后我提取它们并将其保存在列表中并对其排序,然后打印最

我有两个这样的文件

文件1有1行:

6
4 
13 
25 
35 
50  
65 
75 
and so on.....
文件2中有一行

24
45
76
and so on.....
我希望获取file2中的每个值(一次一个),并与file1进行比较,如果file1的值小于该值,则获取这些值并将其保留在列表中,然后根据数字对其进行排序并打印最大值

例如: 我在文件2中取了24个数字,并与文件1进行比较,发现6、4和13都低于该数字,然后我提取它们并将其保存在列表中并对其排序,然后打印最大值(即13)

awk解决方案:

awk 'NR==FNR{a[$0];next} {b[FNR]=$0}
END{
        n=asort(b)
        for(j in a)
                for(i=n;i>0;i--)
                        if(b[i]<j){
                                print "for "j" in file2 we found : "b[i]
                                break
                        }
}' file2 file1
注意:有优化的空间。如果性能是关键的,你可以考虑(只是建议)

  • 按降序对文件1排序
  • 对文件2进行升序排序
  • 从已排序的文件2中取第一个,从文件1中遍历文件1。首先,当找到较小的文件时,记录位置/索引
    x
  • 从排序后的文件2中,从
    file1.x
    开始比较第二个文件,找到正确的文件后,更新
    x
  • 直到文件2结束
暴力方式将采取
O(mxn)
O(nxm)
取决于
n
m
哪个更大

上面的算法。。。我没有分析,应该比
O(mxn)


python和awk都可以完成这项工作。如果可能,将这两个文件加载到内存中。如果你有怪物文件,那是另一个算法问题。e、 g.对大文件进行排序

将每个文件读入一个
列表
,将每一行转换为
int
。然后对两个列表进行排序,使我们能够高效地进行迭代

file1 = sorted([int(l) for l in open('file1.txt').read().split()])
file2 = sorted([int(l) for l in open('file2.txt').read().split()])

i = 0
for file2_number in file2:
    while i+1 < len(file1) and file1[i+1] < file2_number:
        i += 1
    print file1[i]
file1=sorted([int(l)表示打开('file1.txt').read().split()]))
file2=已排序([int(l)表示打开中的l('file2.txt').read().split()]))
i=0
对于文件2中的文件2\u编号:
而i+1

当前,它会打印答案(
13 35 75
),但如果需要,您可以轻松修改它以返回一个
列表。

使用Python,首先将file1中的所有行和file2中的所有行读入两个单独的列表,然后您可以简单地遍历它们,将文件1中的每个数字与文件2中的每个数字进行比较,如下所示:

#First load each of the lines in the data files into two separate lists
file1Numbers = [6, 4, 13, 25, 35, 50, 65, 75]
file2Numbers = [24, 45, 76]
extractedNumbers = []

#Loops through each number in file2
for file2Number in file2Numbers:

    #Loops through each number in file 
    for file1Number in file1Numbers:

        #Compares the current values of the numbers from file1 and file2
        if (file1Number < file2Number):

            #If the number in file1 is less than the number in file2, add
            #the current number to the list of extracted numbers
            extractedNumbers.append(file1Number)


    #Sorts the list of extracted numbers from least to greatest
    extractedNumbers.sort()

    #Prints out the greater number in the list
    #which is the number located at the end of the sorted list (position -1)
    print extractedNumbers[-1]
#首先将数据文件中的每一行加载到两个单独的列表中
file1Numbers=[6,4,13,25,35,50,65,75]
file2Numbers=[24,45,76]
ExtractedNumber=[]
#循环遍历文件2中的每个数字
对于file2Number中的file2Number:
#循环遍历文件中的每个数字
对于file1Number中的file1Number:
#比较文件1和文件2中数字的当前值
如果(file1Number
它们是列还是行?对不起,行,我刚刚编辑过,每个文件是否在数据文件的每一行(“行”)上都包含一个数字?能否显示给定输入的预期输出?是的,每个文件每一行都有一个数字这肯定是最直观的解决方案,但将在
O(n^2)中运行
因为它将第一个列表的每个元素与第二个列表进行比较(+排序的开销
提取的数字
而不仅仅是保留最大值)。不是处理长文件的最佳方法。我以前也尝试过类似的方法,但在获取数字时感到困惑,但现在我得到了,谢谢,我得到了输出文件的相同值,因为我有大文件,这是一个很好的观点。就大数据集的效率而言,我提出的方法不会很快,但它只是解决问题的一种相对简单的方法。谢谢你的提示,如果得到预期的结果,我会告诉你一次,我有一个类似的问题现在file1中有相同的6 4 13 25 35 50 65 75行,file2中有2,3,7行等等。现在我想取一行file2,搜索高于该数字的数字,并打印最低最高数字,例如输出:4 4 4 13等。@user2133307只需将while循环更改为
,而i+1#First load each of the lines in the data files into two separate lists
file1Numbers = [6, 4, 13, 25, 35, 50, 65, 75]
file2Numbers = [24, 45, 76]
extractedNumbers = []

#Loops through each number in file2
for file2Number in file2Numbers:

    #Loops through each number in file 
    for file1Number in file1Numbers:

        #Compares the current values of the numbers from file1 and file2
        if (file1Number < file2Number):

            #If the number in file1 is less than the number in file2, add
            #the current number to the list of extracted numbers
            extractedNumbers.append(file1Number)


    #Sorts the list of extracted numbers from least to greatest
    extractedNumbers.sort()

    #Prints out the greater number in the list
    #which is the number located at the end of the sorted list (position -1)
    print extractedNumbers[-1]