분류 전체보기(104)
-
[Python] 인덱싱, 슬라이싱
인덱싱(indexing) 파이썬도 다른 언어와 같이 index가 0부터 시작한다. 특이한 점은, -n과 같은 음수 인덱싱도 지원한다. 슬라이싱(slicing) 인덱싱을 지원하는 시퀀스 자료형은 슬라이스(slice)기능을 지원한다. num_list[start:end:step] start: 슬라이싱 시작할 위치 end: 슬라이싱 끝나는 위치. end는 포함되지 않음 step: stride라고도 하며, 몇개씩 건너뛰어 가져올건지 설정 list = [1, 2, 3, 4, 5] tuple = (1, 2, 3, 4, 5) str = "12345" # 특정 위치~끝 print("특정 위치~끝 : ", list[3:]) print("특정 위치~끝 : ", tuple[3:]) print("특정 위치~끝 : ", str[..
2021.01.07 -
[LeetCode] 5. Longest Palindromic Substring
https://leetcode.com/problems/longest-palindromic-substring/ Longest Palindromic Substring - 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 next interview. leetcode.com 내 풀이 class Solution: def longestPalindrome(self, s: str) -> str: if len(s) < 2 or s[:] == s[::-1]: return s palindrome = [] length = len(s) s..
2021.01.07 -
[LeetCode] 49. Group Anagrams
https://leetcode.com/problems/group-anagrams/ Group Anagrams - 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 next interview. leetcode.com 내 풀이 class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: anagrams = [] sorted_words = [] for word in strs: sorted_words.append(sorted(','.join(wor..
2021.01.06 -
[Python] collections.Counter() 이용한 빈도수 세기
collections.Counter() Counter는 해시 가능한 객체를 세기 위한 dict의 서브 클래스. 요소가 딕셔너리 키로 저장되고 개수가 딕셔너리 값으로 저장되는 컬렉션. LeetCode 819. Most Common Word와 같은 문제에서 키 존재 유무를 확인 할 필요 없이 즉시 Count 할 수 있다. most_common([n]) n개의 가장 흔한 요소와 그 개수를 가장 흔한 것부터 가장 적은 것 순으로 나열한 리스트를 반환. n이 생략되거나 None이면 모든 요소를 반환. 리스트에서 가장 흔하게 등장하는 단어를 구하는 예제. import collections # apple3개, banana2개, cherry는 4개 list = ['apple', 'apple', 'apple', 'ban..
2021.01.05 -
[LeetCode] 819. Most Common Word
https://leetcode.com/problems/most-common-word/ Most Common Word - 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 next interview. leetcode.com 내 풀이 import collections import re from typing import OrderedDict class Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: word_dic = {} sort..
2021.01.05 -
[Python] re.sub() 으로 문자열 치환
re.sub()로 문자열 치환 import re re.sub(pattern='패턴', repl='바꿀 문자열(to)', string='바뀔 문자열(from)')이렇게 사용한다. '패턴'은 정규표현식이 들어가고, 정규 표현식을 사용하는 방법은 여기에 있다. 문자 외의 특수문자들을 제거하는 예제는 다음과 같다. import re paragraph = "Hi, my name is Yuseon! Nice to meet you~" result = re.sub(pattern='\W', repl=' ', string=paragraph) word_list = result.lower().split() print(result) print..
2021.01.05