python - How i can make my code shorter?

时间:2018-01-23 19:21:20

标签: python

I have this part of codeprint(int(input())-int(input())), but i'm need to make my code more short, and i'm looking for a way to do it.

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

1 个答案:

答案 0 :(得分:2)

You could use operator.sub with starred unpacking of arguments from 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):

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