473,513 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

this is simple...

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?
Jun 28 '08 #1
3 887
Mel
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.

Jun 28 '08 #2
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

Jun 28 '08 #3
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
Jun 28 '08 #4

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

Similar topics

1
3228
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...
2
6066
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...
3
2084
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...
8
6480
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?
1
1227
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; }
13
5711
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...
4
2087
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...
7
1341
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...
7
2265
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...
18
1486
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...
0
7160
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...
0
7537
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...
1
7099
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...
0
5685
agi2029
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,...
1
5086
isladogs
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...
0
4746
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...
0
3233
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
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 ...

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.