@Data Class Type
# List[ ], index
- A collection of items in a particular order : can be mixture of strings and numerals
- 'index'를 사용하여 개별 요소를 액세스 한다
#List
bicycles = ['trek', 'mtb', 'redline', 'specialized', 1]
print(bicycles)
---------------------------------------------
['trek', 'mtb', 'redline', 'specialized', 1]
#index
print(bicycles[0]) #off-by-one
print(bicycles[-1])
--------------------------------------------
trek
1
https://teddylee777.github.io/python/python-tutorial-02/
List에 대해 잘 정리해두어 이것만 보면 되겠다..
#02-파이썬(Python) 리스트(list)와 튜플(tuple)
파이썬(Python) 리스트(list)와 튜플(tuple)을 알아보고 튜토리얼을 진행합니다.
teddylee777.github.io
#List 의 값 변경 / 추가
- 변경
#changing elements
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles) #['honda', 'yamaha', 'suzuki']
motorcycles[0] = 'ducati'
print(motorcycles) #['ducati', 'yamaha', 'suzuki']
- 추가
.append() 함수를 사용하여 새로운 요소 추가
#adding elements
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.append('ducati')
print(motorcycles)
----------------------------------------------
['honda', 'yamaha', 'suzuki', 'ducati']
- 비어있는 List에 elements 추가
motorcycles = []
print(motorcycles)
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
-------------------------------------
[]
['honda', 'yamaha', 'suzuki']
- 원하는 순서에 element 추가
#inserting elements
motorcycles.insert(1, 'ducati') #method = obeject.arg
print(motorcycles)
---------------------------------------------------------
['honda', 'ducati', 'yamaha', 'suzuki']
#inserting multiple elements
motorcycles[1:1] = ['hysung', 'daerim']
print(motorcycles)
--------------------------------------------
['honda', 'hysung', 'daerim', 'ducati', 'yamaha', 'suzuki']
#List 의 값 삭제
del statement
#removing elements
#'del(delete)' statement
print(motorcycles)
del motorcycles[1]
print(motorcycles)
--------------------------------------
['honda', 'hysung', 'daerim', 'ducati', 'yamaha', 'suzuki']
['honda', 'daerim', 'ducati', 'yamaha', 'suzuki']
# 랜덤으로 값 출력 .pop()
#pop method - random output
print(motorcycles.pop())
print(motorcycles)
----------------------------
suzuki
['honda', 'daerim', 'ducati', 'yamaha']
@ 파이썬 오름차순, 내림차순 정렬
#리스트.sort()
- 기본값: .sort(reverse=False)
- 오름차순: .sort(reverse=False)
- 내림차순: .sort(reverse=True)
#organizing list
#sort() << ordering list
motorcycles.sort()
print(motorcycles) #alphabetically
motorcycles.sort(reverse = True)
print(motorcycles)
-----------------------------------------
['daerim', 'ducati', 'honda', 'yamaha']
['yamaha', 'honda', 'ducati', 'daerim']
#reversing the order
motorcycles.reverse()
print(motorcycles)
---------------------------------
['daerim', 'ducati', 'honda', 'yamaha']
@ for문 | 반복문
[1]
for 변수 in 객체:
실행문
[2]
for 카운터변수 in range(반복횟수):
실행문
for 반복문은 리스트, 배열, 문자열 또는 range() 안에 있는 모든 값들에 대한 코드 블록을 반복한다.
for 반복문 작성을 간소화하기 위해 range()를 사용할 수 있다.
https://white-board.tistory.com/132
자바와 파이썬의 for문 차이
참고: https://leetcode.com/problems/longest-repeating-character-replacement/ Longest Repeating Character Replacement - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your
white-board.tistory.com
#print each item in a list
magicians = ['alice',' david', 'carolina']
for magic in magicians: #define for loop
print(magic) #must cotain indent
print(magicians)
--------------------------------------------------
alice
david
carolina
['alice', 'david', 'carolina']
#actions outside the loop NOT repeated
magicicans = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}. that was a great trick!")
print("Thank you. everyone. That was a great magic show")
----------------------------------------------------------
Alice. that was a great trick!
David. that was a great trick!
Carolina. that was a great trick!
Thank you. everyone. That was a great magic show
# range() 숫자 생성하는 함수
#making numerical lists
#range(): generates a series of numbers
for value in range(1,5): #off-by-one
print(value)
--------------------------------------
1
2
3
4
for value in range(6):
print(value)
-----------------------
0
1
2
3
4
5
# range()로 list에 값 넣기
- list(range())
- list(range(start, stop, step))
#convert range into a list
numbers = list(range(6))
print(numbers)
---------------------------
[0, 1, 2, 3, 4, 5]
#define step size
#(1~6의 숫자에서 2씩 증감하는 값)
# range(start,stop,step << 증감 값)
odd = list(range(1, 6, 2))
print(odd)
---------------------------------
[1, 3, 5]
#create list of squares
squares = []
for value in range(1,11):
square = value ** 2 #제곱
squares.append(square)
print(squares)
----------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#List에서의 부분집합
elements 일정 부분 출력하기
#"slice" to work with part of a list << 'subset' (부분집합)
players = ['charles', 'martina', 'michael', 'rose', 'eli']
print(players[0:3]) #off-by-one
print(players[1:4])
------------------------------------------------------------
['charles', 'martina', 'michael']
['martina', 'michael', 'rose']
#기본적으로, elements는 0부터 시작한다.
print(players[:4])
print(players[2:])
#fast three elements: NO off-by-one
print(players[-3:])
#off-by-one REVERSE
print(players[-3:-1])
--------------------------------------------------------
['charles', 'martina', 'michael', 'rose']
['michael', 'rose', 'eli']
['michael', 'rose', 'eli']
['michael', 'rose']
#Tuple
- 리스트(list)는 가변(mutable)하는 객체(object)이지만, 튜플(tuple)은 불변(immutable)한 객체이다.
- 가변 객체는 요소에 대한 수정, 삭제, 변경 등이 가능하지만, 불편 객체는 요소에 대한 수정, 삭제, 변경이 불가하다.
(1) tuple(), () 로 생성
(2) 혹은 , 로 생성
dimensions = (200,50) #list:[], tuple:()
print(dimensions)
print(dimensions[0])
print(dimensions[1])
-----------------------------------------
(200, 50)
200
50
- index로 tuple의 elements 변경하기 (불가)
- tuple의 elements를 새로 덮어쓰기 (가능)
'Python' 카테고리의 다른 글
[Python] 파이썬 기초 (1) (0) | 2024.04.02 |
---|---|
[Python] 파이썬 개발 환경 Google Colab 구글 코랩 (0) | 2024.03.29 |