Test.py
#Generate a multidimensional list for testing
xss = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print xss
#First output in a normal loop
yss = []
for xs in xss:
ys = []
for x in xs:
ys.append(x * 2)
yss.append(ys)
print yss
#Output in list comprehension notation
print [[x * 2 for x in xs] for xs in xss]
#Flat output (changed to one-dimensional array)
print [x * 2 for xs in xss for x in xs]
#set output
print {x / 2 for xs in xss for x in xs}
#Dictionary output
print {x:str(x * 2) for xs in xss for x in xs}
#Generate output
it = (x * 2 for xs in xss for x in xs)
print it.next()
print it.next()
def gen():
for xs in xss:
for x in xs:
yield x * 2
it = gen()
print it.next()
print it.next()
#Sort test
print sorted(gen())
print sorted([[x * 2 for x in xs] for xs in xss], reverse=True)
print [1, 2] == [1, 2]
print [1, 2] < [1, 2, 3]
print [1, 2, -1, 3, 4] > [1, 2, 3]
#Variadic argument
# *Is a list
# **Is a dictionary
def f(*args, **kwargs):
print args
print kwargs
f(1, 2, 3, a=10, b=20, c=30)
#Passing test of various arguments
def g(a, b, c):
print a, b, c
g(10, b=20, c=30)
x = [1, 2, 3]
#Normal way of passing
g(x[0], x[1], x[2])
#How to pass as a list
g(*x)
y = {'a': 10, 'b': 20, 'c': 30}
#Normal way of passing
g(a=y['a'], b=y['b'], c=y['c'])
#How to pass as a dictionary
g(**y)
x = [1]
y = {'b': 20, 'c': 30}
g(*x, **y)
#format test
print '{hoge} {fuga}'.format(hoge=10, fuga=20)
print '{0} {1}'.format(*(value for value in range(0, 2)))
Recommended Posts