메모장

빅데이터 분산 컴퓨팅 정리(15~18강 python관련) 본문

교육(KOCW, 오프라인)

빅데이터 분산 컴퓨팅 정리(15~18강 python관련)

hiandroid 2017. 9. 5. 13:46
반응형

15강

python basics


operators

>>> 1+1

2


>>> 2*3

6


boolean operators

>>> 1==0

false


>>> not (1==0)

true


>>> (2==2) and (2==3)

false


>>> (2==2) or (2==3)

true


Strings관련

+ operator

>>> 'artificial' + "intelligence"

'artificialintelligence'


>>> 'artificial'.upper()

'ARTIFICIAL'


>>> 'HELP'.lower()

'help'


>>> len('Help')  length

4


>>> s = 'hello world'

>>> print s

hello world

>>> s.upper()

'HELLO WORLD'



lists관련

>>> fruits = ['apple','orange','pear','banana']

>>> fruits[0]

'apple'


>>> otherFruits = ['kiwi','strawberry']

>>> fruits + otherFruits

>>> ['apple','orange','pear','banana','kiwi','strawberry']


>>> fruits[-2]

'pear'


>>> fruits.pop()

>>> fruits.append('grapefruit')

>>> fruits

['apple','orange','pear','grapefruit']


>>> fruits[-1] = 'pineapple'  -1자리(grapefruit)에 파인애플을 집어넣음

>>> fruits

['apple','orange','pear','pineapple']


>>>fruits

['apple','orange','pear','pineapple']


>>> fruits[0:2]

['apple','orange']


>>> fruits[:3]

['apple','orange','pear']


>>> fruits[2:]

['pear','pineapple']


>>> len(fruite)

4


>>> lstOfLsts = [['a','b','c'],[1,2,3],['one','two','three']]

>>> lstOfLsts[1][2]

3


>>> lstOfLsts[0].pop()

'c'


>>> lstOfLsts

[['a','b'],[1,2,3],['one','two','three']]


>>> lst = ['a','b','c']

>>> lst.reverse()

>>> ['c','b','a']



set

>>> shapes = ['circle','square','triangle','circle']

>>> setOfShapes = set(shapes)

>>> setOfShapes

set(['circle', 'square',' triangle'])


>>> setOfShapes.add('polygon')

set(['circle','polygon','square','triangle'])


>>> 'circle' in setOfShapes

True


>>> 'test' in setOfShapes

False


>>> studentIds = {"knuth": 42.0, "turing": 56.0, "nash": 92.0}

>>> studentIds

{'knuth': 42.0, 'nash': 92.0, 'turing': 56.0}


>>> studentIds["turing"]

56.0


>>> studentIds['turing']

56.0


>>> studentIds['nash'] = 'ninety-tow'

>>> studentIds

{'knuth': 42.0, 'nash': 'ninety-tow', 'turing': 56.0}


>>> del studentIds['knuth']

>>> studentIds

{'nash': 'ninety-tow', 'turing': 56.0}


>>> studentIds.keys()

['nash', 'turing']


>>> studentIds.values()

['ninety-tow', 56.0]


>>> len(studentIds)

2


>>> studentIds.items()
[('nash', 'ninety-tow'), ('turing', 56.0)]


>>> fruits = ['apples','oranges','pears','bananas']

>>> for fruit in fruits:

...     print fruit + ' for sale'

... 

apples for sale

oranges for sale

pears for sale

bananas for sale



>>> fruitPrices = {'apples': 2.00, 'oranges': 1.50, 'pears': 1.75}

>>> fruitPrices.items()

[('pears', 1.75), ('apples', 2.0), ('oranges', 1.5)]

>>> for fruit, price in fruitPrices.items():

...     if price < 2.00:

...             print '%s cost %f a pound' % (fruit, price)

...     else:

...             print fruit + 'are too expensive'

... 

pears cost 1.750000 a pound

applesare too expensive

oranges cost 1.500000 a pound




mutable(add나 pop같은걸 할 수있음)

list

set

dictionary


immutable(add나 pop같은걸 못함)

String

tuple

integer


lambda: 이름이 없는 function. 한번밖에 사용못하는 function


결과를 출력

>>> map(lambda x: x * x, [1,2,3])

[1,4,9]


true것만 나오도록 필터링

>>> filter(lambda x: x > 3, [1,2,3,4,5,4,3,2,1])



list comprehension

 - 간단하게 쓸 수 있음


>>> nums = [1,2,3,4,5,6]

>>> plusOneNums = [x+1 for x in nums]

[2,3,4,5,6,7]


>>> oddNums = [x for x in nums if x %2 == 1]

>>> print oddNums

[1,3,5]


>>> oddNumsPlusOne = [x+1 for x in nums if x %2 ==1]

>>> print oddNumsPlusOne

[2,4,6]


function


>>> fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75}

>>> fruitPrices

{'pears': 1.75, 'apples': 2.0, 'oranges': 1.5}


>>> def buyFruit(fruit, numPounds):

...     if fruit not in fruitPrices:

...             print "sorry we don`t have %s" % (fruit)

...     else:

...             cost = fruitPrices[fruit] * numPounds

...             print "that`ll be %f please" % (cost)

... 


>>> buyFruit

<function buyFruit at 0x7f32ebcb5500>

 

>>> if __name__ == '__main__':

...     buyFruit('apples',2.4)

...     buyFruit('coconuts',2)

... 

that`ll be 4.800000 please

sorry we don`t have coconuts



반응형