Python Syntax 4 for A.I.
>>> items = 'zero one two three'.split() # 빈칸을 기준으로 문자열 나누기
>>> print (items)
['zero', 'one', 'two', 'three']
>>> example = 'python,java,javascript' # ","을 기준으로 문자열 나누기
>>> example.split(",")
['python', ‘java', 'javascript']
>>> a, b, c = example.split(",")
# 리스트에 있는 각 값을 a,b,c 변수로 unpacking
>>> example = ‘teamlab.technology.io'
>>> subdomain, domain, tld = example.split('.')
# "."을 기준으로 문자열 나누기 → Unpacking
>>> colors = ['red', 'blue', 'green', 'yellow']
>>> result = ''.join(colors)
>>> result
'redbluegreenyellow'
>>> result = ' '.join(colors) # 연결 시 빈칸 1칸으로 연결
>>> result
'red blue green yellow'
>>> result = ', '.join(colors) # 연결 시 ", "으로 연결
>>> result
'red, blue, green, yellow'
>>> result = '-'.join(colors) # 연결 시 "-"으로 연결
>>> result
'red-blue-green-yellow'

#2 nested For loop
>>> word_1 = "Hello"
>>> word_2 = "World"
>>> result = [i+j for i in word_1 for j in word_2]
# Nested For loop
>>> result
['HW', 'Ho', 'Hr', 'Hl', 'Hd', 'eW', 'eo', 'er',
'el', 'ed', 'lW', 'lo', 'lr', 'll', 'ld', 'lW',
'lo', 'lr', 'll', 'ld', 'oW', 'oo', 'or', 'ol', 'od']
#3 if not 구문 마지막에 넣음으로써 filter함
>>> case_1 = ["A","B","C"]
>>> case_2 = ["D","E","A"]
>>> result = [i+j for i in case_1 for j in case_2]
>>> result
['AD', 'AE', 'AA', 'BD', 'BE', 'BA', 'CD', 'CE', 'CA']
>>> result = [i+j for i in case_1 for j in case_2 if not(i==j)]
# Filter: i랑 j과 같다면 List에 추가하지 않음
# [i+j if not(i==j) else i for i in case_1 for j in case_2]
>>> result
['AD', 'AE', 'BD', 'BE', 'BA', 'CD', 'CE', 'CA']
>>> result.sort()
>>> result
['AD', 'AE', 'BA', 'BD', 'BE', 'CA', 'CD', 'CE']
#4 2d array
>>> words = 'The quick brown fox jumps over
the lazy dog'.split()
# 문장을 빈칸 기준으로 나눠 list로 변환
>>> print (words)
['The', 'quick', 'brown', 'fox', 'jumps',
'over', 'the', 'lazy', 'dog']
>>>
>>> stuff = [[w.upper(), w.lower(), len(w)]
for w in words]
# list의 각 elemente들을 대문자, 소문자, 길이로 변
환하여 two dimensional list로 변환
>>> for i in stuff:
... print (i)
...
['THE', 'the', 3]
['QUICK', 'quick', 5]
['BROWN', 'brown', 5]
['FOX', 'fox', 3]
['JUMPS', 'jumps', 5]
['OVER', 'over', 4]
['THE', 'the', 3]
['LAZY', 'lazy', 4]
['DOG', 'dog', 3]
#5 1d vs 2d
>>> case_1 = ["A","B","C"]
>>> case_2 = ["D","E","A"]
['AD', 'AE', 'AA', 'BD', 'BE', 'BA', 'CD', 'CE', 'CA']
>>> result = [i+j for i in case_1 for j in case_2]
>>> result
['AD', 'AE', 'AA', 'BD', 'BE', 'BA', 'CD', 'CE', 'CA']
>>> result = [ [i+j for i in case_1] for j in case_2]
>>> result
[['AD', 'BD', 'CD'], ['AE', 'BE', 'CE'], ['AA', 'BA', 'CA']]
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
# list의 있는 index와 값을 unpacking
... print (i, v)
...
0 tic
1 tac
2 toe
>>> text = 'Artificial intelligence (AI), is intelligence demonstrated by machines, unlike the natural intelligence displayed by humans and animals.'
>>> {i:j for i,j in enumerate(text.split())}
# 문장을 list로 만들고 list의 index와 값을 unpacking하여 dict로 저장
{0: 'Artificial', 1: 'intelligence', 2: '(AI),', 3: 'is', 4: 'intelligence', 5: 'demonstrated', 6: 'by',
7: 'machines,', 8: 'unlike', 9: 'the', 10: 'natural', 11: 'intelligence', 12: 'displayed', 13: 'by', 14:
'humans', 15: 'and', 16: 'animals.'}
>>> alist = ['a1', 'a2', 'a3']
>>> blist = ['b1', 'b2', 'b3']
>>> for a, b in zip(alist, blist): # 병렬적으로 값을 추출
... print (a,b)
...
a1 b1
a2 b2
a3 b3
>>> alist = ['a1', 'a2', 'a3']
>>> blist = ['b1', 'b2', 'b3']
>>>
>>> for i, (a, b) in enumerate(zip(alist, blist)):
... print (i, a, b) # index alist[index] blist[index] 표시
...
0 a1 b1
1 a2 b2
2 a3 b3

ex = [1, 2, 3, 4, 5]
f = lambda x: x ** 2
list(map(f, ex))
#[1, 4, 9 , 16, 25]
[f(value) for value in ex]
#[1, 4, 9 , 16, 25]
ex = [1,2,3,4,5]
f = lambda x, y: x + y
print(list(map(f, ex, ex)))
#[2, 4, 6, 8, 10]

from functools import reduce
print(reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]))
#15
cities = ['Seoul', 'Busan', 'Jeju']
memory_address_cities = iter(cities)
print(memory_address_cities)
#<list_iterator at 0x160ace724f0>
next(memory_address_cities)
#'Seoul'
next(memory_address_cities)
#'Busan'
next(memory_address_cities)
#'Jeju'

def generator_list(value) :
result = []
for i in range(value) :
yield i
for a in generator_list(50) :
print(a)
gen_ex = (n*n for n in range(500))
print(type(gen_ex))
<class 'generator'>from sys import getsizeof
gen_ex = (n*n for n in range(500))
print(getsizeof(gen_ex))
print(getsizeof(list(gen_ex)))
list_ex = [n*n for n in range(500)]
print(getsizeof(list_ex))


>>> def kwargs_test_3(one, two, *args, **kwargs) :
... print(one + two + sum(args))
... print(kwargs)
...
>>> kwargs_test_3(3, 4, 5, 6, 7, 8, 9, first = 3, second = 4, third = 5)
42
{'first': 3, 'second': 4, 'third': 5}
#1
def asterisk_test(a, *args):
print(a, args)
print(type(args))
asterisk_test(1, *(2,3,4,5,6))
1 (2, 3, 4, 5, 6)
<class 'tuple'>
#2 * as unpacking
>>> def asterisk_test(a, args):
... print(a, *args)
... print(type(args))
... asterisk_test(1, (2,3,4,5,6))
...
1 2 3 4 5 6
<class 'tuple'>
#3 **
>>> def asterisk_test(a, b, c, d,):
... print(a, b, c, d)
... data = {"b":1 , "c":2, "d":3}
... asterisk_test(10, **data)
...
10 1 2 3
#4 * with zip
>>> for data in zip(*([1, 2], [3, 4], [5, 6])):
... print(data)
...
(1, 3, 5)
(2, 4, 6)