下面的代码段接受两个参数,比较其中包含的值并打印任何匹配项
from sys import argv
print(set(argv[1]) & set(argv[2]))
#Output: python multicompare.py man fan
#{'n', 'a'}
一个人如何适应这种情况来处理一个未知的和潜在的无限量(或非常大)的论点?我尝试对参数进行迭代并将它们传递给函数,但是在没有已知数量的参数的情况下,如何对所有参数调用set()并引用它们? 期望:
#Output: python3 multicompare.py man fan dan tan han
# {'n', 'a'}
#Output: python3 multicompare.py man fan dan can fin tin mountain happen trappen
# {'n'}
您可以迭代
sys.argv
并使用&=
运算符。如果用参数man fan dan can fin tin mountain happen trappen
调用它:然后打印:
使用^{} 的^{} 方法,实际上不需要将参数转换为集合(当然,除了一个)
所以你可以这样做:
相关问题 更多 >
编程相关推荐