IT

튜플을 푸는 기술적 인 방법은 무엇입니까?

lottoking 2020. 8. 19. 18:53
반응형

튜플을 푸는 기술적 인 방법은 무엇입니까? [복제]


이 질문에 이미 답변이 있습니다.

이건 못 생겼어. 더 확실한 방법은 무엇입니까?

import datetime

t= (2010, 10, 2, 11, 4, 0, 2, 41, 0)
dt = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], t[6])

일반적으로 func(*tuple)구문을 사용할 수 있습니다 . 여기에서 보이는 것처럼 보이는 튜플의 일부를 수도 있습니다.

t = (2010, 10, 2, 11, 4, 0, 2, 41, 0)
dt = datetime.datetime(*t[0:7])

이를 튜플 압축 해제 라고 하며 다른 이터 러블 (예 : 목록)에도 사용할 수 있습니다. 다음은 Python 가이드 의 또 다른 예입니다 .

>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # call with arguments unpacked from a list
[3, 4, 5]

참조 https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists를

 dt = datetime.datetime(*t[:7])

참고 URL : https://stackoverflow.com/questions/2238355/what-is-the-pythonic-way-to-unpack-tuples

반응형