수e 2022. 7. 25. 12:30

1. 배열에서도 string slicing 적용 가능

1
d = [7, 9, ['Myungseo', 'L3opold7']]
cs

 

1
print(d[-1][0])  # 출력값 : Myungseo
cs

 

2. 인덱스 지정해 값 변경

1
2
3
4
5
# List 연속된 값으로 변경
test = [1, 2, 3, 4, 5]
test[2:3] = ['a', 'b', 'c']
 
print(test)  # [1, 2, 'a', 'b', 'c', '4', '5']
 

 

3.인덱스 요소 삭제

1
2
3
4
5
6
7
8
9
10
11
# 리스트 요소 삭제 방법
test = ['a''b''c''d''e']
 
# 1.
test[2:4]=[]        # test = ['a', 'b', 'e']
 
#2. 
del test [2:4]        # test = ['a', 'b', 'e']
 
#3.
del test [2]        # test = ['a', 'b', 'd', 'e']
cs
1
2
3
test = [1, 2, 3, 4, 3]
test.remove(3)
print(test)  #test = [1, 2, 4, 3]
cs
 
   

 

4. reverse, delete

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
test = [31254]
 
 
test.sort(reverse = True)
print(test)        #[5, 4, 3, 2, 1]
 
 
test.reverse()
print(test)        #[4, 5, 2, 1, 3]
 
 
#list의 인덱스값 출력
print(test.index(0))    # [3]
 
 
cs

 

5. list내장함수 활용

1
2
3
4
5
6
7
8
test_list = [12345]
result = []
 
for num in test_list:
    result.append(num*3)
 
print(result)  # [3, 6, 9, 12, 15]
 
cs

=

1
2
3
4
5
test_list = [1, 2, 3, 4, 5]
 
result = [num * 3 for in test_list]
 
print(result)  # [3, 6, 9, 12, 15]
cs