I am a newbie... and the first to admit it... but this has me stuffed:
I have two lists A and B that are both defined as range(1,27) I want
to find the entries that are valid for A = BxB
so here is my code:
A = range(1,27)
B = range(1,27)
for b in B:
if b*b in A:
print b
else:
B.remove(b)
I get, as expected 1,4,9,16,25 printed out being the only members of B
where the condition is true, but when I print B I get:
[1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
1 to 5 is correct, but why doesn't the remove method remove 7 and
above? What am I doing wrong here? 3 887
ToshiBoy wrote:
I have two lists A and B that are both defined as range(1,27) I want
to find the entries that are valid for A = BxB
[ ... ]
I get, as expected 1,4,9,16,25 printed out being the only members of B
where the condition is true, but when I print B I get:
[1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
1 to 5 is correct, but why doesn't the remove method remove 7 and
above? What am I doing wrong here?
Try this:
A = range(1,27)
B = range(1,27)
C = []
for b in B:
print "Trying", b
if b*b in A:
print b
C.append (b)
else:
print "Removing", b
B.remove(b)
print 'B', B
print 'C', C
The essential problem is that your `B.remove`s are pulling the rug out from
under your `for b in B:`. There are ways to mess with B while you iterate.
Running though B backwards will do: `for b in B[::-1]:`, or iterating over
a copy of B: `for b in B[:]:` or `for b in list(B):`. Leaving B alone and
building up the desired items in C is probably simplest.
Mel.
Le Saturday 28 June 2008 06:30:25 ToshiBoy, vous avez écrit*:
I am a newbie... and the first to admit it... but this has me stuffed:
I have two lists A and B that are both defined as range(1,27) I want
to find the entries that are valid for A = BxB
so here is my code:
A = range(1,27)
B = range(1,27)
for b in B:
if b*b in A:
print b
else:
B.remove(b)
I get, as expected 1,4,9,16,25 printed out being the only members of B
where the condition is true, but when I print B I get:
[1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
1 to 5 is correct, but why doesn't the remove method remove 7 and
above? What am I doing wrong here?
-- http://mail.python.org/mailman/listinfo/python-list
This is a frequent error from beginner, you're iterating over a list while
modifying it, the result is undefined.
The most immediate solution is to use a copy of the list you iterate over :
>>[169]: A = range(1,27)
>>>[170]: B = range(1,27)
>>>[171]: for b in B[:] :
if b*b in A:
print b
else:
B.remove(b)
.....:
.....:
1
2
3
4
5
>>>[176]: B
...[176]: [1, 2, 3, 4, 5]
Note that this can be easily done with the simpler :
B = [ e for e in B if e*e not in A ]
--
_____________
Maric Michaud
On Jun 28, 2:48*pm, Mel <mwil...@the-wire.comwrote:
ToshiBoy wrote:
I have two lists A and B that are both defined as range(1,27) I want
to find the entries that are valid for A = BxB
[ ... ]
I get, as expected 1,4,9,16,25 printed out being the only members of B
where the condition is true, but when I print B I get:
[1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
1 to 5 is correct, but why doesn't the remove method remove 7 and
above? What am I doing wrong here?
Try this:
A = range(1,27)
B = range(1,27)
C = []
for b in B:
* * print "Trying", b
* * if b*b in A:
* * * * print b
* * * * C.append (b)
* * else:
* * * * print "Removing", b
* * * * B.remove(b)
print 'B', B
print 'C', C
The essential problem is that your `B.remove`s are pulling the rug out from
under your `for b in B:`. *There are ways to mess with B while you iterate.
Running though B backwards will do: `for b in B[::-1]:`, or iterating over
a copy of B: `for b in B[:]:` or `for b in list(B):`. *Leaving B alone and
building up the desired items in C is probably simplest.
* * * * Mel.
Thank you, of course! :-) Didn't even think of that... that I was
modifying my iterators...
Thank you This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Preston Crawford |
last post by:
I'm looking to quickly get a photo album online. Very simple, thumbnails,
a few pages, maybe a description, but hopefully a small script that's easy
to edit and work into my existing site. I know...
|
by: delisonews |
last post by:
I'm looking for a simple, filesystem-based message board.
(No MySQL!) Something that I could include easily in my code:
include '../inc/messageboard.php';
.... so that the board shows up at...
|
by: Jeff Uchtman |
last post by:
Has anybody found a simple, no zone, banner ad management app? I just want
click through and a start stop date function. I have see everything from
soup to nuts on ad management form $10.00 to...
|
by: Dan |
last post by:
Using XML::Simple in perl is
extreemly slow to parse big
XML files (can be up to 250M,
taking ~1h).
How can I increase my performance /
reduce my memory usage?
Is SAX the way forward?
|
by: Pierre Couderc |
last post by:
I want to serialise quickly a "simple" vector :
(simple is to say with basic types and no pointers)
such as :
class c
{
int i,j;
double z;
}
| |
by: Michael B Allen |
last post by:
Hi,
I've tried to write the *simplest* memory allocator possible. I think it
would be useful in many cases such as allocating memory on stack as a
poor man's garbage collection perhaps.
I was...
|
by: Steven Blair |
last post by:
I have the following number:
64521234567890
and need to apply some sort of simple encryption.
Does c# have any classes for doing this. I cant use 3DES or anything as
complex as.
The size...
|
by: Mark Prenter |
last post by:
Hi all, I'm fairly new to .NET and I haven't done much in C++ before,
nothing complex anyway, but I have a pretty good understanding of
programming in general.
What I'm trying to do is create a...
|
by: abcd |
last post by:
I am trying to set up client machine and investigatging which .net
components are missing to run aspx page.
I have a simple aspx page which just has "hello world" printed....
When I request...
|
by: Sender |
last post by:
Yesterday there was a very long thread on this query. (You can search on
this by post by 'sender' with subject 'Simple Problem' post date Oct 7 time
1:43p)
And in the end the following code was...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |