반응형

1. Using a list comprehension

a, b, c, d = [int(x) for x in input().split()] 
print(a*b*c*d)

리스트 내포함수를 이용하여 리스트 내부에서 인풋을 모두 받아버린다.

 

- input()을 받고 공백을 기준으로 split하여 ["1 2 3 4"] -> ["1", "2", "3", "4"]

- 나온 x를 int(x) 로 변환해준다. [1,2,3,4]

- 이때 각 리스트에 값 순서대로 a, b, c, d에 들어가게 된다. [a = 1, b = 2, c = 3, d = 4]

 

 

Method 2: Using the map function

a, b, c, d = map(int, input().split())
print(a*b*c*d)

 

- input() 을 통해 표준 입력을 받는다. (1 2 3 4)

- 그 후 split를 통해 공백을 제거하여준다. ["1", "2", "3", "4"]

- 이제 int를 통해 형변환을 해준다. [1, 2, 3, 4]

- map 자료구조에 해당 값을 넣어준다.

- 이때 순서대로 a, b, c, d에 map의 값을 넣어준다.

 

 

Method 3: List comprehension with stdin and stdout

위 두 내용보다 더 빠른 방법은 stdin, stdout을 이용하는 방법이다.

 

from sys import stdin, stdout
a, b, c, d = [int(x) for x in stdin.readline().rstrip().split()]
stdout.write(str(a*b*c*d) + "\n")

list comprehension을 쓰는 것은 같고 코드에서 stdin, stdout을 쓰는 차이가 있다.

 

 

 

 

https://www.tutorialspoint.com/python-input-methods-for-competitive-programming

https://programmers.co.kr/learn/questions/4736

반응형

'Basic > Python' 카테고리의 다른 글

Ubuntu에서 Pycharm 설치하기  (0) 2020.02.12
파이썬 loop, map, list 처리 속도 차이  (0) 2020.01.22
Python2에서 range와 xrange 차이  (0) 2019.10.27
파이썬 tkinter 예제  (0) 2019.09.11
Python에서 string 표현시 주의사항  (0) 2019.08.03