본문 바로가기

python34

[Python3] two list for loop 두 개의 리스트를 동시에 루프를 돌며 갚을 얻어오고 싶은 경우, zip() 함수를 이용하면 된다. >>> li = { ... 'email': ['1', '2', '3', '4'], ... 'name': ['a', 'b', 'c', 'd'] ... } >>> for email, name in zip(li['email'], li['name']): ... print(email, name) ... 1 a 2 b 3 c 4 d for num, cheese, color in zip([1,2,3], ['manchego', 'stilton', 'brie'], ['red', 'blue', 'green']): print('{} {} {}'.format(num, color, cheese)) 1 red manchego 2 b.. 2016. 11. 8.
[Python] call by assignment (call by object, call by object reference) 공식 문서에는 Python은 call by assignment라고 되었다. Python은 모든 것이 객체(object)이고 객체에는 두 가지 종류가 있다. 1. immutable objectint, float, tuples 등이 함수 arguments로 넘어갈 땐 call by value로 넘어감. 2. mutable objectlist, dict, set과 같이 mutable object 가 argument로 넘어가면 object reference가 넘어가서 담고 있는 값이 바뀔 수도 있다. >>> def spam(eggs): ... eggs.append(1) ... eggs = [2, 3] ... >>> ham = [0] >>> spam(ham) >>> print(ham) [0, 1] eggs.app.. 2016. 10. 26.
[Django] values() values_list() flat values()object의 원하는 컬럼만 가져오기 위해서는 values를 사용할 수 있다. values를 사용하면 해당 컬럼의 key,value의 쌍의 리스트를 얻을 수 있다. >>> Entry.objects.values()[{'blog_id': 1, 'headline': 'First Entry', ...}, ...] >>> Entry.objects.values('blog')[{'blog': 1}, ...] >>> Entry.objects.values('blog_id')[{'blog_id': 1}, ...] values_list()values_list()를 사용하면 key,value 형태가 아닌 tuples 형태의 리스트로 가져올 수 있다. >>> Entry.objects.values_list('id'.. 2016. 10. 26.
[Python] RabbitMQ Publish Subscribe RabbitMQ 에 Publish/Subscribe 하는 방식입니다.Publishtopic 으로 호출하는 방식import pika import json class Publish(object): def __init__(self): self.connection = None self.channel = None self.topic = None def is_connect(self, host, topic=None): try: self.topic = topic self.connection = pika.BlockingConnection(pika.ConnectionParameters( host=host )) self.channel = self.connection.channel() self.channel.queue_dec.. 2016. 10. 26.