comparing two lists and returning "position"
Hi there, I have a 2 lists.. for simplicities sake lets say the are: l1 = [ 'abc' 'ghi' 'mno' ] l2 = [ 'abc' 'def' 'ghi' 'jkl 'mno' 'pqr'] what I need to do is compare l1 against l2 and return the "position" of where each object in l1 is in l2 ie: pos = 0, 2, 4 Thanks in advance, -h -- http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists and returning "position"
On Jun 22, 1:46 am, Charles Sanders <[EMAIL PROTECTED]> wrote: > Paul Rubin wrote: > > > from itertools import izip > > pos = map(dict(izip(l2, count())).__getitem__, l1) > > or probably less efficiently ... > > >>> l1 = [ 'abc', 'ghi', 'mno' ] > >>> l2 = [ 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqr'] > >>> pos = [ l2.index(i) for i in l1 ] > >>> print pos > [0, 2, 4] > > Charles Hey Guys thanks for the feedback and the suggestions. Charles I got your implementation to work so many thanks for this. this is what I had so far for spam in l1: for eggs in l2: if spam == eggs: print "kaka", spam, eggs so its almost working just need the index, I'll continue playing with the nested loop approach for a bit more. Thanks once again guys -- http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists and returning "position"
On Jun 22, 2:16 am, hiro <[EMAIL PROTECTED]> wrote: > On Jun 22, 1:46 am, Charles Sanders <[EMAIL PROTECTED]> > wrote: > > > Paul Rubin wrote: > > > > from itertools import izip > > > pos = map(dict(izip(l2, count())).__getitem__, l1) > > > or probably less efficiently ... > > > >>> l1 = [ 'abc', 'ghi', 'mno' ] > > >>> l2 = [ 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqr'] > > >>> pos = [ l2.index(i) for i in l1 ] > > >>> print pos > > [0, 2, 4] > > > Charles > > Hey Guys thanks for the feedback and the suggestions. > Charles I got your implementation to work so many thanks for this. > > this is what I had so far > > for spam in l1: > for eggs in l2: > if spam == eggs: > print "kaka", spam, eggs > > so its almost working just need the index, I'll > continue playing with the nested loop approach for a bit more. > > Thanks once again guys Hi once again, Charles.. I have tried your approach in my data set l2 and it keeps crashing on me, bare in mind that I have a little over 10 million objects in my list (l2) and l1 contains around 4 thousand objects.. (i have enough ram in my computer so memory is not a problem) python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 error is : ValueError: list.index(x): x not in list when using Charles's pos = [ l2.index(i) for i in l1 ] print pos does anybody know of if I have to many data points ? the nested for loop approach seems to be working(still have get the index "position" returned though) Charles's approach works fine with less data. Cheers, -d -- http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists and returning "position"
On Jun 22, 1:56 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, hiro wrote: > > Hi once again, Charles.. I have tried your approach in my data set l2 > > and it keeps crashing on me, > > bare in mind that I have a little over 10 million objects in my list > > (l2) and l1 contains around 4 thousand > > objects.. (i have enough ram in my computer so memory is not a > > problem) > > > python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit > > (Intel)] on win32 > > > error is : ValueError: list.index(x): x not in list > > So you are saying you get this error with the value of `x` actually in the > list!? Somehow hard to believe. > > Ciao, > Marc 'BlackJack' Rintsch yes I do -- http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists and returning "position"
On Jun 22, 1:58 pm, hiro <[EMAIL PROTECTED]> wrote: > On Jun 22, 1:56 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > > > > > In <[EMAIL PROTECTED]>, hiro wrote: > > > Hi once again, Charles.. I have tried your approach in my data set l2 > > > and it keeps crashing on me, > > > bare in mind that I have a little over 10 million objects in my list > > > (l2) and l1 contains around 4 thousand > > > objects.. (i have enough ram in my computer so memory is not a > > > problem) > > > > python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit > > > (Intel)] on win32 > > > > error is : ValueError: list.index(x): x not in list > > > So you are saying you get this error with the value of `x` actually in the > > list!? Somehow hard to believe. > > > Ciao, > > Marc 'BlackJack' Rintsch > > yes I do I doubled, trippled check my data already (even doing a search by hand using vim) and the data is fine. Still looking into it though -- http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists and returning "position"
On Jun 22, 2:00 pm, hiro <[EMAIL PROTECTED]> wrote: > On Jun 22, 1:58 pm, hiro <[EMAIL PROTECTED]> wrote: > > > > > On Jun 22, 1:56 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > > > > In <[EMAIL PROTECTED]>, hiro wrote: > > > > Hi once again, Charles.. I have tried your approach in my data set l2 > > > > and it keeps crashing on me, > > > > bare in mind that I have a little over 10 million objects in my list > > > > (l2) and l1 contains around 4 thousand > > > > objects.. (i have enough ram in my computer so memory is not a > > > > problem) > > > > > python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit > > > > (Intel)] on win32 > > > > > error is : ValueError: list.index(x): x not in list > > > > So you are saying you get this error with the value of `x` actually in the > > > list!? Somehow hard to believe. > > > > Ciao, > > > Marc 'BlackJack' Rintsch > > > yes I do > > I doubled, trippled check my data already (even doing a search by hand > using vim) and the data is fine. Still looking into it though hahaha, K found out what was wrong.. in the function computing the data for l1 there was extra space was being put in. ie: l1 = [ 'abc ' 'ghi ' 'mno ' ] and I didn't strip it properly after splitting it.. silly me, well.. live and learn.. thanks guys Cheers, -h -- http://mail.python.org/mailman/listinfo/python-list
python noob, multiple file i/o
Hi there, I'm very new to python, the problem I need to solve is whats the "best/ simplest/cleanest" way to read in multiple files (ascii), do stuff to them, and write them out(ascii). -- import os filePath = ('O:/spam/eggs/') for file in os.listdir(filePath): #straight from docs # iterate the function through all the files in the directory # write results to separate files <- this is where I'm mostly stuck. -- For clarity's sake, the file naming conventions for the files I'm reading from are file.1.txt -> file.nth.txt It's been a long day, i'm at my wits end, so I apologize in advance if I'm not making much sense here. syntax would also be great if you can share some recipes. Thanks in advance. h. -- http://mail.python.org/mailman/listinfo/python-list
Re: python noob, multiple file i/o
Thanks a lot for the help guys, I'm at work right now and I will go over your suggestions one by one this weekend. Being more alert now, taking a look at the examples you posted, I now see how to approach this problem. The thing with python that I'm starting to realize is that there are a million different ways to approach a problem, so I find it great for experimenting (when time allows) yet very challenging to choose an approach. Cheers, h. -- http://mail.python.org/mailman/listinfo/python-list
Re: python noob, multiple file i/o
Thanks a lot for the help guys, I'm at work right now and I will go over your suggestions one by one this weekend. Being more alert now, taking a look at the examples you posted, I now see how to approach this problem. The thing with python that I'm starting to realize is that there are a million different ways to approach a problem, so I find it great for experimenting (when time allows) yet very challenging to choose an approach. Cheers, h. -- http://mail.python.org/mailman/listinfo/python-list
file io (lagged values) newbie question
Hey there, I'm currently doing data preprocessing (generating lagged values for a time series) and I'm having some difficulties trying to write a file to disk. A friend of mine, wrote this quick example for me: --- array = ['1','2','3','4','5','6','7'] lineSize = 4 skip = 4 condition = 1 startIndex = 0 for letter in array: line = [] startIndex = array.index(letter) for indexNum in range(startIndex, startIndex + (skip - 1), 1): #print "first loop" #print if indexNum > (len(array) - 1): break else: line.append(array[indexNum]) #print "startIndex" #print startIndex + skip for indexNum in range(startIndex + skip, (startIndex + lineSize) + 1, 1): #print "second loop" #print if indexNum > (len(array) - 1): break else: line.append(array[indexNum]) print line -- which outputs to the console: ['1', '2', '3', '5'] ['2', '3', '4', '6'] ['3', '4', '5', '7'] ['4', '5', '6'] ['5', '6', '7'] ['6', '7'] ['7'] This is exactly what I want and need, but when modified to read and write files from/to disk, I run into problems. example text file for reading: C:\>more kaka.txt 1 2 3 4 5 6 7 tweaked code: --- f=open('c:/kaka.txt','r') array=f.readlines() f.close() f=open('c:/kakaDump.txt','w') lineSize = 4 skip = 4 condition = 1 startIndex = 0 for letter in array: line = [] startIndex = array.index(letter) for indexNum in range(startIndex, startIndex + (skip - 1), 1): if indexNum > (len(array) - 1): break else: line.append(array[indexNum]) for indexNum in range(startIndex + skip, (startIndex + lineSize) + 1, 1): if indexNum > (len(array) - 1): break else: line.append(array[indexNum]) f.writelines(line) --- C:\>more kakaDump.txt 1 2 3 5 2 3 4 6 3 4 5 74 5 6 5 6 76 77 For those familiar with neural networks, the input file is a time series and the output file needs to have 3 lagged variables for training and a (two time steps ahead) variable for the target. Ie: input file 1 2 3 4 5 6 7 output file 1 2 3 5 2 3 4 6 3 4 5 7 4 5 6 5 6 7 6 7 7 Thanks in advanced, D. -- http://mail.python.org/mailman/listinfo/python-list