본문 바로가기
파이썬

파이썬 python select specific elements from a list

by º기록 2020. 12. 25.
반응형

목록에서 특정 값만 가져 오는 "pythonic"방법이 있습니까?이 펄 코드와 유사합니다.

my ($one,$four,$ten) = line.split(/,/)[1,4,10]

 

해결 방법

 

operator.itemgetter 를 찾고 있다고 생각합니다.

import operator
line=','.join(map(str,range(11)))
print(line)
# 0,1,2,3,4,5,6,7,8,9,10
alist=line.split(',')
print(alist)
# ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
one,four,ten=operator.itemgetter(1,4,10)(alist)
print(one,four,ten)
# ('1', '4', '10')

 

참조 페이지 https://stackoverflow.com/questions/2134539

 

 

반응형

댓글