파이썬 리스트

2020. 2. 5. 17:10개발노트

if __name__ == '__main__':

    q = [2,3]
    p = [1, q, 4]
    p[1].append('버퍼')
    print(p)
    print(q)

    print('---- 리스트 메서드 ----')
    people = ['아이유', '청하']
    people.append('제키와이')
    print(people)
    people[len(people):] = ['윤하']
    print(people)

    print('---- extend() ----')
    people = ['아이유', '청하']
    people.extend('태연')
    print(people)
    people += '윤하'
    print(people)
    people += ['제키와이']
    print(people)

    print('---- insert() ----')
    people = ['아이유', '청하']
    people.insert(1, '태연')
    print(people)

    print('---- remove() ----')
    people = ['아이유', '청하']
    people.remove('청하')
    print(people)

    print('---- pop() ----')
    people = ['아이유', '청하', '태연']
    people.pop(1)
    print(people)
    people.pop()
    print(people)

    print('---- del문 ----')
    a = [-1, 4, 5, 7, 10]
    del a[0]
    print(a)
    del a[2:3]
    print(a)

    print('---- index() ----')
    people = ['아이유', '청하', '태연']
    print(people.count('청하'))

    print('---- count() ----')
    people = ['아이유', '청하', '태연', '아이유']
    print(people.count('아이유'))

    print('---- sort() ----')
    mynum = [8,3,2,1,5,6,11,442]
    mynum.sort()
    print(mynum)
    mynum.sort(reverse=True)
    print(mynum)

    print('---- reverse() ----')
    people = ['아이유', '청하', '태연']
    people.reverse()
    print(people)
    print(people[::-1])

    print('---- 리스트 언패킹 ----')
    first, *rest = [1,2,3,4,5]
    print(first)
    print(rest)

    def example_args(a, b, c):
        return a * b * c
    L = [2, 3, 4]
    print(example_args(*L))
    print(example_args(2, *L[1:]))

    print('---- 리스트 컴프리헨션 ----')
    a = [y for y in range(1, 10) if y % 4 is 0]
    print(a)
    b = [2**i for i in range(13)]
    print(b)
    c = [x for x in a if x % 2 is 0]
    print(c)
    d = [str(round(355/133.0,i)) for i in range(1, 6)]
    print(d)
    words = 'a bc def gefdawf'.split()
    e = [[w.upper(), w.lower(), len(w)] for w in words]
    for i in e:
        print(i)
[1, [2, 3, '버퍼'], 4]
[2, 3, '버퍼']
---- 리스트 메서드 ----
['아이유', '청하', '제키와이']
['아이유', '청하', '제키와이', '윤하']
---- extend() ----
['아이유', '청하', '태', '연']
['아이유', '청하', '태', '연', '윤', '하']
['아이유', '청하', '태', '연', '윤', '하', '제키와이']
---- insert() ----
['아이유', '태연', '청하']
---- remove() ----
['아이유']
---- pop() ----
['아이유', '태연']
['아이유']
---- del문 ----
[4, 5, 7, 10]
[4, 5, 10]
---- index() ----
1
---- count() ----
2
---- sort() ----
[1, 2, 3, 5, 6, 8, 11, 442]
[442, 11, 8, 6, 5, 3, 2, 1]
---- reverse() ----
['태연', '청하', '아이유']
['아이유', '청하', '태연']
---- 리스트 언패킹 ----
1
[2, 3, 4, 5]
24
24
---- 리스트 컴프리헨션 ----
[4, 8]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
[4, 8]
['2.7', '2.67', '2.669', '2.6692', '2.66917']
['A', 'a', 1]
['BC', 'bc', 2]
['DEF', 'def', 3]
['GEFDAWF', 'gefdawf', 7]