본문 바로가기

ALL

(300)
[Python] 제어문과 조합하여 만들기 Comprehension Comprehension iterable한 오브젝트를 생성하기 위한 방법 중 하나 파이썬에서 사용할 수 있는 유용한 기능 List Comprehension (LC) >>> new_squares = [x**2 for x in range(10)] >>> new_squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> new_results = [(a,b) for a in items for b in items if a != b] >>> new_results [('가위', '바위'), ('가위', '보'), ('바위', '가위'), ('바위', '보'), ('보', '가위'), ('보', '바위')] Set Comprehension (SC) >>> new_a = {x for x in..
[Python] set() set() 집합 파이썬 2.3부터 지원하기 시작한 자료형 데이터 집합을 위한 데이터 타입 세트 타입은 파이썬 기본 함수인 set() 함수로만 빈 세트를 만들 수 있다 color = set() set()의 특징 중복을 허용하지 않음 순서가 없음(Unordered) set.add(항목) 세트 항목 추가 >>> color.add('red') >>> color.add('blue') >>> color.add('gray') >>> color {'gray', 'blue', 'red'} set.remove(항목) 세트 특정 항목 제거 >>> color.remove('red') >>> color {'gray', 'blue'} set.discard(항목) 세트 항목 폐기 >>> color.discard('black') >..
[해커랭크(HackerRank)] Japan Population (MySQL) 문제 Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN. Input Format The CITY table is described as follows: 코드 SELECT sum(population) FROM City WHERE countrycode = 'JPN' city 테이블에서 모든 일본 도시의 인구 합계를 출력하라 일본의 country code = 'JPN' 출처
[Python] list에 다른 변수 할당할 경우 주의할 점 list에 다른 변수 할당할 경우 주의할 점 python에서 list를 다른 변수에 할당하게 되면, 참조형태로 전달되기 때문에 실제로 같은 물리공간에 위치한 데이터를 가리킨다. new_scores 리스트의 값이 변경될 경우 scores 리스트도 함께 변경된다 # 원본 데이터와 같은 주소를 바라보게 됨 >>> scores = [1, 2, 3, 4, 5] >>> new_scores = scores # 값을 복사하는것이 아닌 위치만 참조하는것 >>> new_scores.append(6) >>> new_scores [1, 2, 3, 4, 5, 6] >>> scores [1, 2, 3, 4, 5, 6] 해결방법 참조하는 형태가 아닌 값을 복사하는 형태로 값을 가져오려면 copy모듈 또는 slice[:]를 사용하면..