Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Archives
Today
Total
관리 메뉴

RUBY

[WebData] 4. Python List 데이터형 본문

데이터 분석/EDA_웹크롤링_파이썬프로그래밍

[WebData] 4. Python List 데이터형

ruby-jieun 2023. 2. 5. 11:11

 

 

웹 데이터 수집하고 정리하기
4. Python List 데이터형


 

 

 

1. List 형은 대괄호로 생성한다.

colors = ["red", "blue", "green"]

colors[0], colors[1], colors[2]

 

 

 

2. 데이터 변경하기

b = colors
b
b[1] = "black"
b
colors

 

 

 

 

3. 복사하고 데이터 변경하기

c = colors.copy()
c
c[1] = "yellow"
c
colors

 

 

 

 

4. list형을 반복문에(for) 적용 

for color in colors: 
    print(color)

 

 

 

 

5. in명령으로 조건문(if)에 적용 

if "black" in colors:
    print("True")

 

 

 

6. movies 라는 list형 자료를 만든다.

movies = ["오늘 밤, 세계에서 이 사랑이 사라진다 해도", "센과 치히로의 행방불명", "인생은 아름다워", "러브레터"]
movies

 

 

 

7. append : list 제일 뒤에 하나 추가

movies.append("님아, 그 강을 건너지 마오")
movies

 

 

8. pop : 제일 뒤 자료를 지움

movies.pop()
movies

 

 

 

9. extend : 제일 뒤에 다수의 자료를 추가

movies.extend(["첫 키스만 50번째", "시간을 달리는 소녀", "어바웃 타임"])
movies

 

 

10. remove : 같은 이름의 자료를 지움

movies.remove("첫 키스만 50번째")
movies

 

 

 

11. 슬라이싱 : [n:m] n번째부터 m-1까지

movies[3:5]

 

favorite_movies = movies[3:5]
favorite_movies

 

 

 

12. insert : 원하는 위치에 자료를 삽입

favorite_movies.insert(1, 9.36)
favorite_movies

 

favorite_movies.insert(3, 9.43)
favorite_movies

 

 

 

14. list 안에 list를 가질 수 있다.

favorite_movies.insert(5, ["후지이 이츠키", "코노 마코토"])
favorite_movies

 

 

 

15. isinstance : 자료형이 list인지 확인할 수 있다.

isinstance(favorite_movies, list)

 

 

 

16.

for each_item in favorite_movies:
    if isinstance(each_item, list):
        for nested_item in each_item:
            print(nested_item)
    else:
        print(each_item)
Comments