Re: Remove empty strings from list

2009-09-14 Thread Helvin Lui
Thanks Chris! Thanks for the quick reply. Indeed this is the case! I have
now written out a new list, instead of modifying the list I am iterating
over.
Logged at my blog:
http://learnwithhelvin.blogspot.com/2009/09/python-loop-and-modify-list.html

Regards,
Helvin  =)

On Tue, Sep 15, 2009 at 1:55 PM, Chris Rebert c...@rebertia.com wrote:

 On Mon, Sep 14, 2009 at 6:49 PM, Helvin helvin...@gmail.com wrote:
  Hi,
 
  Sorry I did not want to bother the group, but I really do not
  understand this seeming trivial problem.
  I am reading from a textfile, where each line has 2 values, with
  spaces before and between the values.
  I would like to read in these values, but of course, I don't want the
  whitespaces between them.
  I have looked at documentation, and how strings and lists work, but I
  cannot understand the behaviour of the following:
 line = f.readline()
 line = line.lstrip() # take away whitespace at the
 beginning of the
  readline.
 list = line.split(' ') # split the str line into a
 list
 
 # the list has empty strings in it, so now,
  remove these empty strings
 for item in list:
 if item is ' ':
 print 'discard these: ',item
 index = list.index(item)
 del list[index] # remove
 this item from the list
 else:
 print 'keep this: ',item
  The problem is, when my list is :  ['44', '', '', '', '', '',
  '0.0\n']
  The output is:
 len of list:  7
 keep this:  44
 discard these:
 discard these:
 discard these:
  So finally the list is:   ['44', '', '', '0.0\n']
  The code above removes all the empty strings in the middle, all except
  two. My code seems to miss two of the empty strings.
 
  Would you know why this is occuring?

 Block quoting from http://effbot.org/zone/python-list.htm
 
 Note that the for-in statement maintains an internal index, which is
 incremented for each loop iteration. This means that if you modify the
 list you’re looping over, the indexes will get out of sync, and you
 may end up skipping over items, or process the same item multiple
 times.
 

 Thus why your code is skipping over some elements and not removing them.
 Moral: Don't modify a list while iterating over it. Use the loop to
 create a separate, new list from the old one instead.

 Cheers,
 Chris
 --
 http://blog.rebertia.com




-- 
Helvin

Though the world may promise me more, I'm just made to be filled with the
Lord.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global variable not working inside function. Increment

2009-09-04 Thread Helvin Lui
Wow!!! Thanks a million!! It worked!   = DThanks for the fast reply too!

Helvin

On Sat, Sep 5, 2009 at 11:52 AM, Rami Chowdhury rami.chowdh...@gmail.comwrote:

global no_picked
no_picked = 0

def picked(object, event):
  no_picked += 1
  print no_picked


 In order to be able to affect variables in the global scope, you need to
 declare them global inside the function, and not at the global scope. So
 your code should read:

no_picked = 0

def picked(object, event):
global no_picked
no_picked += 1
print no_picked

 I believe that will work.


 On Fri, 04 Sep 2009 16:43:27 -0700, Helvin helvin...@gmail.com wrote:

  Hi,

 This increment thing is driving me nearly to the nuts-stage.  

 I have a function that allows me to pick points. I want to count the
 number of times I have picked points.

global no_picked
no_picked = 0

def picked(object, event):
  no_picked += 1
  print no_picked

 Error msg says: UnboundLocalError: local variable 'no_picked'
 referenced before assignment
 For some reason, no_picked does not increment, but the printing
 statement works.

 Do you know why?

 (I'm actually writing this for a vtkrenderwindowinteractor.)

 Helvin




 --
 Rami Chowdhury
 Never attribute to malice that which can be attributed to stupidity --
 Hanlon's Razor
 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)




-- 
Helvin

Though the world may promise me more, I'm just made to be filled with the
Lord.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string find mystery

2009-09-02 Thread Helvin Lui
Thanks!  I just realised that too, but I used the condition:.find()  0 But
I think your's is better.
Simple programming knowledge...   
I made a blog post:
http://learnwithhelvin.blogspot.com/2009/09/1-is-true-if-loops.html

http://learnwithhelvin.blogspot.com/2009/09/1-is-true-if-loops.html

On Thu, Sep 3, 2009 at 5:19 PM, Stephen Hansen apt.shan...@gmail.comwrote:

 The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
 \Data_Input_Material.txt',
 the first if statement if fulfilled, that seemingly says that in this
 file_str, python actually finds the word 'Geometry'.
 I know this, because the line: 'I found geometry' is printed. However,
 if instead of using file_str.find(), I use file_str.endswith(), it
 does not exhibit this strange behaviour.


 The problem is str.find returns -1 on failure; and -1 is a true value. Only
 0, empty string, empty sequences, etc, are false values.

 So, you have to test, 'if find_str.find(pattern) != -1:'

 HTH,

 --S




-- 
Helvin

Though the world may promise me more, I'm just made to be filled with the
Lord.
-- 
http://mail.python.org/mailman/listinfo/python-list