Python
-
[Python] - Sort / Reverse개발/Python 2022. 7. 27. 15:26
1. Sort Only List(In-place) Reverse = [::-1] a = [1,5,2,4] a.sort() > [1,2,4,5] # a.sort() -> a[::-1] a.sort(reverse=True) > [5,4,2,1] 2. Sorted Use Iterable Object A simple ascending sort and returns a new sorted list key parameter call function for list element #iterable dict temp = {1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'} sorted(temp) > [1, 2, 3, 4, 5] sorted(temp.items()) > [(1, 'D'), (2, 'B..
-
[Python] - Iterators개발/Python 2022. 7. 27. 15:01
Iterators An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__(). iterable Lists, tuples, dictionaries, and sets are all iterable obje..
-
[Python] - Function , Lambda개발/Python 2022. 7. 27. 14:37
1. Function A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. A function Object is mutable(User classes are considered mutable) - https://stackoverflow.com/questions/12076445/are-user-defined-classes-mutable # create function def test(): return "test" # call function test() 1-1) *args ..
-
[Python] - Powerful Function개발/Python 2022. 7. 27. 14:12
1. Range range는 range(start, end, step) Option range's result start_number ~ end-1 collection list(range(5,10)) > [5, 6, 7, 8, 9] list(range(10,20,2)) > [10, 12, 14, 16, 18] 2. Enumerate Use repeat Loop we need to check times Loop return tuple( index , element ) for i, v in enumerate(t): print("index : {}, value: {}".format(i,v)) """ index : 0, value: 1 index : 1, value: 5 index : 2, value: 7 in..
-
[Python] - Set개발/Python 2022. 7. 27. 13:05
Set set은 수학에서 이야기하는 집합과 비슷합니다. Not order, unique value mutable Object 1. Syntax s = set() s = set([1,3,5,7]) similar dictionary, but no key . Only value value is not immutable s = {"1", 3, 5, (1,3)} # Error s = {"1", 3, 5, [1,3]} s = {"1", 3, 5, {1,3}} s = {"1", 3, 5, frozenset([1,3,4])} 2. Change Element k = {100, 105} # insert value k.add(50) # multiple value insert k.update([3, 4, 5]) # delet..
-
[Python] - Dictionary개발/Python 2022. 7. 27. 12:17
Dictionary type is immutable key and mutable value mapping not sorted set No index , so access the key Syntax # Default type {"a" : 1, "b":2} e = {} f = dict() newdict = dict( alice = 5, bob = 20, tony= 15, suzy = 30) Key is not allowed mutable key like list , set , dict # Error case a = { {1, 3}: 5, {3,5}: 3} #set a = {[1,3]: 5, [3,5]: 3} #list a = { {"a":1}: 5, "abc": 3} #dict # Key immuttable..
-
[Python] - Tuple개발/Python 2022. 7. 27. 11:59
Tuple immutable Object (순서가 존재) List와 유사하지만 한번 생성되면 값 변경 X 1. 기본 연산 #list와 마찬가지로 다양한 타입이 함께 포함될 수 있습니다. t = (1, "korea", 3.5, 1) # 순서가 있기때문에 인덱스로 접근 가능 t[0] > 1 # '+' 연산으로 tuple(튜플)을 추가 t = t + (3 ,5) > (1, 'korea', 3.5, 1, 3, 5) # '*' 연산으로 tuple(튜플)을 반복 t * 2 > (1, 'korea', 3.5, 1, 3, 5, 1, 'korea', 3.5, 1, 3, 5) 2. 함수 연산 #함수에서 여러 값을 한꺼번에 리턴 def minmax(items): return min(items), max(items) minm..
-
[Python] - List개발/Python 2022. 7. 27. 01:28
List 원소들이 연속적으로 저장되는 형태의 자료형입니다. mutable -> reallocate 할 필요 X 1. 원소 추가 # List 끝에 element 추가 / O(1) list.append(x) # List 끝에 iterable 추가 / O(len(iterable)) list.extend(iterable) # 주어진 i 위치에 항목에 삽입 / O(N) list.insert(i, x) 2. 원소 제거 # delete x's first element / 없다면, ValueError # O(N) list.remove(x) # delete index element / return Value # pop() = O(1) pop(i) = O(N) list.pop([i]) # delete List all el..