[Python] 리스트(List) Start

BioinformaticsAndMe





1. Python List


: 파이썬 리스트는 순서를 가진 집합으로, 리스트는 대괄호( [ ] )로 선언됨

: 자유롭게 변경이 가능하며, 중복을 허용 

→ ['a', 'b', 'c', 'a']

# 리스트 thislist 출력

thislist = ["apple", "banana", "cherry"] print(thislist)

['apple', 'banana', 'cherry']



2. 리스트 Indexing


: 인덱스 번호를 통해, 리스트 항목에 접근할 수 있음

# 리스트의 Index1을 출력

thislist = ["apple", "banana", "cherry"] print(thislist[1])

banana

# 리스트의 Negative Indexing

thislist = ["apple", "banana", "cherry"] print(thislist[-1])

cherry



3. 리스트 Slicing


: 슬라이스 구문(':', 콜론)을 사용하여 문자열 일부를 리턴할 수 있음

: 첫번째 항목의 인덱스는 0

→ [start index]:[end index]

# Index2 ~ Index4 문자 슬라이싱 (Index5는 포함되지 않음)

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[2:5])

['cherry', 'orange', 'kiwi']



4. 리스트 항목 변경


: 리스트의 특정 항목값을 변경하기 위해서, 인덱스 번호를 참조함

# 'banana'를 'blackcurrant'로 변경

thislist = ["apple", "banana", "cherry"] thislist[1] = "blackcurrant" print(thislist)

['apple', 'blackcurrant', 'cherry']



5. 리스트 항목 Check


: 특정 항목이 리스트 내에 존재하는지, 'in' 연산자를 사용해서 확인함

# 'apple'이 thislist에 존재하는지 확인 (존재한다면, 결과 출력)

thislist = ["apple", "banana", "cherry"] if "apple" in thislist: print("Yes, 'apple' is in the fruits list")

Yes, 'apple' is in the fruits list



6. 리스트 join


: 두 개의 리스트를 합치기 위해, 간단하게 '+' 연산자를 사용

# list1과 list2 리스트 값 합치기

list1 = ["a", "b" , "c"]

list2 = [1, 2, 3] list3 = list1 + list2 print(list3)

['a', 'b', 'c', 1, 2, 3]




파이썬 리스트 함수


함수

기능

실행

결과

append

리스트 끝에 항목 추가

fruits = ['apple', 'banana', 'cherry']

fruits.append("orange")

print(fruits)

['apple', 'banana', 'cherry', 'orange']

clear

리스트 모든 항목 제거

fruits = ["apple", "banana", "cherry"]

fruits.clear()

print(fruits)

[ ]

copy

리스트 복사

fruits = ["apple", "banana", "cherry"]

x = fruits.copy()

print(x)

['apple', 'banana', 'cherry']

count

리스트 내 특정 항목을 카운트

fruits = ["apple", "banana", "cherry"]

x = fruits.count("cherry")

print(x)

1

extend

리스트 끝에 다른 리스트 추가

fruits = ['apple', 'banana']

cars = ['Ford', 'BMW', 'Volvo']

fruits.extend(cars)

print(fruits)

['apple', 'banana', 'Ford', 'BMW', 'Volvo']

index

리스트 내 특정값의 첫번째 인덱스 리턴

fruits = ['apple', 'banana', 'cherry']

x = fruits.index("cherry")

print(x)

2

insert

리스트 내 특정 위치에 항목 추가

fruits = ['apple', 'banana', 'cherry']

fruits.insert(1, "orange")

print(fruits)

['apple', 'orange', 'banana', 'cherry']

pop

리스트 내 특정 위치 항목 제거

fruits = ['apple', 'banana', 'cherry']

fruits.pop(1)

print(fruits)

['apple', 'cherry']

remove

리스트 내 특정값을 갖는 항목 제거

fruits = ['apple', 'banana', 'cherry']

fruits.remove("banana")

print(fruits)

['apple', 'cherry']

reverse

리스트의 순서 뒤집기

fruits = ['apple', 'banana', 'cherry']

fruits.reverse()

print(fruits)

['cherry', 'banana', 'apple']

sort

 리스트 값 정렬

cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)

['BMW', 'Ford', 'Volvo']





#Reference

1) https://www.w3schools.com/python/python_lists.asp

2) https://realpython.com/python-lists-tuples/





[Python] 리스트(List) End

BioinformaticsAndMe

'Python' 카테고리의 다른 글

[Python] 파이썬 조건문  (0) 2019.11.21
[Python] 사전(Dictionary)  (0) 2019.11.05
[Python] 문자열(String)  (0) 2019.10.24
[Python] 연산자(Operator)  (0) 2019.10.18
[Python] 자료형(Data type)  (0) 2019.10.15

+ Recent posts