티스토리 뷰

Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).

파이썬 리스트 (= 상위수준의 집합체) 특징

  • 파이썬 변수는 식별자를 지정하지 않는다.
  • 파이썬 리스트는 여러 데이터 형을 함께 사용할 수 있다.
  • 중첩 리스트 : 무한 단계까지 리스트 안에 리스트가 들어갈 수 있다.

리스트 내장함수

  • append
  • extend
  • insert
  • pop
  • remove

참고

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> cast = ["Cleese", 'Palin', 'Jones', "Idel"]
>>> print(cast)
['Cleese', 'Palin', 'Jones', 'Idel']
>>> print(len(cast))
4
>>> print(cast[1])
Palin
>>> cast.append("Gilliam")
>>> print(cast)
['Cleese', 'Palin', 'Jones', 'Idel', 'Gilliam']
>>> cast.pop()
'Gilliam'
>>> print(cast)
['Cleese', 'Palin', 'Jones', 'Idel']
>>> cast.extend(["Gilliam", "Chapman"])
>>> print(cast)
['Cleese', 'Palin', 'Jones', 'Idel', 'Gilliam', 'Chapman']
>>> cast.remove("Chapman")
>>> print(cast)
['Cleese', 'Palin', 'Jones', 'Idel', 'Gilliam']
>>> cast.insert(0, "Chapman")
>>> print(cast)
['Chapman', 'Cleese', 'Palin', 'Jones', 'Idel', 'Gilliam']
>>> 

for

루프를 제어할 이유가 없다는 while문 대산 for문을 쓴다.

>>> for each_flick in fav_movies:
	print(each_flick)

	
The Holy Grail
The Life of Brian
>>> 

while

while문을 사용할 경우 상태 정보에 신경을 써야한다.

>>> count = 0
>>> while count < len(fav_movies):
	print(fav_movies[count])
	count = count+1

	
The Holy Grail
The Life of Brian
>>> 

리스트 처리

>>> movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
	  ["Graham Chapman", ["Michael Palin", "John Cleese",
			      "Terry Gilliam", "Eric Idle", "Terry Jones"]]]
>>> print(movies)
['The Holy Grail', 1975, 'Terry Jones & Terry Gilliam', 91, ['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]]
>>> for each_item in movies:
	print(each_item)

	
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]
>>> isinstance(movies, list)
True
isinstance() 식별자가 특정 형의 데이터를 갖고 있는지 확인

def 문으로 함수 정의하여 사용하기

>>> def print_lol(the_list):
	for each_item in the_list :
		if isinstance(each_item, list):
			print_lol(each_item)
		else :
			print(each_item)

			
>>> print_lol(movies)
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
>>> 

파이썬 3는 기본적으로 재귀 함수 호출 횟수를 1,000번으로 제한

댓글