Get Pairs of List Python [duplicate]

2024/7/7 6:05:22
c = [1,2,3,4,5,6,7,8,9,10]for a,b in func(c):doSomething()

So func() have to return (1,2) (2,3) (3,4) ... (8,9) (9,10)

Is there a elegant method in python 2.7 to achieve this?

Answer

Sure, there are many ways. Simplest:

def func(alist):return zip(alist, alist[1:])

This spends a lot of memory in Python 2, since zip makes an actual list and so does the slicing. There are several alternatives focused on generators that offer memory savings, such as a very simple:

def func(alist):it = iter(alist)old = next(it, None)for new in it:yield old, newold = new

Or you can get fancier deploying the powerful itertools instead, as in the pairwise recipe proposed by @HughBothwell .

https://en.xdnf.cn/q/120156.html

Related Q&A

Python - make a list

How can I turn something like this: (not a file)Edit: Its what I get when I print this:adjusted_coordinate_list = str(adjusted_X_coordinate) + , + str(Y_coordinate)1,1 1,2 1,3 1,4 1,5into this:[1,1,1,2…

Error while running test case

I need to test my code below. I am using one test to see if it is working or not. but dont know what exactly I should pass as a parameter in the test code. Please see the test code at the end and pleas…

How to run something on each line of txt file?

I am trying to get this to run on each line of a .txt file.D1 =int(lines[0])*10 D2 =int(lines[1])*9 D3 =int(lines[2])*8 D4 =int(lines[3])*7 D5 =int(lines[4])*6 D6 =int(lines[5])*5 D7 =int(lines[6])*4 D…

Python np.nditer() - ValueError: Too many operands

I have a few methods which pass different amount of messy data to this function to combine headers with data and return a list of dictionaries:def zip_data(self, indicator_names, indicator_values):valu…

Python 3 - Find the Mode of a List

def mode(L):shows = []modeList = []L.sort()length = len(L)for num in L:count = L.count(num)shows.append(count)print List = , LmaxI = shows.index(max(shows))for i in shows:if i == maxI:if modeList == []…

Create list of sublists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Python error - list index out of range?

Anyone help me see why I keep getting "list index out of range" as an error ?? def printInfo(average):average.sort() # sorts the list of tuples average.reverse() # reverses the list of tu…

Access strings inside a list

A Python list has [12:30,12:45] and I want to access the 12:30 for the first iteration, and on the second iteration I should get 12:45.my_list=[12:30,12:45] for each_value in my_list:print(each_value[0…

How do I install NumPy under Windows 8.1?

How do I install NumPy under Windows 8.1 ? Similar questions/answers on overflow hasnt helped.

Design In-Memory File System - Python - Trie - Leetcode Error

I am trying to solve the below leetcode problem Design a data structure that simulates an in-memory file system. Implement the FileSystem class: FileSystem() Initializes the object of the system. List …