LoginSignup
0
0

More than 1 year has passed since last update.

リストで指定要素を先頭に持ってくる(メモです)

Posted at

リストで指定要素を先頭に持ってくる方法の一つです。マイメモとしてアップします。

from itertools import chain
import time
s = time.perf_counter()
x = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
q = 'g'
assert q in x
qi = x.index(q)
print(qi)
y = x[qi],x[:qi],x[qi+1:]
print(y)
z = list(chain.from_iterable(list(y)))
print(z)
e = time.perf_counter()
print(e -s)

結果
6
('g', ['a', 'b', 'c', 'd', 'e', 'f'], [])
['g', 'a', 'b', 'c', 'd', 'e', 'f']
0.00038519999998243293

終わり

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0