1 분 소요

순서

1.리스트 정의

2.list 정렬

3.리스트 슬라이싱


1.리스트

  • 대괄호[]로 감싸주고 각 요솟값은 쉼표(,)로 구분한다
  • list안에 갓을 담아서 생성
  • 문자열, split()함수로 생성
  • 순서가 있음(인덱스가 있다), 중복,수정,삭제가능
중고차 = ['k5', 'white']  
print(type(중고차))
print(중고차)

숫자와 문자를 혼용해서 담을 수 있다 중고차 의 타입을 알 수 있음

중고차 = ['k5', 'white',['520d', 'black']] 
print(중고차)

 

k5만 출력하고 싶을때-리스트라는 자료에서 원하는 자료를 출력할때

중고차 = ['k5', 'white'] 

print(중고차[0]) 

 

자료 수정하는법 중고차1을 black로 수정

중고차 = ['k5', 'white'] 
중고차[1] = 'black'
print(중고차[1])

 

2.list 정렬

  • sort() 리스트 자체를 내부적으로 정렬
  • sort() 리스트의 정렬된 복사본을 반환
중고차 = ['k5', 'white'] 
중고차.sort()
print(중고차) 

문자 숫자 순으로 정렬시킴


    ['k5', 'white']

 

a.sort(reverse=False)# 기본적으로 오름차순 정렬 (=a.sort())

a = [9, 10, 7, 19, 1, 2, 20, 21, 7, 8]


a.sort(reverse=True) # 내림차순

a.reverse() # 내림차순 정렬

# b = sorted(a) # 원본 a는 그대로 유지

# print(b)
print(a)


    [1, 2, 7, 7, 8, 9, 10, 19, 20, 21]

 

중고차 = ['k5', 'white'] 
중고차.reverse() #리스트의 순서를 뒤집어줌
print(중고차)

x = 'hello world'

a = list(x)

print(a)

#튜플형태에서 리스트로 변환

x = (1,2,3)
print(type(x))

y = list(x)
print(y)

#split를 이용하여 리스트 형으로 변환

str_b ='hello world nice weather'
li_b = str_b.split() #구분자가 없으면 띄어쓰기로 구분

print(li_b)


    ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
    <class 'tuple'>
    [1, 2, 3]
    ['hello', 'world', 'nice', 'weather']

list()함수는 다른 데이터 타입을 리스트로 변환할떄도 사용 한다

list()함수를 사용하여 list로 변환을 한다

()소괄호- 인자값,매개변수라고함

 

리스트 인덱싱-순서가 있을떄 인덱싱 가능


a = [1,11,2,22,3,33]

print(a[3])
print(a[-1])

b = [10, 100, ['pen', 'cap', 'plate']] 

print(b[2][1])
print(b[2][-1])


    22
    33
    cap
    plate
# 문자열은 불변(immutable)

str = 'hello world'
print(str[0])

str[0]='k'
 h
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_19392/2564782471.py in <module>
      4 print(str[0])
      5 
----> 6 str[0]='k'

TypeError: 'str' object does not support item assignment

카테고리:

업데이트:

댓글남기기