본문 바로가기
Python/PythonLibrary

List (리스트 자료형) 간단 정리 [리스트 컴프리핸션 / len / count / append, insert, extend / del, remove, clear / sort / reverse / index / pop / join / in / list 튜플 변환]

by leehii 2022. 7. 26.

[리스트 컴프리핸션]

 

--for문 변경--

 

list = [ ]

for i in range(10) :

  list.append('hellow')

 

>>

 

list = [ 'hellow' for i in range(10) ]

 

 

 

--다중 for문 변경--

 

list1 = ['가', '나', '다']

list2 = ['a', 'b', 'c']

new_list = [ ]

 

for i in list1:

  for j in list2 :

    new_list .append( str( i ) + str( j ) )

 

>> 

new_list = [ str( i ) + str( j ) for i in list1 for j in list2 ]

 

 

---if문 변경---

 

list = [ i for i in range (1, 11) ]

list2 = [ num for num in list if num % 3 == 0 ]

>>  1 ~ 10 까지 담긴 list1 에서 3의 배수만 list2에 담김

 

 

---리스트 컴프리핸션과 zip을 이용한 리스트 여러개 DataFrame 만들기---

 

 

----------------------------------------------------------

 

리스트 길이 구하기 :

len(list)

 

list = [1, 2, 3]

len(list) = 3

 

 

---------------------------------------------------------------------------------

 

 

리스트에 있는 특정 요소 개수 세기 :

list.count(카운트할 요소)

 

list= [1, 2, 3, 1]

list.count(1) >> 리턴값은 2이다

 

 

 

---------------------------------------------------------------------------------

 

 

리스트 요소추가 :

 list.appned(추가할 요소)

 

list = [1, 2, 3]

list.append(4)

list >> [1, 2, 3, 4]

 

append와 똑같이 extend를 사용할 수 있는데 

append는 리스트 끝에 해당 요소를 그대로 넣고

extend는 객체 자체를 넣는다

따라서 추가하는 대상이 리스트라 가정할때

append로 추가하면 1번 리스트안에 2번 리스트로 들어가고

extend로 추가하면 1번 리스트안에  2번리스트의 요소값만 들어가서

리스트는 한개이다

 

 

리스트 요소 삽입 :

 list.insert(추가할 위치, 추가할 요소)

 

insert (a,b)를 이용해 a번째 인덱스에 요소 b를 삽입하는 방법

list = [1, 2, 3]

list.insert(0, 4)

>> list = [4, 1, 2, 3]이 된다

 

 

---------------------------------------------------------------------------------

 

인덱스로 리스트 요소 삭제 :

del list[인덱스]

 

list=[1,2,3]

del list[0]

list >> [2, 3]

 

 

 

요소로 리스트 요소 삭제 :

list.remove(삭제할 요소)

 

list=[1 ,2, 3, 1, 2, 3]

list.remove(3)

list >> [1, 2, 1, 2, 3]

 

 

 

 

list 모든 요소 삭제 :

list.clear( )

 

list  = [ 1, 2, 3, 4, 5 ]

list.clear()

print(list)

>> 빈 리스트 출력

---------------------------------------------------------------------------------

 

 

리스트 정렬 : sort

list.sort()

 

 

리스트 역순 정렬 : reverse

list.reverse()

 

 

---------------------------------------------------------------------------------

 

 

리스트 요소 위치 리턴 : index

list=[1, 2, 3]

list.index(3) >> 요소 3의 인덱스 번호인 2가 리턴됨

 

 

---------------------------------------------------------------------------------

 

리스트 마지막 요소 불러오고 삭제 :

list.pop()

 

list = [1, 2, 3]

list.pop()

 

>>return 값 = 3

>> list = [1, 2]

 

 

---------------------------------------------------------------------------------

문자열을 list로 분리하기 :  

list명 = str.split("나눌 기준")


list를 문자열로 분리 : 

text명 = "나눌 기준".join(list명)

 

 

 

---------------------------------------------------------------------------------

list = ['a', 'b', 'c']

'a' in list  >> True로 리턴됨

'1' in list >> False로 리턴됨

---------------------------------------------------------------------------------

리스트가 []의 형태라면 튜플은 ()의 형태

리스트는 요소의 생성, 삭제, 수정이 가능하지만 튜플은 불가능

 

tuple1 = ( )

tuple2 = ( 1, )

tuple3 = ( 1, 2, 3 )

tuple4 = 1,2,3

tuple5 = ( 'a', 'b', ('ab', 'cd') )

 

리스트와의 또다른 차이점은

tuple2에서 보이듯이 1개의 요소일땐 뒤에 , 콤마를 적어줘야 한다는것과

tuple4처럼 괄호생략이 가능하다는 점이 있음

 

수정이 어렵기때문에 튜플보다는 리스트의 사용빈도가 더 높다

 

---------------------------------------------------------------------------------

 

a_list = [1, 2, 3]

b_tuple = (1, 2, 3)

 

 

tuple(a_list) >> 리스트를 튜플로 변환

list(b_tuple) >> 튜플을 리스트로 변환

---------------------------------------------------------------------------------