Introfor

list remove, pop, insert, extend 본문

Doing/Python

list remove, pop, insert, extend

YongArtist 2019. 10. 20. 13:09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
nums = [1,2,3,4]
print(nums)
 
nums.remove(3)
print(nums)
# remove(객체):객체의 값을 인자로 받음. 리스트에 있는 객체이 remove의 인자값과 동일 시 제거하고, 리스트 크기를 1 감소
 
nums.pop()
print(nums)
print(nums.pop(0))
print(nums)
# pop(인덱스):인덱스 값을 인자로 받음. 인덱스 값을 통해 리스트에 객체를 삭제하고, 그 값을 반환.
# 인덱스 값이 없을 시 마지막 객체를 삭제&반환
 
print(nums.extend([3,4]))
# extend(객체 리스트):객체 리스트를 인자로 받음. 리스트와 리스트를 병합할 수 있음.
# extend([])시 기존 리스트에 아무 변화가 없음. 맨 끝에만 객체를 추가.
 
print(nums.insert(0,1))
# insert(인덱스, 객체) 인덱스 값과 객체를 인자로 받음. 지정된 위치에 객체를 추가.
 
# Don't panic!을 on tap으로 바꾸는 작업.
phrase = "Don't panic!"
 
plist = list(phrase)
print(phrase)
print(plist)
 
plist[3= plist[5]
plist[5= plist[7]
 
for i in range(5):
    plist.pop()
 
plist.remove('D')
 
new_pharse = ''.join(plist)
print(plist)
print(new_pharse)
 
# remove(), pop(), extend(), insert()를 모두 활용하여 변형한 코드.
phrase = "Don't panic!"
 
plist = list(phrase)
print(phrase)
print(plist)
 
for i in range(4):
    plist.pop()
    
plist.pop(0)
plist.remove("'")
plist.extend([plist.pop(),plist.pop()])
plist.inert(2,plist.pop(3))
 
new_pharse = ''.join(plist)
print(plist)
print(new_pharse)
cs


'Doing > Python' 카테고리의 다른 글

Python Flask  (0) 2019.12.23
How to release python module  (0) 2019.12.19
enumerate 사용법  (0) 2019.07.05
How to install Pycharm on ubuntu  (0) 2017.02.02
랜덤 뽑기 & excel 연동  (0) 2017.02.01
Comments