30 seconds of Code
-
filter_non_unique30 seconds of Code 2022. 2. 8. 23:46
내가 푼 것 from collections import Counter def fnu(lst): counter = Counter(lst) nl = [] for u in counter: if counter[u] == 1 : nl.append(u) print(nl) >>> fnu([1,2,2,3,4,4,5]) #[1,3,5] 제공하는 답 print([item for item, count in Counter(lst).items() if count == 1]
-
왼손코딩 1일1파이썬 9,10,11,1230 seconds of Code 2022. 2. 4. 16:51
digitize def digitize(num): print([int(i) for i in str(num)]) #list(str(num))을 하면 각각의 숫자가 쪼개어서 리스트로 들어간다. 단 스트링형태로 #['1','2','3'] #하지만, 이건 주어진 조건인 map()을 사용하지 않았다. #map()은 리스트의 요소를 지정된 함수로 처리해주는 함수이며, 원본 리스트를 변경하지 않고 새 리스트를 생성한다. #list(map(함수, 리스트) #tuple(map(함수, 튜플) def digitize(num): print(list(map(int, str(num)))) >>> digitize(123) #[1,2,3] drop :주어진 리스트와 값을 주었을 때, 리스트에 있는 0번째 요소 ~ 주어진 값을 삭제한..
-
[python]count_occurrences /degrees_to_rads/*difference30 seconds of Code 2022. 2. 4. 16:11
*는 고민했던 문제 count_occurrences def count_occurences(lst, val): print(lst.count(val)) >> count_occurences([1,21, 2, 1, 2, 3], 1) #2 리스트명.count(세고싶은 인덱스) degrees_to_rads 1도 = 파이/180 x도 = x * 파이 / 180 def degrees_to_rads(degree): print(degree * math.pi / 180) >>> degrees_to_rads(180) # ~3.1416 *difference def difference(a,b): newb = set(b) print([c for c in a if c not in newb])
-
파이썬 join, os.path.join30 seconds of Code 2022. 2. 3. 11:30
os.path.join은 여러인자를 받을 수 있고 (단 스트링) 리턴값을 패스처럼 돌려준다. 각 인자사이에 /가 들어간다. join()은 “”.join이 기본 포맷이다. 구분자.join()의 형태로 사용하고, 인자는 하나만 받는다. 이 때 인자로 리스트를 넣어줄 수도 있다. ls=[‘hello’,’iam’,’gaebal’] os.path.join(‘hello’,’iam’,’gaebal’) “/“.join(ls) 위 둘은 같은 값을 출력한다.
-
[python]Compact30 seconds of Code 2022. 1. 28. 01:12
https://www.30secondsofcode.org/python/s/compact compact - 30 seconds of code Removes falsy values from a list. www.30secondsofcode.org 1. what is a 'falsey value?' >> False, None, 0, "" 를 falsey value라고 한다. 2. filter함수 filter함수는 (원하는 값, 리스트)의 형태를 가지는데, 원하는 값 부분에 필터처리할 대상을 넣어주면된다. def compact(lst): print(list(filter(None, lst))) None은 모든 falsey value를 인식한다.