list=('a','d','c','d')
for a in list:
if a=='a' :
#skip the letter affer 'a'
what am I supposed to do? 6 8943
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
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
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'
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']
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |