ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] 리스트 유형 - 리스트형, 튜플형, 딕셔너리형, 불리언형, 셋형
    2.IT의 나비효과/② Software 2016. 7. 2. 23:25
    반응형




    리스트형(list) - 생성 후 수정 가능

                      - 대괄호[ ] 사용



    list1 = ["용인","수원"]

    >>> list1

    ['용인', '수원']

    >>> list1.append("안산")

    >>> list1

    ['용인', '수원', '안산']

    >>> list1.insert(0,"성남")

    >>> list1

    ['성남', '용인', '수원', '안산']

    >>> del.list1 = "용인"

    SyntaxError: invalid syntax

    >>> list1.del("용인")

    SyntaxError: invalid syntax

    >>> list1.remove("용인")

    >>> list1

    ['성남', '수원', '안산']

    >>> del list1[2]

    >>> list1

    ['성남', '수원']





    튜플형(tuple) - 생성 후 내용 수정 불가

                       - 소괄호( ) 사용


    >>> del tup1[1]

    Traceback (most recent call last):

      File "<pyshell#17>", line 1, in <module>

        del tup1[1]

    TypeError: 'tuple' object doesn't support item deletion






    딕셔너리형(dictionary) / (map형) - 생성 후 수정 가능

                                                 - 중괄호{ } 사용

                                                 - Key와 Value로 매칭시킴


    >>> ironman = {"company" : "Stark",

          "job" : "CEO",

          "hobby" : "Hero"}

    >>> ironman[1]

    Traceback (most recent call last):

      File "<pyshell#34>", line 1, in <module>

        ironman[1]

    KeyError: 1

    >>> ironman

    {'hobby': 'Hero', 'job': 'CEO', 'company': 'Stark'}

    >>> ironman(company)

    Traceback (most recent call last):

      File "<pyshell#36>", line 1, in <module>

        ironman(company)

    NameError: name 'company' is not defined

    >>> ironman[company]

    Traceback (most recent call last):

      File "<pyshell#37>", line 1, in <module>

        ironman[company]

    NameError: name 'company' is not defined

    >>> ironman["company"]

    'Stark'

    >>> ironman["job"]

    'CEO'

    >>> ironman["hobby"]

    'Hero'

    >>> 

    >>> 

    >>> del ironman["job"]

    >>> ironman

    {'hobby': 'Hero', 'company': 'Stark'}

    >>> ironman["job"]

    Traceback (most recent call last):

      File "<pyshell#52>", line 1, in <module>

        ironman["job"]

    KeyError: 'job'

    >>> ironman.append("job")

    Traceback (most recent call last):

      File "<pyshell#53>", line 1, in <module>

        ironman.append("job")

    AttributeError: 'dict' object has no attribute 'append'

    >>> ironman["suit"] = 'mark'

    >>> ironman

    {'hobby': 'Hero', 'suit': 'mark', 'company': 'Stark'}

    >>> 'hobby' in ironman

    True

    >>> 'hero' in ironman

    False

    >>> 




    불리언형(boolean) - True 또는 False 판별


    >>> bool(ironman)

    True

    >>> 





    SET 유형 - 딕셔너리에서 Value가 없고 Key만 존재하는 유형


    >>> set(ironman)

    {'hobby', 'suit', 'company'}

    반응형

    댓글

Designed by Tistory.