Created
November 7, 2016 14:50
-
-
Save recuraki/f43c021c39fa3450401c4acadcb7510f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = [100,200,201,202,203,301,303,305,306,401,402,403,405,501,502,601,602,603,701] | |
""" | |
下の4つについてtimeitしたけど、どう考えても、 | |
r,s = s[:9], s[9:] | |
が早いです。本当にありがとうございました。 | |
2.260901534450575 | |
6.795336202391907 | |
1.9528116482399511 | |
9.414626154138594 | |
""" | |
from itertools import zip_longest | |
import timeit | |
n=1000000 | |
######################################### | |
# zip_longestっていうのがあった | |
# [[100, 200, 201, 202, 203, 301, 303, 305, 306], [401, 402, 403, 405, 501, 502, 601, 602, 603], [701, None, None, None, None, None, None, None, None]] | |
# 惜しい!違う!そうじゃないんだ!Noneいらないから! | |
x=''' | |
l = [100,200,201,202,203,301,303,305,306,401,402,403,405,501,502,601,602,603,701] | |
[list(x) for x in zip_longest(*[iter(l)]*9)] | |
''' | |
print(timeit.timeit(stmt=x, setup="from itertools import zip_longest", number=n)) | |
######################################### | |
# 一応できた | |
# [[100, 200, 201, 202, 203, 301, 303, 305, 306], [401, 402, 403, 405, 501, 502, 601, 602, 603], [701]] | |
x=''' | |
l = [100,200,201,202,203,301,303,305,306,401,402,403,405,501,502,601,602,603,701] | |
[list(filter(lambda a: a is not None, x)) for x in zip_longest(*[iter(l)]*9)] | |
''' | |
print(timeit.timeit(stmt=x, setup="from itertools import zip_longest", number=n)) | |
######################################### | |
# こうかなー。 | |
# [[100, 200, 201, 202, 203, 301, 303, 305, 306], [401, 402, 403, 405, 501, 502, 601, 602, 603], [701]] | |
x = ''' | |
l = [100,200,201,202,203,301,303,305,306,401,402,403,405,501,502,601,602,603,701] | |
def f(s, l = 9): | |
while s: | |
r,s = s[:9], s[9:] | |
yield r | |
[x for x in f(l)] | |
''' | |
print(timeit.timeit(stmt=x, number=n)) | |
######################################### | |
# 内包リスト表記は無理やりか | |
# [[100, 200, 201, 202, 203, 301, 303, 305, 306], [401, 402, 403, 405, 501, 502, 601, 602, 603], [701]] | |
x=''' | |
l = [100,200,201,202,203,301,303,305,306,401,402,403,405,501,502,601,602,603,701] | |
def f(s,l = 9): | |
s = s.copy() # popするから破壊しないようにする | |
while s: | |
yield [s.pop(0) for i in range(len(s) if len(s) < l else l)] | |
[x for x in f(l)] | |
''' | |
print(timeit.timeit(stmt=x, number=n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment