[TIL]list, tuple

newjinnyยท2021๋…„ 10์›” 15์ผ
0

python

๋ชฉ๋ก ๋ณด๊ธฐ
4/4
post-thumbnail

3. list

3-1 index๋กœ ์ ‘๊ทผ, ์ˆ˜์ •

#index ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผ
numbers = [50,60,70]
print(numbers[0])#50
print(data = numbers[2]) #70

#index ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์ •
our_list = [50,40,30]
our_list[0] = 15
print(our_list) #[15,40,30]

#๋‹ค์ฐจ์› ๋ฆฌ์ŠคํŠธ์— ์ ‘๊ทผํ•˜๊ธฐ
any_list = [
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [[10,11,12], 13, 14]
]
any_list[3] # [[10,11,12], 13, 14]
any_list[3][0] # [10,11,12]
any_list[3][0][1] # 11

3-2 ์ถ”๊ฐ€, ์ œ๊ฑฐ

#์ถ”๊ฐ€
arr = [1,2,3]
arr.append(4)
print(arr) #[1,2,3,4]

#์ œ๊ฑฐ
three = [1, 4, 6]
remove_one = three.pop()
print(remove_one) #6
print(three) #[1,4]

4. tuple

Tuple์€ ์ผ๋ฐ˜์ ์œผ๋กœ 2๊ฐœ์—์„œ 5๊ฐœ ์‚ฌ์ด์˜ ์š”์†Œ๋“ค์„ ์ €์žฅํ• ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ad hoc(์ฆ‰์„์ ์œผ๋กœ) ํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

my_list = [1, 2, 3, 4, 5, 6]
 a = tuple(my_list[0:2])
 b = tuple(my_list[2:4])
 c = tuple(my_list[4:6])

 tuple_abc = [a,b,c] #[(1,2), (3, 4), (5,6)]

๐Ÿ” list ์™€ tuple ์ฐจ์ด

  • list๋Š” ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • tuple์€ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
profile
I Love Pizza, ๋‚˜๋งŒ์˜ ํ† ํ•‘์œผ๋กœ ํ•œ์กฐ๊ฐ์”ฉ ๋งž์ถฐ๊ฐ€๋Š” ์ค‘

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