K 값에서 다음 N개의 요소를 가져와야 하는 경우 단순 반복이 사용됩니다.
아래는 동일한 데모입니다 -
예시
my_list = [31, 24, 46, 18, 34, 52, 26, 29]
print("The list is :")
print(my_list)
K = 2
print("The value of K is :")
print(K)
N = 3
print("The value of N is :")
print(N)
for index in range(K):
my_list[index] = N
print("The result is :")
print(my_list) 출력
The list is : [31, 24, 46, 18, 34, 52, 26, 29] The value of K is : 2 The value of N is : 3 The result is : [3, 3, 46, 18, 34, 52, 26, 29]
설명
-
목록이 정의되어 콘솔에 표시됩니다.
-
K와 N의 값이 정의되어 콘솔에 표시됩니다.
-
목록은 K 범위에서 반복되고 N 값은 특정 인덱스의 요소에 할당됩니다.
-
콘솔에 표시되는 출력입니다.