본문 바로가기
혼자 공부하는 프로그래밍/혼자 공부하는 파이썬 (8기)

[혼공파 3주차] 리스트, 딕셔너리, 범위 자료형에 대해 이해한 내용을 바탕으로 포스팅하기

by goosegun 2022. 7. 4.

1. list

    □ [ ]를 이용하여 python list를 만든다.
        list의 index는 0부터 시작한다.
        >>> movies = ["movie1", "movie2", "movie3"]
        >>> print(movies[0])                   # movies list의 첫번째 data를 보여준다.
        >>> len(movies)                           # len()은 data의 크기를 반환하는 BIF이다.
        >>> print(len(movies))                 # 다른 BIF의 결과를 이용하여 BIF를 사용한다.
    □ list의 마지막에 data를 추가하기 위해 append( ) 함수를 사용한다.
        >>> movies.append("movie4")
     □ list의 마지막 data를 제거하기 위해 pop( ) 함수를 사용한다.
        >>> movies.pop()
     □ list에 다수의 data를 추가하기 위해 extend( ) 함수를 사용한다.
        >>> movies.extend(["movie5", "movie6"])
     □ list에서 특정 data를 찾아서 제거하기 위해 remove( ) 함수를 사용한다.
        >>> movies.remove("movie2")
     □ list에서 특정 위치 앞에 특정 data를 추가하기 위해 insert( ) 함수를 사용한다.
        >>> movies.insert(1, "movie2")
     □ list는 다른 list를 포함할 수 있다.
       >>> movies = [["movie1", 1978], ["movie2", 1980], [["movie2", "movie2_ver2"], 1973]]
       >>> print(movies[2][0][1])             # 결과는 movie2_ver2 이다.
     □ 여러개의 List를 묶을때 zip( ) 함수를 사용한다.
         같은 Index의 data끼리 묶이고, List의 크기가 다를 때는 작은 List의 크기를 기준으로 한다.
       >>> list(zip([1,2,3], ['a', 'b', 'c']))          # 결과는 [(1,'a'), (2,'b'), (3,'c')] 이다.

 

2. dictionary

     □ list는 경우 data를 다루기 위해 index number를 사용하지만,
          dictionary는 number외의 다른 어떤 것들도 사용할 수 있다.
          즉, dictionary는 어떤 것을 다른 어떤 것과 결합한다.
     □ python의 dictionary를 다른 언어에서는 hashes라고 부른다.
     □ dictionary는 { }를 이용하여 생성한다.
         >>> stuff = {'name': 'Starwars', 'age': 40, 'child': 2}
         >>> print(stuff['name'])             # 결과는 Starwars이다.
         >>> print(stuff['age'])                # 결과는 40이다.
         >>> print(stuff['child'])              # 결과는 2이다.
         >>> stuff['city'] = "San Francisco"          # stuff dictionary에 새로운 data가 추가된다.
         >>> print(stuff)        # stuff는 {'name':'Starwars', 'age':40, 'child':2, 'city':"San Francisco"이다.
         >>> stuff[1] = "Hi"      # 1은 index가 아니다. {1:"Hi"}라는 새로운 dictionary data가 추가된다.
     □ dictionary에서 원소를 지우기 위해 del( ) 함수를 사용한다. 
         >>> del(stuff['name'])       # 'name':'Starwars' 원소를 지운다
     □ default dictionary는 dictionary에 키값을 정의하고 키값이 없더라도 에러를 출력하지 않는다.
          >>> from collections import defaultdict       # collections에서 불러온다.
          >>> d = defaultdict(object)         # default dictionary를 생성한다.
          >>> d = defaultdict(lambda : 0)      # default dictionary를 생성하고 default값을 0으로 설정한다.

 

3. 범위 자료형

     □ range( ) 함수는 숫자의 배열을 반환한다.
          >>> numbers = range(6)

          >>> print(list(numbers))     # 결과는 [0, 1, 2, 3, 4, 5] 이다.

     □ range( ) 함수는 3개의 파라미터를 가진다.  range(start, stop, step)

      1개의 파라미터가 전달되면 이것은 stop으로 간주한다.

          >>> numbers = range(4)

          >>> print(list(numbers))      # 결과는 [0, 1, 2, 3]이다.

     □ 0 또는 음수가 전달되면, 빈 배열이 반환된다.

          >>> numbers = range(-4)

          >>> print(list(numbers))     # 결과는 [ ] 이다.

     □ 2개의 파라미터가 전달되면 이것은 start (inclusive)와 stop (exclusive)으로 간주한다.

          >>> numbers = range(2, 5)

          >>> print(list(numbers))     # 결과는 [2, 3, 4]이다.

     □ range( ) in for loop

        range( ) 함수는 정해진 수만큰 loop를 반복할때 사용된다.

          >>> for i in range(5):

                     print(i, 'hello')