python memo

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

Python memo
python memo
Python memo
python memo
Python memo
Python memo
Python memo
[Python] Memo dictionary
python beginner memo (9.2-10)
python beginner memo (9.1)
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
[Python] Numpy memo
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Python module (Python learning memo ④)
Visualization memo by Python
Python
Python test package memo
[Python] Memo about functions
python regular expression memo
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors
DynamoDB Script Memo (Python)
Python basic memo --Part 2
python recipe book Memo
Basic Python command memo
Python OpenCV tutorial memo
Python basic grammar memo
TensorFlow API memo (Python)
python useful memo links
Python decorator operation memo
Python basic memo --Part 1
Effective Python Memo Item 3
Divisor enumeration Python memo
Python memo (for myself): Array
Python execution time measurement memo
Twitter graphing memo with Python
[Line / Python] Beacon implementation memo
Python and ruby slice memo
Python Basic Grammar Memo (Part 1)
Python code memo for yourself
Raspberry Pi + Python + OpenGL memo
Python basic grammar (miscellaneous) Memo (3)
Python immutable type int memo
Python memo using perl --join
Python data type summary memo
Python basic grammar (miscellaneous) Memo (2)
[MEMO] [Development environment construction] Python
[Python] virtualenv creation procedure memo
Python basic grammar (miscellaneous) Memo (4)
Regarding speeding up python (memo)
gzip memo