내멋대로 해라

icehan.egloos.com

포토로그 마이가든



[python] List Comprehension(리스트 내장) tip&tech

s = ' first item : second item : third item '
공백을 없애고, ':' 기준으로 분리하여 리스트로 만들기
L = ['first item', 'second item', 'third item']

>>> L = [x.strip() for x in s.split(':')]

s.split(':')  # ':' 기준으로 분리한 리스트
x.strip()     # 공백 제거(trim). lstrip(), rstrip() 도 있다.

List Comprehension
[expression for expr in sequence
                  for expr2 in sequence2 ...
                  if condition ]

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://icehan.egloos.com/tb/4870199 [도움말]

덧글

덧글 입력 영역