블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
  • total
  • today
  • yesterday

Category

'Code/Python'에 해당되는 글 3건

  1. 2007.08.03 2차원 행렬 테스트
  2. 2007.08.01 Lists
  3. 2007.07.11 생성자와 소멸자
2007. 8. 3. 16:37 Code/Python
code:
s = 10
c = range(s)

for y in range(s):
    c[y] = [ [x,y] for x in range(s) ]

print c

run:
[[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1], [8, 1], [9, 1]],
[[0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2]],
[[0, 3], [1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3], [8, 3], [9, 3]],
[[0, 4], [1, 4], [2, 4], [3, 4], [4, 4], [5, 4], [6, 4], [7, 4], [8, 4], [9, 4]],
[[0, 5], [1, 5], [2, 5], [3, 5], [4, 5], [5, 5], [6, 5], [7, 5], [8, 5], [9, 5]],
[[0, 6], [1, 6], [2, 6], [3, 6], [4, 6], [5, 6], [6, 6], [7, 6], [8, 6], [9, 6]],
[[0, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [7, 7], [8, 7], [9, 7]],
[[0, 8], [1, 8], [2, 8], [3, 8], [4, 8], [5, 8], [6, 8], [7, 8], [8, 8], [9, 8]],
[[0, 9], [1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [6, 9], [7, 9], [8, 9], [9, 9]]]


그런데, 다음과 같이 하면 전혀 다른 결과가 나온다.

s = 10
c = range(s)

for y in range(s):
    for x in range(s):
        c[y] = [x,y]

    print c

[[9, 0], 1, 2, 3, 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], 2, 3, 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], 3, 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], [9, 9]]


'Code > Python' 카테고리의 다른 글

Lists  (0) 2007.08.01
생성자와 소멸자  (0) 2007.07.11
posted by maetel
2007. 8. 1. 13:17 Code/Python
ref. 이강성 <열혈강의 파이썬> 5장 리스트

List 리스트: 순서를 가지는 객체들의 집합.

1) 시퀀스 자료형이면서 변경 가능(mutable) 형이다.
    : 시퀀스 자료형의 일반적인 특징(인덱싱, 슬라이싱, 연결, 반복, 멤버십 테스트 등)을 지원하며, 변경이 가능한 특성에 따라 자료의 크기를 동적으로 임의 조절하거나, 내용을 치환하여 변경할 수 있다.
 
2) 리스트는 다른 객체를 직접 저장하지 않고, 객체들의 레퍼런스(Reference)만을 저장한다. (레퍼런스란 객체의 주소를 말한다.)


ref. Guido van Rossum's Python Tutorial: 3.1.4 Lists

- built-in function len()
- to nest lists (create lists containing other lists)
- object semantics

5.1 More on Lists

append(x)
extend(L)
insert(i, x)
remove(x)
pop([i])
index(x)
count(x)
sort(x)
reverse()


Stack 스택: 나중에 넣은 데이터를 먼저 꺼내도록 되어 있는 메모리 구조
Queue 큐: 먼저 넣은 데이터를 먼저 꺼내도록 되어 있는 메모리 구조
    - push 연산 (append 메쏘드) &pop 연산 (pop 메쏘드)


filter(function, sequence)

map(function, sequence)

reduce(function, sequence)


ref. NodeBox | Lists

choice(list) 
    = list[random(len(list)]

'Code > Python' 카테고리의 다른 글

2차원 행렬 테스트  (0) 2007.08.03
생성자와 소멸자  (0) 2007.07.11
posted by maetel
2007. 7. 11. 20:37 Code/Python
이강성 <파이썬> 379p - 생성자와 소멸자

from time import time, ctime, sleep

class Life:
    def __init__(self):
        self.birth = ctime()
        print 'Birthday', self.birth
    def __del__(self):
        print 'Deathday', ctime()
        
def test():
    mylife = Life()
    print 'Sleepling for 3 sec'
    sleep(3)
    
test()

실행하면
Birthday Wed Jul 11 20:29:36 2007
Sleepling for 3 sec
Deathday Wed Jul 11 20:29:39 2007


test 함수가 불리면서 Life 클래스 인스턴스가 하나 생성된다. 이때 자동으로 __init__ 함수가 호출되어 Birthday를 츨력한다. 3초 후에 test 함수가 종료하면서 (mylife는 지역 이름이므로 함수가 종료되면서 사라진다.) 인스턴스가 소멸된다. 이때 자동으로 __del__ 함수가 호출된다.

'Code > Python' 카테고리의 다른 글

2차원 행렬 테스트  (0) 2007.08.03
Lists  (0) 2007.08.01
posted by maetel