473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

looping through two list simultenously

folks
I have two lists

i am trying to loop thorough them simultenously.

Here is the code i am using

f1 = os.popen('ls chatlog*.out')
data1=f1.readlines()
f1.close()

data1=[x.strip() for x in data1]
f1 = os.popen('ls chatlog*.txt')
data=f1.readlines()
f1.close()
for eachline in data1 and line in data:

filename='/root/Desktop/project/'+ eachline
print filename
outfile=open(filename,'r')
filename1='/root/Desktop/project/' + line
print filename1

I get the error that line is not defined.
Traceback (most recent call last):
File "list.py", line 16, in ?
for eachline in data1 and line in data:
NameError: name 'line' is not defined

Is there any efficient doing this

Oct 29 '06 #1
5 1104
"CSUIDL PROGRAMMEr" <sy*********@yahoo.com>:
folks I have two lists

i am trying to loop thorough them simultenously.

Is there any efficient doing this
Try the built-in function zip.
>>zip(['a', 'b', 'c'], [1, 2, 3])
[('a', 1), ('b', 2), ('c', 3)]

--
Björn Lindström <bk**@stp.lingfil.uu.se>
Student of computational linguistics, Uppsala University, Sweden
Oct 29 '06 #2
folks
I have two lists

i am trying to loop thorough them simultenously.

Here is the code i am using
[...]
Is there any efficient doing this
Try the zip function:
>>list1 = [ 'a', 'b', 'c' ]
list2 = [ 'A', 'B', 'C' ]
for i, j in zip( list1, list2 ):
.... print i, j
....
a A
b B
c C
Oct 29 '06 #3

CSUIDL PROGRAMMEr wrote:
folks
I have two lists

i am trying to loop thorough them simultenously.

Here is the code i am using

f1 = os.popen('ls chatlog*.out')
You can replace this by
pyimport glob
pyf1 = glob.glob('chatlog*.out')
it will return a list of filenames in f1, so you can skip next two
lines in your code
data1=f1.readlines()
f1.close()
If you use the glob this step can be skipped too, as glob returns the
filenames, without any whitespace added
data1=[x.strip() for x in data1]
f1 = os.popen('ls chatlog*.txt')
data=f1.readlines()
f1.close()
for eachline in data1 and line in data:

filename='/root/Desktop/project/'+ eachline
print filename
outfile=open(filename,'r')
filename1='/root/Desktop/project/' + line
print filename1
now you do the zip as suggested by the other people, but be aware that
it only returns the tuples as long as the shortest list is, so it might
be a good idea to add a test on equality of the number of .log and .txt
files
pyimport glob
pyf1 = glob.glob('chatlog*.out')
pyf2 = glob.glob('chatlog*.txt')
pyif len(f1) != len(f2):
py print "number of .out files doesn't match number of .txt files"
pyf3 = zip(f1, f2)
now f3 will be somethine like [('chatlog1.out',
'chatlog1.txt'),('chatlog2.out', 'chatlog2.txt')], then you can
continue with your iteration loops

I get the error that line is not defined.
Traceback (most recent call last):
File "list.py", line 16, in ?
for eachline in data1 and line in data:
NameError: name 'line' is not defined

Is there any efficient doing this
Oct 29 '06 #4
On Sunday 29 October 2006 15:28, CSUIDL PROGRAMMEr
wrote:
folks
I have two lists

i am trying to loop thorough them
simultenously.
Try something like this.

for eachline in data1:
print eachline
for line in data::
print line

You might also think about a while loop.

jim-on-linux

http://www.inqvista.com
Here is the code i am using

f1 = os.popen('ls chatlog*.out')
data1=f1.readlines()
f1.close()

data1=[x.strip() for x in data1]
f1 = os.popen('ls chatlog*.txt')
data=f1.readlines()
f1.close()
for eachline in data1 and line in data:

filename='/root/Desktop/project/'+ eachline
print filename
outfile=open(filename,'r')
filename1='/root/Desktop/project/' + line
print filename1

I get the error that line is not defined.
Traceback (most recent call last):
File "list.py", line 16, in ?
for eachline in data1 and line in data:
NameError: name 'line' is not defined

Is there any efficient doing this
Oct 29 '06 #5
"CSUIDL PROGRAMMEr" <sy*********@yahoo.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
folks
I have two lists

i am trying to loop thorough them simultenously.

Here is the code i am using

f1 = os.popen('ls chatlog*.out')
data1=f1.readlines()
f1.close()

data1=[x.strip() for x in data1]
f1 = os.popen('ls chatlog*.txt')
data=f1.readlines()
f1.close()
for eachline in data1 and line in data:
Change:
for eachline in data1 and line in data:

to:
for eachline, line in zip(data1,data):
-- Paul
Oct 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephen | last post by:
I have an array list alSearchCriteria which contains values which vary each time its created. It will always have 15 items in the arraylist each time its created. Some of the values in the array...
2
by: Stephen | last post by:
I have an array list alSearchCriteria which contains values which vary each time its created. It will always have 15 items in the arraylist each time its created. Some of the values in the array...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
2
by: pob | last post by:
Whats the difference between using a control or a listbox when looping thru a listbox. In example 1 it dims a listbox and an example 2 it dims a control. Please explain. Thanks in advance ...
8
by: Tommy Grav | last post by:
I have a list: a = I want to loop over a and then loop over the elements in a that is to the right of the current element of the first loop In C this would be equivalent to:
3
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
0
by: jags_32 | last post by:
We have a pretty simple data flow that fetches data from our Source ERP system and dumps it into a SQL Server table. This functionality works, what we are trying to do now is to extend this...
10
by: Charlie Brown | last post by:
I am using something like the following function to loop through a recipe program I am working on. As I was writing it, I starting thinking this isn't going to work at all... but not I'm not so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.