简体   繁体   English

我如何使我的代码更短?

[英]python - How i can make my code shorter?

I have this part of code print(int(input())-int(input())) , but i'm need to make my code more short, and i'm looking for a way to do it. 我有这部分代码print(int(input())-int(input())) ,但是我需要使代码更简短,我正在寻找一种实现方法。

I think, i can do map(int, input.split(' ') , but i don't know, how to do difference of two elements of list using functions of Python 我认为,我可以做map(int, input.split(' ') ,但是我不知道如何使用Python函数来实现列表的两个元素的区别

You could use operator.sub with starred unpacking of arguments from map 您可以将operator.sub与星号从map解包的星标一起使用

import operator

print(operator.sub(*map(int,"3 1".split())))  # => 2

it's not shorter, but it avoids to access the elements of the splitted list by index, and it's one line. 它并不短,但避免了按索引访问拆分列表的元素,而是一行。

interactive variant with 2 calls to input() (instead of split on one input): 交互变体,其中有两次调用input() (而不是一次输入split ):

operator.sub(*(int(input()) for _ in range(2)))

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

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