본문 바로가기
Algorithm/Python

10871 파이썬: 리스트에서 브라켓 [ ] 없애기

by Saans 2022. 4. 7.

10871번: X보다 작은 수 (acmicpc.net)

 

10871번: X보다 작은 수

첫째 줄에 N과 X가 주어진다. (1 ≤ N, X ≤ 10,000) 둘째 줄에 수열 A를 이루는 정수 N개가 주어진다. 주어지는 정수는 모두 1보다 크거나 같고, 10,000보다 작거나 같은 정수이다.

www.acmicpc.net

갈고 닦은 코드

n, x = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
    if a[i] < x:
        print(a[i], end =" ")

 

덜효율적인 내코드

answer =[]
n, x = map(int, input().split())
a = list(map(int, input().split()))
for i in range(-1, n):
    i += 1
    if a[i] < x:
        answer.append(a[i])
print(*answer, sep =' ')

그러나 리스트에서 브라켓 없애는 법을 배웠다 막줄 참고!

수가 하나일 때는 *(리스트) 만해도 된다.

 

여기 더 많은 방법이 나와있다.

5 Ways to Remove Brackets from List in Python - Python Pool

 

5 Ways to Remove Brackets from List in Python

We all know What is a list in python? Whenever we hear a word list. We will say it is a collection of elements separated by commas and enclosed within the

www.pythonpool.com

 

댓글