ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python tutorial] 7. Data type - List
    코딩/Python 2022. 12. 21. 17:12
    728x90

    List

    • ordered
    • changeable
    • allow duplicate values

    Note
    Python에는 array가 없고 list를 대신 사용할 수 있다.
    대량의 데이터는 NumPy를 사용한다.

    thislist = ["apple", "banana", "cherry", "apple", "cherry"] # allow duplicate values
    print(thislist)
    ['apple', 'banana', 'cherry', 'apple', 'cherry']

    List Length: len()

    print(len(thislist)) # 5

    Data Types - any data types

    list1 = ["apple", "banana", "cherry"]
    list2 = [1, 5, 7, 9, 3]
    list3 = [True, False, False]
    list4 = ["abc", 34, True, 40, "male"]

    Constructor: list(())

    thislist = list(("apple", "banana", "cherry")) # list(())

    Access Items

    The first item index: 0

    thislist = ["apple", "banana", "cherry"]
    
    print(thislist[1])    # banana
    
    # Negative Indexing: -1, the last item
    print(thislist[-1])    # cherry
    thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
    
    # Range of Indexes: 마지막 숫자는 제외
    print(thislist[2:5])    # ['cherry', 'orange', 'kiwi']
    
    # items from the beginning
    print(thislist[:4])     # ['apple', 'banana', 'cherry', 'orange']
    
    # to the end
    print(thislist[2:])     # ['cherry', 'orange', 'kiwi', 'melon', 'mango']
    
    # Range of Negative Indexes
    print(thislist[-4:-1])    # ['orange', 'kiwi', 'melon']

    Check if Item Exists: if...in...

    thislist = ["apple", "banana", "cherry"]
    
    if "apple" in thislist: # True
      print("Yes, 'apple' is in the fruits list")

    Change List Items

    thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
    
    # Change Item Value
    thislist[1] = "blackcurrant"

    Change a Range of Item Values

    # 2 items -> 2 items
    thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
    thislist[1:3] = ["blackcurrant", "watermelon"]
    # ["apple", "blackcurrant", "watermelon", "orange", "kiwi", "mango"]
    
    #  1 item -> 2 items: + 1
    thislist[1:2] = ["grape", "pear"]
    # ["apple", "grape", "pear", "watermelon", "orange", "kiwi", "mango"]
    
    # 2 items -> 1 item: -1
    thislist[1:3] = ["banana"]
    # ["apple", "banana", "watermelon", "orange", "kiwi", "mango"]

    Add List Items

    thislist = ["apple", "banana", "cherry"]
    
    # Append Items: append()
    thislist.append("orange")
    # ['apple', 'banana', 'cherry', 'orange']
    
    # Insert Items: insert()
    thislist.insert(1, "melon")
    # ['apple', 'melon', 'banana', 'cherry', 'orange']

    Extend List: extend()

    Add Any Iterable

    thislist = ["apple", "banana", "cherry"]
    tropical = ["mango", "pineapple", "papaya"]
    thislist.extend(tropical)
    # ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
    thislist = ["apple", "banana", "cherry"]
    thistuple = ("kiwi", "orange")
    thislist.extend(thistuple)
    # ['apple', 'banana', 'cherry', 'kiwi', 'orange']

    Remove List Items

    thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
    
    # Remove Specified Item: remove()
    thislist.remove("banana")
    # ["apple", "cherry", "orange", "kiwi", "mango"]
    
    # Remove Specified Index: pop()
    thislist.pop(1)
    # ["apple", "orange", "kiwi", "mango"]
    
    # Remove the last item
    thislist.pop() 
    # ["apple", "orange", "kiwi"]
    
    # del keyword
    del thislist[0]
    # ["orange", "kiwi"]
    
    # Clear the List: clear()
    thislist.clear()
    # []
    
    # Delete the entire list: del
    del thislist

    Loop Lists

    thislist = ["apple", "banana", "cherry"]
    
    for x in thislist:
      print(x)

    apple
    banana
    cherry

    Loop Through the Index Numbers: range() & len()

    thislist = ["apple", "banana", "cherry"]
    for i in range(len(thislist)): # [0, 1, 2]
      print(thislist[i])

    apple
    banana
    cherry

    While Loop: while...len()...

    thislist = ["apple", "banana", "cherry"]
    i = 0
    while i < len(thislist):
      print(thislist[i])
      i = i + 1

    apple
    banana
    cherry

    List Comprehension

    thislist = ["apple", "banana", "cherry"]
    [print(x) for x in thislist]

    apple
    banana
    cherry

    for.. statement

    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    newlist = []
    
    for x in fruits:
      if "a" in x:
        newlist.append(x)
    
    print(newlist)
    # ['apple', 'banana', 'mango']

    list comprehension

    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    
    newlist = [x for x in fruits if "a" in x]
    # ['apple', 'banana', 'mango']

    Syntax

    newlist = [expression for item in iterable if condition == True]

    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    
    newlist = [x for x in fruits if x != "apple"]
    # ['banana', 'cherry', 'kiwi', 'mango']
    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    
    newlist = [x for x in fruits] # condition: option
    # ['apple', 'banana', 'cherry', 'kiwi', 'mango']
    newlist = [x for x in range(10)] # any iterable
    # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    newlist = [x for x in range(10) if x < 5]
    # [0, 1, 2, 3, 4]

    Expression

    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    
    newlist = [x.upper() for x in fruits]
    # ['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO']
    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    
    newlist = ['hello' for x in fruits]
    # ['hello', 'hello', 'hello', 'hello', 'hello']
    fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
    
    newlist = [x if x != "banana" else "orange" for x in fruits]
    # ['apple', 'orange', 'cherry', 'kiwi', 'mango']

    Sort Lists: sort()

    thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
    thislist.sort()
    # ['banana', 'kiwi', 'mango', 'orange', 'pineapple']
    
    thislist = [100, 50, 65, 82, 23]
    thislist.sort()
    [23, 50, 65, 82, 100]

    Sort Descending: reverse = True

    thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
    thislist.sort(reverse = True)
    # ['pineapple', 'orange', 'mango', 'kiwi', 'banana']

    Customize Sort Function: key = function

    def myfunc(n):
      return abs(n - 50) # how close the number is to 50
    
    thislist = [100, 50, 65, 82, 23]
    thislist.sort(key = myfunc) # [50, 0, 15, 32, 27] -> [50, 65, 23, 82, 100]
    # [50, 65, 23, 82, 100]

    Case Insensitive Sort: str.lower

    thislist = ["banana", "Orange", "Kiwi", "cherry"]
    thislist.sort() # case sensitive
    # ['Kiwi', 'Orange', 'banana', 'cherry']
    
    thislist = ["banana", "Orange", "Kiwi", "cherry"]
    thislist.sort(key = str.lower)
    # ['banana', 'cherry', 'Kiwi', 'Orange']

    Reverse Order: reverse()

    thislist = ["banana", "Orange", "Kiwi", "cherry"]
    thislist.reverse()
    # ['cherry', 'Kiwi', 'Orange', 'banana']

    Copy Lists

    thislist = ["apple", "banana", "cherry"]
    
    # copy()
    mylist = thislist.copy()
    
    # list()
    mylist = list(thislist)

    copy()는 shallow copy (리스트 객체의 참조를 복사). deepcopy()는 객체 자체를 복사

    Join Lists

    list1 = ["a", "b", "c"]
    list2 = [1, 2, 3]
    
    # +
    list3 = list1 + list2
    # ['a', 'b', 'c', 1, 2, 3]
    
    # append()
    for x in list2:
      list1.append(x) # join list2 into list1
    
    # extend()
    list1.extend(list2) # add list2 at the end of list1

    List Methods

    728x90

    댓글

Designed by Tistory.