I have to write a program that, when given numbers from the command line, manages to read and print the highest number.
我必须编写一个程序,当从命令行给出数字时,它会设法读取并打印最高数字。
import sys
numbers = sys.argv[1]
def map(f,items):
result = []
for i in range(0,len(items),1):
result = result + [f(items[i])
return result
im trying to find the easiest way to go about doing this! Thank you!
我试图找到最简单的方法去做这个!谢谢!
2 个解决方案
#1
7
import sys
print(max(float(x) for x in sys.argv[1:]))
Alternatively, you can use print(max(sys.argv[1:], key=float))
(thanks to the nice addition by frostnational).
或者,你可以使用print(max(sys.argv [1:],key = float))(感谢frnational添加了很多)。
Demo:
演示:
>> python a.py 12 6 3.14 11
12.0
Explanation: If we add print(sys.argv)
to the script, the output for the above example will be
说明:如果我们将print(sys.argv)添加到脚本中,则上面示例的输出将是
['a.py', '12', '6', '3.14', '11']
12.0
The solution is slicing off the first element of sys.argv
, converting the other strings to floats in a generator expression, and then finding the maximum.
解决方案是切掉sys.argv的第一个元素,将其他字符串转换为生成器表达式中的浮点数,然后找到最大值。
#2
1
@timgeb answer is very good but i would also check argparse if you are using the command line interface it can give you some very good option for your current and future code, by using argparse
you can save a lot of programming time and get a full help and usage messages.
@timgeb答案非常好,但我也会检查argparse如果你使用命令行界面它可以为你当前和未来的代码提供一些非常好的选择,通过使用argparse你可以节省大量的编程时间并获得一个完整的帮助和使用信息。
I'll take the example form the docs since it's relevant for your question, this code will print either max or sum.
我将以文档为例,因为它与您的问题相关,此代码将打印max或sum。
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
For max: just call the code with a list of numbers.
对于max:只需使用数字列表调用代码即可。
For sum: or any other option you would like to add you can use an argument, in this case --sum
对于sum:或者您想要添加的任何其他选项,您可以使用参数,在本例中为-sum
#1
7
import sys
print(max(float(x) for x in sys.argv[1:]))
Alternatively, you can use print(max(sys.argv[1:], key=float))
(thanks to the nice addition by frostnational).
或者,你可以使用print(max(sys.argv [1:],key = float))(感谢frnational添加了很多)。
Demo:
演示:
>> python a.py 12 6 3.14 11
12.0
Explanation: If we add print(sys.argv)
to the script, the output for the above example will be
说明:如果我们将print(sys.argv)添加到脚本中,则上面示例的输出将是
['a.py', '12', '6', '3.14', '11']
12.0
The solution is slicing off the first element of sys.argv
, converting the other strings to floats in a generator expression, and then finding the maximum.
解决方案是切掉sys.argv的第一个元素,将其他字符串转换为生成器表达式中的浮点数,然后找到最大值。
#2
1
@timgeb answer is very good but i would also check argparse if you are using the command line interface it can give you some very good option for your current and future code, by using argparse
you can save a lot of programming time and get a full help and usage messages.
@timgeb答案非常好,但我也会检查argparse如果你使用命令行界面它可以为你当前和未来的代码提供一些非常好的选择,通过使用argparse你可以节省大量的编程时间并获得一个完整的帮助和使用信息。
I'll take the example form the docs since it's relevant for your question, this code will print either max or sum.
我将以文档为例,因为它与您的问题相关,此代码将打印max或sum。
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
For max: just call the code with a list of numbers.
对于max:只需使用数字列表调用代码即可。
For sum: or any other option you would like to add you can use an argument, in this case --sum
对于sum:或者您想要添加的任何其他选项,您可以使用参数,在本例中为-sum