Python/Data Struc & algo 11

[Python] IIS log date Sqlite3 Memory DB and pandas DataFrame

- pandas.read_sql_query를 통해 쿼리의 결과값을 pandas DataFrame 가져올 수 있다 # 약 42만 라인의 IIS로그를 SQLite3 메모리DB로 생성하여 'scbytes', 'csbytes', 'timetaken' 컬럼을 pandas와 연동 pandas.read_sql_query(쿼리, conn) import sqlite3 from datetime import datetime import pandas as pd conn = sqlite3.connect(':memory:') c = conn.cursor() c.execute('''CREATE TABLE memorylogdb (date date, time time, sitename VARCHAR(50), computername V..

[Python] collections namedtuple (이름을 가진 튜플)

DOCS : https://docs.python.org/3/library/collections.html?highlight=namedtuple#collections.namedtuple - 인덱스로 요소를 접근하는 튜플보다 Naming으로 직관적으로 접근이 가능. (마치 사전처럼) - 변경 불가능한 자료형(immutable)으로 요소를 변경하지 않을 경우 사전보다 성능에 이점이 있다. namedtuple (네임드 튜플) import collections Score = collections.namedtuple('Score', 'english math music ') Student1 = Score(english=90, math=80, music=85) print(Student1.english, Student1[..

[Python] collections Counter를 이용한 합집합, 교집합, 차집합 구하기

# 합집합 import collections lst1 = ['a','b','c','a','c','f','g'] tup1 = ('b','d','e','e','f','f','g','h','h') ct1 = collections.Counter(lst1) ct2 = collections.Counter(tup1) print(ct1+ct2) # 합집합 요소 갯수 세기 print(list((ct1+ct2).elements())) # 합집합 리스트 Counter({'f': 3, 'a': 2, 'b': 2, 'c': 2, 'g': 2, 'e': 2, 'h': 2, 'd': 1}) ['a', 'a', 'b', 'b', 'c', 'c', 'f', 'f', 'f', 'g', 'g', 'd', 'e', 'e', 'h', 'h'..

[Python] collections deque를 이용하여 리스트의 왼쪽에 요소 추가하기

# 왼쪽에 요소값 추가하기 (1) - appendleft import collections lst = ['a','b','c','d','e','f','g'] deq = collections.deque(lst) deq.appendleft('h') print(list(deq)) ['h', 'a', 'b', 'c', 'd', 'e', 'f', 'g'] # 왼쪽에 요소값 추가하기 (2) - extendleft import collections lst = ['a','b','c','d','e','f','g'] deq = collections.deque(lst) deq.extendleft('h') print(list(deq)) ['h', 'a', 'b', 'c', 'd', 'e', 'f', 'g']

[Python] 회문 판별(palindrome)

파이썬 회문 판별(palindrome) 회문(palindrome) : 순서를 거꾸로 읽어도 제대로 읽은 것과 같은 단어와 문장 Ex) 'level', 'sos', 'nurses run' 같은 단어와 문장 'l e v e l' 을 예를들어 보면, 첫 문자와 마지막 문자가 동일하고, 안으로 들어갈 수록 서로 같으면 회문 def palindrome(word): for i in range(len(word) // 2): if word[i] != word[-1 -i]: return False return True print(palindrome('level')) True 회문은 거꾸로 읽어도 동일하므로, 입력받은 문자열을 리스트로 변환하여 reversed(역순)와 비교하는 방법도 가능하다 words = ['level..