Iteracja wszystkich par kolejnych pozycji na liście [duplikat]
Podano listę
l = [1, 7, 3, 5]
Chcę iterować wszystkie pary kolejnych pozycji listy (1,7), (7,3), (3,5)
, tj.
for i in xrange(len(l) - 1):
x = l[i]
y = l[i + 1]
# do something
Chciałbym to zrobić w bardziej kompaktowy sposób, jak
for x, y in someiterator(l): ...
Czy istnieje sposób, aby to zrobić używając wbudowanych iteratorów Pythona? Jestem pewien, że moduł itertools
powinien mieć rozwiązanie, ale nie mogę tego rozgryźć.
7 answers
Wystarczy użyć zip
>>> l = [1, 7, 3, 5]
>>> for first, second in zip(l, l[1:]):
... print first, second
...
1 7
7 3
3 5
Zgodnie z sugestią możesz rozważyć użycie funkcji izip
w itertools
dla bardzo długich list, gdzie nie chcesz tworzyć nowej listy.
import itertools
for first, second in itertools.izip(l, l[1:]):
...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 08:45:47
Spójrz na pairwise
na przepisy itertools: http://docs.python.org/2/library/itertools.html#recipes
Cytowanie stamtąd:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
Wersja Ogólna
Ogólna wersja, która daje krotki dowolnego dodatniego rozmiaru naturalnego, może wyglądać tak:
def nwise(iterable, n=2):
iters = tee(iterable, n)
for i, it in enumerate(iters):
next(islice(it, i, i), None)
return izip(*iters)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 09:06:30
Stworzyłbym generator generyczny grouper
, taki jak ten
def grouper(input_list, n = 2):
for i in xrange(len(input_list) - (n - 1)):
yield input_list[i:i+n]
Sample run 1
for first, second in grouper([1, 7, 3, 5, 6, 8], 2):
print first, second
wyjście
1 7
7 3
3 5
5 6
6 8
Sample run 1
for first, second, third in grouper([1, 7, 3, 5, 6, 8], 3):
print first, second, third
wyjście
1 7 3
7 3 5
3 5 6
5 6 8
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 08:51:16
Uogólnienie podejścia sberry do nwise ze zrozumieniem:
def nwise(lst, k=2):
return list(zip(*[lst[i:] for i in range(k)]))
Eg
nwise(list(range(10)),3)
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9)]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-01-04 21:12:51
Prostym sposobem, aby to zrobić bez zbędnego kopiowania, jest generator, który przechowuje poprzedni element.
def pairs(iterable):
"""Yield elements pairwise from iterable as (i0, i1), (i1, i2), ..."""
it = iter(iterable)
try:
prev = next(it)
except StopIteration:
return
for item in it:
yield prev, item
prev = item
W przeciwieństwie do rozwiązań opartych na indeksach, działa to na dowolnych iterowalnych, w tym tych, dla których indeksowanie nie jest obsługiwane (np. generator) lub wolne (np. collections.deque
).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-20 13:00:57
Możesz użyć
.
>>> list(zip(range(5), range(2, 6)))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
Podobnie jak Zamek błyskawiczny tworzy pary. Więc, aby wymieszać dwie listy, otrzymujesz:
>>> l = [1,7,3,5]
>>> list(zip(l[:-1], l[1:]))
[(1, 7), (7, 3), (3, 5)]
Potem iteracja idzie jak
for x, y in zip(l[:-1], l[1:]):
pass
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 08:48:19
Jeśli chcesz coś inline, ale nie jest strasznie czytelny tutaj jest inne rozwiązanie, które korzysta z generatorów. Spodziewam się też, że nie jest to najlepszy występ: - /
Konwertuj listę do generatora z poprawką, aby zakończyć przed ostatnią pozycją:
gen = (x for x in l[:-1])
Przelicz na pary:
[(gen.next(), x) for x in l[1:]]
To wszystko, czego potrzebujesz.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-12-28 18:07:59