원소 덧붙이기 .append()
원소 하나를 꺼내기 .pop()
i.pop(2)
원소 삽입하기 .insert()
i = [20, 37, 58, 72]
i.insert [3, 5]
원소 삭제하기 .del()
del(i[2])
원소 탐색하기: .index()
i.index('Spam')
함수
def add(x, y):
n = x + y
return n
sort : 정렬
- 파이썬 내장 함수 sorted()
- 리스트에 쓸 수 있는 메서드 .sort()
i.sort()
index : 위치 반환
index(x) 함수는 리스트에 x 값이 있으면 x의 위치 값을 돌려준다.
>>> a = [1,2,3]
>>> a.index(3)
2
>>> a.index(1)
0
find(찾을문자, 찾기시작할위치)
>>> s = '가나다라 마바사아 자차카타 파하'
>>> s.find('마')
5
>>> s.find('가')
0
>>> s.find('가',5)
-1
리스트 list
a = [1, 2, 3]
len() 문자열 길이 구하기
len('123')
// 3을 출력
len('webisfree')
// 9를 출력
len('웹이즈프리')
// 5를 출력
len()
// 최소 1개의 인자가 필요하다는 에러가 발생
range 함수: 1부터 10까지 더하는 것
add = 0
for i in range(1, 11):
add = add + i
print(add)
55
문자열 나누기(split)
>>> a = "Life is too short"
>>> a.split()
['Life', 'is', 'too', 'short']
>>> b = "a:b:c:d"
>>> b.split(':')
['a', 'b', 'c', 'd']
map은 원본 리스트를 변경하지 않고 새 리스트를 생성
a = [1.2, 2.5, 3.7, 4.6]
a = list(map(int, a))
a
[1, 2, 3, 4]
연산자
OperatorDescriptionExample
= | 왼쪽 변수에 오른쪽 값을 할당한다 | c = a + b → c = a + b |
+= | 왼쪽 변수에 오른쪽 값을 더하고 결과를 왼쪽변수에 할당 | c += a → c = c + a |
-= | 왼쪽 변수에서 오른쪽 값을 빼고 결과를 왼쪽변수에 할당 | c -= a → c = c - a |
*= | 왼쪽 변수에 오른쪽 값을 곱하고 결과를 왼쪽변수에 할당 | c *= a → c = c * a |
/= | 왼쪽 변수에서 오른쪽 값을 나누고 결과를 왼쪽변수에 할당 | c /= a → c = c / a |
%= | 왼쪽 변수에서 오른쪽 값을 나눈 나머지의 결과를 왼쪽변수에 할당 | c %= a → c = c % a |
**= | 왼쪽 변수에 오른쪽 값만큼 제곱을 하고 결과를 왼쪽변수에 할당 | c **= a → c = c ** a |
//= | 왼쪽 변수에서 오른쪽 값을 나눈 몫의 결과를 왼쪽변수에 할당 | c //= a → c = c // a |
OperatorName
+ | Addition | x + y | Try it » |
- | Subtraction | x - y | Try it » |
* | Multiplication | x * y | Try it » |
/ | Division | x / y | Try it » |
% | Modulus | x % y | Try it » |
** | Exponentiation | x ** y | Try it » |
// | Floor division | x // y | Try it » |
enumerate
- 인덱스 번호와 컬렉션의 원소를 tuple형태로 반환합니다.
# Python program to illustrate
# enumerate function in loops
l1 = ["eat","sleep","repeat"]
# printing the tuples in object directly
for ele in enumerate(l1):
print (ele)
print
# changing index and printing separately
for count,ele in enumerate(l1,100):
print (count,ele)
#getting desired output from tuple
for count,ele in enumerate(l1):
print(count)
print(ele)
(0, 'eat')
(1, 'sleep')
(2, 'repeat')
100 eat
101 sleep
102 repeat
0
eat
1
sleep
2
repeat
입력 받아서 list로 나누기
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(a+b)
set
SET을 이용하여 중복 제거하기
my_list = ['A', 'B', 'C', 'D', 'B', 'D', 'E']
my_set = set(my_list) #집합set으로 변환
my_list = list(my_set) #list로 변환
print(new_list)
출력된 값은 ['D', 'B', 'A', 'E', 'C']
round
소수점을 n번째 까지만 표현하고 반올림을 하고싶을때, round 함수를 사용하면된다.
>>> n = 7/15
>>> n
0.4666666666666667
>>> round(n,2)
0.47
>>> round(n,4)
0.4667
>>> round(n)
0
reverse()
listValue = [1,2,3,4,5] # list(range(5)로 생성 가능
listValue.reverse()
print(listValue)
'Developer > 알고리즘' 카테고리의 다른 글
Leetcode - Two Sum (0) | 2021.09.14 |
---|---|
LeetCode Start (0) | 2021.09.13 |