简体   繁体   English

在 Python 3 中使用 sys 查找两个排序的整数列表的交集

[英]Find the intersection of two sorted lists of integers using sys in Python 3

If you have done HireVue coding challenges you know that you have inputs that are already in the system and should be called with:如果您完成了 HireVue 编码挑战,您就会知道您的输入已经存在于系统中,应该使用以下命令调用:

import sys (I guess) import sys (我猜)

How should I write the code that reads raw inputs to find the intersection between the 2 lists?我应该如何编写读取原始输入以找到两个列表之间的交集的代码?

An intersection is simply a common value between the 2 lists.交集只是两个列表之间的公共值。

My attempt:我的尝试:

def intersection(lst1, lst2):
    lst3 = [value for value in lst1 if value in lst2]
    return lst3

The problem is that I don't know how to put in the code the command that reads the raw input.问题是我不知道如何将读取原始输入的命令放入代码中。

Just use the intersection operator of a set :只需使用集合交集运算符

def intersection(lst1, lst2):
    return list(set(lst1) & set(lst2))

If you have import sys and you need to take inputs as well on your own, one way that I am aware of is using sys.stdin for input.如果您有import sys并且您也需要自己进行输入,我知道的一种方法是使用sys.stdin进行输入。

sys.stdin is the Standard Input Stream for Python that normally uses the terminal or console running the program for input. sys.stdin是 Python 的标准输入流,通常使用运行程序的终端或控制台进行输入。 But it can be updated to use some different file as well.但它也可以更新为使用一些不同的文件。 It might be the case that your code platform has updated this parameter to reference their own input.可能是您的代码平台已更新此参数以引用他们自己的输入。

With sys.stdin , you have methods that can help you in taking inputs from stdin such as:使用sys.stdin ,您可以使用一些方法来帮助您从 stdin 获取输入,例如:

  • sys.stdin.read() : To read some characters from sys.stdin sys.stdin.read() : 从sys.stdin读取一些字符
  • sys.stdin.readline() : To read a line from sys.stdin sys.stdin.readline() : 从sys.stdin读取一行
  • sys.stdin.readlines() : To read multiple lines from sys.stdin sys.stdin.readlines() : 从sys.stdin读取多行

To check about all its available methods, use import sys; help(sys.stdin)要检查所有可用的方法,请使用import sys; help(sys.stdin) import sys; help(sys.stdin) with Python 3.使用 Python 3 import sys; help(sys.stdin)

Now, for your required code for intersection, [val for val in lst1 if val in lst2] might work as expected, but will take some more time since in operator in lists will look for your val in lst2 in O(n) time, which can be optimised if you use sets, as mentioned in other answers here.现在,对于您所需的交集代码, [val for val in lst1 if val in lst2]可能会按预期工作,但需要更多时间,因为列表in操作符将在O(n)时间内在 lst2 中查找您的 val,如果您使用集合,可以对其进行优化,如此处的其他答案中所述。

I hope it helps.我希望它有帮助。 I was initially planning to leave a comment, but with under 50 reputation for now, I had no other choice but to separately answer to your question.我最初打算发表评论,但现在声望不到 50,我别无选择,只能单独回答你的问题。 Let me know by commenting below if anything is incorrect or unclear here and we might update it.如果此处有任何不正确或不清楚的地方,请通过下面的评论告诉我,我们可能会对其进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM