Test.py
#Générer une liste multidimensionnelle pour les tests
xss = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print xss
#Première sortie dans une boucle normale
yss = []
for xs in xss:
ys = []
for x in xs:
ys.append(x * 2)
yss.append(ys)
print yss
#Sortie en notation d'inclusion de liste
print [[x * 2 for x in xs] for xs in xss]
#Sortie plate (modifiée en tableau à 1 dimension)
print [x * 2 for xs in xss for x in xs]
#définir la sortie
print {x / 2 for xs in xss for x in xs}
#Sortie du dictionnaire
print {x:str(x * 2) for xs in xss for x in xs}
#Générer une sortie
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()
#Trier le 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]
#Argument de longueur variable
# *Est une liste
# **Est un dictionnaire
def f(*args, **kwargs):
print args
print kwargs
f(1, 2, 3, a=10, b=20, c=30)
#Réussir le test de divers arguments
def g(a, b, c):
print a, b, c
g(10, b=20, c=30)
x = [1, 2, 3]
#Manière normale de passer
g(x[0], x[1], x[2])
#Comment passer en liste
g(*x)
y = {'a': 10, 'b': 20, 'c': 30}
#Manière normale de passer
g(a=y['a'], b=y['b'], c=y['c'])
#Comment se faire passer pour un dictionnaire
g(**y)
x = [1]
y = {'b': 20, 'c': 30}
g(*x, **y)
#test de format
print '{hoge} {fuga}'.format(hoge=10, fuga=20)
print '{0} {1}'.format(*(value for value in range(0, 2)))
Recommended Posts