472,978 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

skip next item in list

list=('a','d','c','d')
for a in list:
if a=='a' :
#skip the letter affer 'a'

what am I supposed to do?

Jun 11 '07 #1
6 8910
2007/6/11, ahlongxp <ah******@gmail.com>:
list=('a','d','c','d')
for a in list:
if a=='a' :
#skip the letter affer 'a'

what am I supposed to do?
There might be better ways to do it, but I would do:

flag_last_a = False
for a in list:
if flag_last_a:
flag_last_a = False
continue
if a=='a':
flag_last_a = True
# Whatever is done when you don't skip

--
Andre Engels, an*********@gmail.com
ICQ: 6260644 -- Skype: a_engels
Jun 11 '07 #2
ahlongxp wrote:
list=('a','d','c','d')
for a in list:
if a=='a' :
#skip the letter affer 'a'

what am I supposed to do?
First - don't use list as name, as it is a builtins-name and shadowing is
likely to produce errors at some point.

list_iterator = iter(('a','d','c','d'))

for a in list_iterator:
if a == 'a':
list_iterator.next()
...

should do the trick.

Diez
Jun 11 '07 #3
On Jun 11, 8:49 am, ahlongxp <ahlon...@gmail.comwrote:
list=('a','d','c','d')
for a in list:
if a=='a' :
#skip the letter affer 'a'

what am I supposed to do?

You could do this with itertools.ifilter and an predicate (pred) for a
more OO solution. I've created 2 lists, the source list (l) and the
expected answer (ans). Make sure this is what you meant in your
problem statement:

import itertools

l = ['a', 'b', 'c', 'a', 'd', 'e', 'f', 'a', 'g', 'h']
ans = ['a', 'c', 'a', 'e', 'f', 'a', 'h']

class pred(object):
def __init__(self):
self.last = None

def __call__(self, arg):
result = None

if self.last == 'a':
result = False
else:
result = True

self.last = arg

return result

i = itertools.ifilter(pred(), l)

result = list(i)

print result
print ans

assert result == ans

print 'done'

Jun 11 '07 #4
On 6/11/07, Andre Engels <an*********@gmail.comwrote:
2007/6/11, ahlongxp <ah******@gmail.com>:
list=('a','d','c','d')
for a in list:
if a=='a' :
#skip the letter affer 'a'

what am I supposed to do?

There might be better ways to do it, but I would do:

flag_last_a = False
for a in list:
if flag_last_a:
flag_last_a = False
continue
if a=='a':
flag_last_a = True
# Whatever is done when you don't skip

--
Andre Engels, an*********@gmail.com
ICQ: 6260644 -- Skype: a_engels
--
http://mail.python.org/mailman/listinfo/python-list
another way:

def skip_after(l):
i = iter(l)
for x in i:
yield x
while x == 'a':
x = i.next()

depending on what to do in case of consecutive 'a's, change the
'while' for an 'if'
list(skip_after('spam aand eggs'))
['s', 'p', 'a', ' ', 'a', 'd', ' ', 'e', 'g', 'g', 's']
Jun 11 '07 #5
ahlongxp wrote:
list=('a','d','c','d')
for a in list:
if a=='a' :
#skip the letter affer 'a'

what am I supposed to do?
Maybe,
>>it = iter(['a','d','c','d'])
for item in it:
print item
if item == 'a':
x = it.next()
a
c
d
>>>
The binding of a name to it.next() is unnecessary. It just prevents 'd'
being printed.

Duncan
Jun 11 '07 #6
Thanks to all of you.
I think the next() trick is the one I'm looking for.

--
ahlongxp

Software College,Northeastern University,China
ah******@gmail.com
http://www.herofit.cn

Jun 12 '07 #7

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

Similar topics

3
by: rasdj | last post by:
Input is this: SET1_S_W CHAR(1) NOT NULL, SET2_S_W CHAR(1) NOT NULL, SET3_S_W CHAR(1) NOT NULL, SET4_S_W CHAR(1) NOT NULL, ; ..py says:
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
2
by: magix | last post by:
I have an drop down list let say: <SELECT name="id" id="id" OnClick="Skip()"> <option value="0"></option> <option value="1">Item 1</option> <option value="2">Item 2</option> <option...
3
by: raghudr | last post by:
Hi all, I am parsing a .xml file.My main intention is to retrieve the name value of node "Signal":- "Name Value" which is "rag". i want to store only the <signal"name value" that is only...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.