전체 글
-
[Python] Django urls.py, views.py 초기 세팅Python/Django 2021. 11. 30. 17:21
# urls.py of project foler from django.contrib import admin from django.urls import path, include from first_app import views urlpatterns = [ path('admin/', admin.site.urls), path('', include('first_app.urls')), ] # urls.py of app folder from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
-
[Python] TypeError: __init__() missing 1 required positional argument: 'on_delete'Python/Django 2021. 11. 30. 16:19
class Webpage(models.Model): topic = models.ForeignKey(Topic, on_delete=models.CASCADE,) name = models.CharField(max_length=264, unique=True) url = models.URLField(unique=True) Django 2.0 이후 부터는 ForeignKey를 사용할 경우, 2개의 파라미터를 받게 되어있다. on_delete를 사용할 경우에 대한 설정
-
[Programmers/ Python] 로또의 최고 순위와 최저 순위Algorithm 2021. 11. 30. 10:24
두개의 리스트가 주어진다. 당첨번호와, 내가 뽑은 로또 번호 리스트. 그런데 내가 뽑은 리스트에는 몇몇 번호가 0로 들어오는 이건 미지수이다. 그래서 내가 뽑은 번호와 당첨번호를 비교해서 미지수가 당첨일 경우와 당첨이 아닐 경우 두가지 경우의 수를 계산해서 반환해 주면된다 def solution(lottos, win_nums): rank = [6, 6, 5, 4, 3, 2, 1] unknown = lottos.count(0) # 미지수 구하기 match = 0 for x in win_nums: if x in lottos: match += 1 return rank[match + unknown], rank[match]
-
[Programmers/ Python] 완주하지 못한 선수Algorithm 2021. 11. 30. 09:30
개념은 쉽다 participant에서 completion을 빼주어 차집합을 찾으면 된다. 따라서 set를 이용해 해결하려 했으나 set는 중복된 값을 허용하지 않기 때문에 막혔다. 그래서 구글을 뒤지다가 collections.counter라는 모듈을 발견! counter이란? list안에 같은 이름을 가진 요소가 몇개 인지 dict 형태로 알려준다. 예를 들면 import collections participant = ["leo", "kiki", "eden"] completion = ["eden", "kiki"] p = collections.Counter(participant) c = collections.Counter(completion) print(p, c) => Counter({'leo': 1, '..
-
[Python] text를 index해보자 module : rePython 2021. 11. 24. 22:00
import re text = "Let me grab a coffee real quick" match = re.search("grab", text) print(match) => print(match.span()) => (7, 11) # 위치를 반환 Match = re.search("Hello World", text) print(Match) => None import re split_term = "@" email = "user@gmail.com" print(re.split(split_term, email)) => ['user', 'gmail.com'] https://docs.python.org/ko/3/library/re.html re — 정규식 연산 — Python 3.10.0 문서 scanf() 시뮬레..
-
[Python] with문에 대해 알아보자 (실행과 종료)Python 2021. 11. 24. 21:51
with statement: __enter__ 와 __exit__ 특수메소드를 가진 객체에 with 문을 쓸 수 있다. 자동으로 파일에 접근하고 수행하고 닫아준다. example: with {expression} as {variable}: 실행 with open('textfile.txt', 'r') as file: contents = file.read() # 위 구문과 동일한 내용 file = open('textfile.txt', 'r') contents = file.read() file.close()