4

Python returns a linear / cumulative pair of a list of numbers

 3 years ago
source link: https://www.codesd.com/item/python-returns-a-linear-cumulative-pair-of-a-list-of-numbers.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python returns a linear / cumulative pair of a list of numbers

advertisements

I have a pandas dataframe which has a messy concatenation of many different tables. I want to segment these tables out and perform operations on them. I have a list of table header locations that looks like this: [1, 4, 5, 7, 9, 12, 15] - so the header of the first table is at index 1, the header of the second table is at index 4, etc. My goal is to use this list to slice the dataframe and extract information from each slice and munge the data into something pretty.

I'm trying to get a list of pairs like this for this purpose: [[1,4], [4,5], [5,7], [7,9], [9,12], [12,15]]

I tried this function, but its not quite returning what I want, it returns pairs like this: 1,4, 5,7, 9,12 - this is making me skip over every other table :/.

def pairwise(iterable): #this is what is wrong
    a = iter(iterable)
    return izip(a, a)

Am I missing something? I'm going crazy here.


Why not just [[a[x], a[x+1]] for x in range(len(a)-1)]? assuming a= [1, 4, 5, 7, 9, 12, 15]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK