(DataStructure & Algorithm) List data

์ž„๊ฒฝ๋ฏผยท2023๋…„ 10์›” 25์ผ
1
post-thumbnail
post-custom-banner

๊ธฐ๋ณธ ํ˜•ํƒœ

  • listํ˜•์€ ๋Œ€๊ด„ํ˜ธ๋กœ ์ƒ์„ฑ
colors = ["red", "blue", "green"]

colors[0], colors[1], colors[2]

Output : ('red', 'blue', 'green')

b = colors
b

Output : ('red', 'blue', 'green')


b[1] = "black"
b

Output : ['red', 'blue', 'green']


colors

Output : ['red', 'black', 'green']


์›๋ณธ ๋ฐ์ดํ„ฐ๊ฐ ๋ณ€๊ฒฝ๋˜๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ

  • ๊นŠ์€๋ณต์‚ฌ : .copy() ํ™œ์šฉ
c = colors.copy()
c

Output : ['red', 'black', 'green']


c[1] = "yellow"
c

Output : ['red', 'yellow', 'green']


colors

Output : ['red', 'black', 'green']


๋ฐ˜๋ณต๋ฌธ


  • List ํ˜•์„ ๋ฐ˜๋ณต๋ฌธ(for)์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅธ ์–ธ์–ด์— ๋น„ํ•ด ํŽธ๋ฆฌํ•จ
for color in colors: 
    print(color)

์กฐ๊ฑด๋ฌธ


  • in ๋ช…๋ น์œผ๋กœ ์กฐ๊ฑด๋ฌธ(if)์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅธ ์–ธ์–ด์— ๋น„ํ•ด ํŽธ๋ฆฌํ•จ
if "white" in colors:
    print("True")

๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค


movies = ["๋ผ๋ผ๋žœ๋“œ", "๋จผ ํ›—๋‚  ์šฐ๋ฆฌ", "์–ด๋ฒค์ €์Šค", "๋‹คํฌ๋‚˜์ดํŠธ"]
print(movies)

Output : ['๋ผ๋ผ๋žœ๋“œ', '๋จผ ํ›—๋‚  ์šฐ๋ฆฌ', '์–ด๋ฒค์ €์Šค', '๋‹คํฌ๋‚˜์ดํŠธ']


  • append: list ์ œ์ผ ๋’ค์— ์ถ”๊ฐ€
movies.append("ํƒ€์ดํƒ€๋‹‰")
movies

Output : ['๋ผ๋ผ๋žœ๋“œ', '๋จผ ํ›—๋‚  ์šฐ๋ฆฌ', '์–ด๋ฒค์ €์Šค', '๋‹คํฌ๋‚˜์ดํŠธ', 'ํƒ€์ดํƒ€๋‹‰']


  • pop: ๋ฆฌ์ŠคํŠธ ์ œ์ผ ๋’ค๋ถ€ํ„ฐ ์ž๋ฃŒ๋ฅผ ํ•˜๋‚˜์”ฉ ์‚ญ์ œ
movies.pop()
movies

Output : ['๋ผ๋ผ๋žœ๋“œ', '๋จผ ํ›—๋‚  ์šฐ๋ฆฌ', '์–ด๋ฒค์ €์Šค', '๋‹คํฌ๋‚˜์ดํŠธ']


  • extend: ์ œ์ผ ๋’ค์— ์ž๋ฃŒ ์ถ”๊ฐ€
movies.extend(["์œ„๋Œ€ํ•œ์‡ผ๋งจ", "์ธ์…‰์…˜", "ํ„ฐ๋ฏธ๋„ค์ดํ„ฐ"])
movies

Output : ['๋ผ๋ผ๋žœ๋“œ', '๋จผ ํ›—๋‚  ์šฐ๋ฆฌ', '์–ด๋ฒค์ €์Šค', '๋‹คํฌ๋‚˜์ดํŠธ', '์œ„๋Œ€ํ•œ์‡ผ๋งจ', '์ธ์…‰์…˜', 'ํ„ฐ๋ฏธ๋„ค์ดํ„ฐ']


  • remove: ์ž๋ฃŒ๋ฅผ ์‚ญ์ œ
movies.remove("์–ด๋ฒค์ €์Šค")
movies

Output : ['๋ผ๋ผ๋žœ๋“œ', '๋จผ ํ›—๋‚  ์šฐ๋ฆฌ', '๋‹คํฌ๋‚˜์ดํŠธ', '์œ„๋Œ€ํ•œ์‡ผ๋งจ', '์ธ์…‰์…˜', 'ํ„ฐ๋ฏธ๋„ค์ดํ„ฐ']


  • ์Šฌ๋ผ์ด์‹ฑ:ย [n:m][n:m] nn๋ฒˆ์งธ๋ถ€ํ„ฐ mโˆ’1m-1๋ฒˆ์งธ ๊นŒ์ง€
movies[3:5]

Output : ['์œ„๋Œ€ํ•œ์‡ผ๋งจ', '์ธ์…‰์…˜']


  • insert: ์›ํ•˜๋Š” ์œ„์น˜์— ์ž๋ฃŒ๋ฅผ ์‚ฝ์ž…
favorite_movies.insert(1, 9.60)
favorite_movies

Output : ['์œ„๋Œ€ํ•œ์‡ผ๋งจ', 9.6, '์ธ์…‰์…˜']


favorite_movies.insert(3, 9.50)
favorite_movies

Output : ['์œ„๋Œ€ํ•œ์‡ผ๋งจ', 9.6, '์ธ์…‰์…˜', 9.5]


  • list์•ˆ์— list ํ˜•ํƒœ ๊ฐ€๋Šฅ
favorite_movies.insert(5, ["๋ ˆ์˜ค๋‚˜๋ฅด๋„ ๋””์นดํ”„๋ฆฌ์˜ค", "์กฐ์šฉํ•˜"])
favorite_movies

Output : ['์œ„๋Œ€ํ•œ์‡ผ๋งจ', 9.6, '์ธ์…‰์…˜', 9.5, ['๋ ˆ์˜ค๋‚˜๋ฅด๋„ ๋””์นดํ”„๋ฆฌ์˜ค', '์กฐ์šฉํ•˜']]


  • isinstance: ์ž๋ฃŒํ˜• True/False
isinstance(favorite_movies, list)

Output : True


for each_item in favorite_movies:
    if isinstance(each_item, list):
        for nested_item in each_item:
            print("nested_item", nested_item)
    else:
        print("each_item", each_item)

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€