ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • List
    python/기본 2022. 4. 29. 12:08
    # List 는 mutable
    
    l = [0, 1, 2, 3, 4, 5]
    print(l[0])  # 0
    print(l[-1])  # 5
    print(l[-2])  # 4
    print(l[0:2])  # [0, 1]
    print(l[2:])  # [2, 3, 4, 5]
    
    print(list('abc'))  # ['a', 'b', 'c']
    print(type(l))  # 'list'
    
    print(l[::2])  # [0, 2, 4]
    print(l[::-1])  # [5, 4, 3, 2, 1, 0]
    
    a = ['a', 'b', 'c']
    n = [1, 2, 3]
    x = [a, n]  # [['a', 'b', 'c'], [1, 2, 3]]
    print(x[0])  # ['a', 'b', 'c']
    print(x[0][1])  # 'b'
    
    ####################################################################
    s = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    s[0] = 'X'  # 문자열에서는 에러가 발생했지만, list 에서는 OK
    print(s[2:5])  # ['c', 'e', 'f']
    s[2:5] = ['C', 'D', 'E']  # s = ['X', 'b', 'C', 'D', 'E', 'f', 'g']
    s[2:5] = []  # 삭제처리 되어 s = ['X', 'b', 'f', 'g']
    print(s[:])  # ['X', 'b', 'f', 'g']
    s[:] = []
    
    ####################################################################
    n = [1, 2, 3, 4, 5]
    n.append(100)
    print(n)  # [1, 2, 3, 4, 5, 100]
    n.insert(0, 200)
    print(n)  # [200, 1, 2, 3, 4, 5, 100]
    n.pop()  # 100
    print(n)  # [200, 1, 2, 3, 4, 5]
    n.pop(0)  # 200
    print(n)  # [1, 2, 3, 4, 5]
    del n[0]
    print(n)  # [2, 3, 4, 5]
    
    ####################################################################
    n = [1, 2, 2, 2, 3]
    n.remove(2)
    print(n)  # 첫 2만 제거 [1, 2, 2, 3]
    
    n.remove(2)
    n.remove(2)
    print(n)  # [1, 3]
    # n.remove(2)  # 2가 없는 상태에서 지우려고 하면 에러 발생, ValueError: list.remove(x): x not in list
    
    ####################################################################
    a = [1, 2, 3, 4, 5]
    b = [6, 7, 8, 9, 10]
    x = a + b
    print(x)  # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    a += b
    print(a)  # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    a = [1, 2, 3, 4, 5]
    b = [6, 7, 8, 9, 10]
    a.extend(b)
    print(a)  # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    ####################################################################
    r = [1, 2, 3, 4, 5, 1, 2, 3]
    print(r.index(3))  # 2
    print(r.index(3, 3))  # 7
    if 5 in r:
        print('exist')
    
    r.sort()
    print(r)  # [1, 1, 2, 2, 3, 3, 4, 5]
    r.sort(reverse=True)
    print(r)  # [5, 4, 3, 3, 2, 2, 1, 1]
    
    s = 'My name is Mike'
    to_split = s.split(' ')
    print(to_split)  # ['My', 'name', 'is', 'Mike']
    
    x = ' '.join(to_split)
    print(x)  # 'My name is Mike'
    print(help(list))
    
    ####################################################################
    i = [1, 2, 3, 4, 5]
    j = i
    j[0] = 100
    print(i)  # [100, 2, 3, 4, 5]
    print(j)  # [100, 2, 3, 4, 5]
    
    x = [1, 2, 3, 4, 5]
    y = x.copy()  # 혹은 y = x[:] 로 사용도 가능
    y[0] = 100
    print(x)  # [1, 2, 3, 4, 5]
    print(y)  # [100, 2, 3, 4, 5]
    
    -----------------------------------
    
    a = [0, 1, 2, [3]]
    b = copy.deepcopy(a)
    b[3][0] = -3
    
    print(a)
    print(b)
    
    ####################################################################
    X = 20
    Y = X
    Y = 5
    print(id(X))
    print(id(Y))
    print(X)  # 20
    print(Y)  # 5
    
    X = ['a', 'b']
    Y = X
    Y[0] = 'p'
    print(X)  # ['p', 'b']
    print(Y)  # ['p', 'b']

    'python > 기본' 카테고리의 다른 글

    Set  (0) 2022.04.29
    Dictionary  (0) 2022.04.29
    Tuple  (0) 2022.04.29
    String  (0) 2022.04.24
    변수, 수치, print 출력  (0) 2022.04.24

    댓글

Designed by Tistory.