You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[:material-arrow-left-bold: 이전 페이지](../index.md){ .md-button }
6
+
7
+
## _2025. 03. 01_
8
+
9
+
### python 여러 라인 및 공백 입력 받기
10
+
11
+
입력 관련 글들을 찾아서 돌아다니다가 아래와 같은 방식으로 입력을 받는 것이 가능하다는 것을 알게 되었다.
12
+
13
+
```py
14
+
# 입력 예시:
15
+
# 1 2
16
+
# 3 3 3 3 3
17
+
18
+
a, b, *l =map(int, open(0).read().split())
19
+
print(a, b, l)
20
+
21
+
# 출력 예시:
22
+
# 1 2 [3, 3, 3, 3, 3]
23
+
```
24
+
25
+
파이썬 숏코딩에 유용하게 사용될 수 있어보인다.
26
+
27
+
### python map 함수를 사용하여 만든 객체를 한 번 밖에 순회할 수 없음
28
+
29
+
[공식 문서에 있는 map에 대한 설명](https://docs.python.org/3/library/functions.html#map){:target="\_blank"}에 따르면 당연한 이야기일 수 있겠다.
30
+
31
+
```txt
32
+
Return an iterator that applies function to every item of iterable, yielding the results.
33
+
```
34
+
35
+
예를 들어, 아래와 같은 일이 일어난다.
36
+
37
+
```py
38
+
# 입력 예시:
39
+
# 1 2 4 111 7
40
+
41
+
l =map(int, input().split())
42
+
print(max(l)) # 111
43
+
print(max(l)) # ValueError: max() arg is an empty sequence
44
+
```
45
+
46
+
첫 번째 max함수에서 map에서 인자로 받은 `input().split()` 안에 있는 아이템을 순회하면서 `int`를 먹이고, 그 값으로 max값을 찾았는데, 이 과정에서 모든 아이템을 순회해버려서 다시 max를 부르려 하니 arg로 빈 시퀀스가 들어왔다고 하는 것이다.
47
+
48
+
이를 피하려면 다음과 같이 코드를 작성하면 된다.
49
+
50
+
```py
51
+
l = [*map(int, input().split())] # 리스트로 만들어버린다.
52
+
print(max(l)) # 111
53
+
print(max(l)) # 111
54
+
```
55
+
56
+
[:material-arrow-left-bold: 이전 페이지](../index.md){ .md-button }
0 commit comments