Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python - 목록에서 대체 요소 패턴 증가

<시간/>

여기서는 enumerate()와 함께 List Comprehension을 사용할 것입니다. Python은 하나의 목록을 다른 목록에서 파생시키기 위한 간결한 구문을 제공합니다. 이러한 표현식을 목록 이해라고 합니다. 목록 이해는 Python에서 가장 강력한 도구 중 하나입니다. Python의 목록 이해는 함수형 프로그래밍 개념에 대한 언어 지원의 예입니다. 자세한 내용은 "www.tutorialspoint.com/python-list-comprehension"에서 읽을 수 있습니다. enumerate() 메서드는 iterable에 카운터를 추가합니다. 열거에 대한 자세한 내용은 여기 " www.tutorialspoint.com/enumerate-in-python"

에서 읽을 수 있습니다.

# declare list of integers
my_list = [1, 2, 3]
# printing the value
print("Printing my_list list : " + str(my_list))
response = [value for sub in ((i, "*" * j)
   for j, i in enumerate(my_list, 1))
   for value in sub]
# print result
print("The increasing element pattern IS : " + str(response))

출력

Printing my_list list : [1, 2, 3]
The increasing element pattern IS : [1, '*', 2, '**', 3, '***']