473,385 Members | 1,730 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,385 software developers and data experts.

skip item in list "for loop"

I am new to python and I love it. I am hacking a file. I want to not
print a line if it contains the word 'pmos4_highv'. I also don't want
to print the next line. The following code works but it "smells bad"
and just doesn't look right. I was reading about generators. If I was
using one, I could do a .next() after the match and get rid of the
flags. Any suggestions how I can improve this?
inPmos = False

inFile = sys.stdin
fileList = inFile.readlines()

for line in fileList:
line = line.rstrip()
# Remove PMOS definitions - typically two lines. Screwed if only
one and last inst.
if inPmos:
inPmos = False
continue
if 'pmos4_highv' in line:
inPmos = True
continue
jr

Apr 14 '06 #1
4 4703
Jacob Rael schrieb:
I am new to python and I love it. I am hacking a file. I want to not
print a line if it contains the word 'pmos4_highv'. I also don't want
to print the next line. The following code works but it "smells bad"
and just doesn't look right. I was reading about generators. If I was
using one, I could do a .next() after the match and get rid of the
flags. Any suggestions how I can improve this?


A generator certainly would be handy:

def read_lines(inFile):
fg = iter(inFile)
for line in fg:
if "pmos4_highv" in line:
fg.next()
else:
yield line

lines = """a
b
c
d
pmos4_highv
no no!
e
f
g""".split("\n")
for line in read_lines(lines):
print line
Diez
Apr 14 '06 #2
Em Sex, 2006-04-14 Ã*s 20:33 +0200, Diez B. Roggisch escreveu:
def read_lines(inFile):
fg = iter(inFile)
for line in fg:
if "pmos4_highv" in line:
fg.next()
else:
yield line


Just be aware that the "fb.next()" line can raise an StopIteration
exception that would otherwise be caught by the for loop. If you have
any resources that need to be cleaned up, try...finally is you friend.

--
Felipe.

Apr 14 '06 #3
Felipe Almeida Lessa schrieb:
Em Sex, 2006-04-14 Ã*s 20:33 +0200, Diez B. Roggisch escreveu:
def read_lines(inFile):
fg = iter(inFile)
for line in fg:
if "pmos4_highv" in line:
fg.next()
else:
yield line


Just be aware that the "fb.next()" line can raise an StopIteration
exception that would otherwise be caught by the for loop. If you have
any resources that need to be cleaned up, try...finally is you friend.


Actually I did that on purpose - as the StopIteration would simply end
the generator. Any clean-up - well, if that was the scope of the
generator on could add it of course, but I'd do that on the same level
the file has been opened.

Regards,

Diez
Apr 14 '06 #4
Thanks for the suggestions.
The solution I liked most was to prevent the lines from appearing in
the first place!!

Apr 15 '06 #5

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

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
17
by: Istvan Albert | last post by:
Paul McGuire wrote: > Please reconsider the "def f() :" construct. Instead of > invoking a special punctuation character, it uses context and placement, > with familiar old 's, to infuse the...
15
by: adomas.paltanavicius | last post by:
Hi there, I am quite new to Python, and have a straight & simple question. In C, there is for (init; cond; advance). We all know that. In Python there are two ways to loop over i=A..B...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
0
by: Dica | last post by:
i'm just trying out c# and VS 2003 for the first time and i'm running into an error: "Cannot create a child list for field tblProjects" the section of code that seems to be throwing this is:...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
8
by: Ratko | last post by:
Say you have something like this: for item in myList: del item Would this actually delete the item from the list or just decrement the reference counter because the item in myList is not...
6
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.