728x90
반응형
SMALL
리스트- 리스트, 튜플, 문자열 같은 것들이 열거형이다.
열거형은 슬라이스를 제공한다.
예제 코드와 결과를 보면 굳이 설명하지 않아도 이해하기 쉬울 것이다.
Java와 같은 language와 조금 다른점은 열거형에 접근 시 -1 처럼 마이너스로 접근이 가능하다는 것이다. (뒤에서 부터) 그리고 slice 에서 세번째 인자로 숫자를 넘겨줄 수 있는데 이는 step 을 말하며 기본값은 1이다. (ex. shoplist[::2] 하면 처음부터 끝까지 2개씩 넘어가면서)
집합 - 정렬되지 않은 단순 객체의 묶음이다.
집합끼리는 부분집합인지 확인할 수 있으며 두집합의 교집합 등을 알아낼 수도 있다.
참조 - 객체를 생성하고 변수에 할당해 줄 때, 실제 객체가 변수에 할당되는 것은 아니며 변수에는 객체의 참조가 할당된다. 참조로 인해 발생하는 몇 가지 현상에 대해서만 알아보자. slice 를 이용하면 참조가 아닌 복사본을 만들 수 있다.
- 대괄호 [] 를 이용해서 표현한다.
- frutslist = ['apple', 'banana', 'orange']
튜플
for f in frutslist: print(f) frutslist.append('mango') frutslist.sort() del frutslist[0]
- 리스트와 비슷하지만 수정이 불가능하여 주로 문자열과 같이 비정적인 객체들을 담을 때 사용한다.
- 리스트 안의 리스트는 리스트의 성질을 잃지 않는다. 파이썬에서 이들은 단지 다른 객체 안에 저장된 객체일 뿐이다.
사전
zoo = ('python', 'elephant', 'penguin') new_zoo = 'monkey', 'camel', zoo print('num of cages in the new zoo is', len(new_zoo)) # 3 print('num of animals in the new zoo is', len(new_zoo)-1 + len(new_zoo[2])) #5
- 전화번호부 같은 것인데, 누군가의 이름을 찾으면 그 사람의 주소와 연락처를 알 수 있는 것과 같다.
Map 와 조금 유사하다고 볼 수 있다.
다만, 사전은 key 에 대한 value 가 없을 경우 null 을 반환하지 않고 에러를 발생 시킨다. (KeyError)
열거형
ab = { 'yhkim' : 'yhkim@gmail.com', 'kskim' : 'kskim@gmail.com', 'sckim' : 'sckim@gmail.com', 'crpark' : 'crpark@gmail.com' } print(ab['kim']['a']) print(ab['kims']) if 'kskim' in ab: print(ab['kskim']) for name, address in ab.items(): print('Contact {} at {}'.format(name, address))
shoplist = ['apple', 'mango', 'caroot', 'banana'] name = 'swaroop' # indexing or subscription print('item 0 is', shoplist[0]) print('item -1 is', shoplist[-1]) print('item 3 is', shoplist[3]) print('character 0 is', name[0]) # slicing on a list print('item 1 to 3 is', shoplist[1:3]) print('item 2 to end is', shoplist[2:]) print('item 1 to -1 is', shoplist[1:-1]) # slicing on a string print('characters 1 to 3 is', name[1:3])
bri = set(['brazil', 'russia', 'india']) print('india' in bri) bric = bri.copy() bric.add('korea') print(bri.issubset(bric)) bric.remove('russia') print(bri & bric)
shoplist = ['apple', 'mango', 'carrot', 'banana'] mylist = shoplist # 참조 del shoplist[0] print('shoplist is', shoplist) # ['mango', 'carrot', 'banana'] print('mylist is', mylist) # ['mango', 'carrot', 'banana'] - 참조의 원래 값이 변경되었기 때문에 당연히 같이 변경됨 # slice 를 이용하면 참조가 아닌 복사본을 만들 수 있다. mylist = shoplist[:] del mylist[0] print('shoplist is', shoplist) # ['mango', 'carrot', 'banana'] print('mylist is', mylist) # ['carrot', 'banana']
728x90
반응형
LIST
'IT > Python' 카테고리의 다른 글
Python - I/O, File I/O, pickle (0) | 2018.09.10 |
---|---|
Python - Class (0) | 2018.09.07 |
Python - 함수 (0) | 2018.08.27 |
Python - 기본문법 (0) | 2018.08.27 |
Python 시작하기 - hello world (0) | 2018.08.24 |