473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

iteration doesn't seem to work ??

hello,

can someone tell me why the following iteration doesn't work,
and
how I should replace empty strings in a list with a default value.
>>v
['123', '345', '', '0.3']
>>for items in v:
.... if items=='':
.... items='3'
....
>>>
v
['123', '345', '', '0.3']
>>>
thanks,
Stef Mientki
May 16 '07 #1
3 979
On May 16, 1:41 am, stef <s.mien...@id.umcn.nlwrote:
hello,

can someone tell me why the following iteration doesn't work,
and
how I should replace empty strings in a list with a default value.
>>v
['123', '345', '', '0.3']
>>for items in v:
... if items=='':
... items='3'
...
>>>
>>v
['123', '345', '', '0.3']
>>>

thanks,
Stef Mientki
Inside the loop, 'items' is no longer referencing the list...its a
string.
>>v = ['123', '4', '567', '']
for i in v:
.... print type(i)
....
<type 'str'>...

This works
>>for j,i in enumerate(v):
.... if i=='':
.... v[j] = '3'
....
>>v
['123', '4', '567', '3']
>>>
~Sean

May 16 '07 #2
hello Sean,

thanks very much for the explanation and solution.

cheers,
Stef Mientki

ha**********@gmail.com wrote:
On May 16, 1:41 am, stef <s.mien...@id.umcn.nlwrote:
>hello,

can someone tell me why the following iteration doesn't work,
and
how I should replace empty strings in a list with a default value.
> >>v
['123', '345', '', '0.3']
> >>for items in v:
... if items=='':
... items='3'
...
> >>>
v
['123', '345', '', '0.3']
> >>>

thanks,
Stef Mientki

Inside the loop, 'items' is no longer referencing the list...its a
string.

>>>v = ['123', '4', '567', '']
for i in v:
... print type(i)
...
<type 'str'>...

This works

>>>for j,i in enumerate(v):
... if i=='':
... v[j] = '3'
...
>>>v
['123', '4', '567', '3']
~Sean

May 16 '07 #3
Ant
On May 16, 9:41 am, stef <s.mien...@id.umcn.nlwrote:
hello,

can someone tell me why the following iteration doesn't work,
and
how I should replace empty strings in a list with a default value.
See the other reponse for the why. Here's another how, using list
comprehension.:

1 v = ['123', '345', '', '0.3']
2 v = [x if x else '3' for x in v]
3 v
3 = ['123', '345', '3', '0.3']

Note that this replaces the list, so won't be appropriate for
modifying a list passed in from elsewhere.

--
Ant...

http://antroy.blogspot.com/
May 16 '07 #4

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

Similar topics

39
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor...
8
by: Mattias Campe | last post by:
Hello, On http://student.ugent.be/astrid/bewoners.php I got the problem that I want Javascript to let my browser go to http://student.ugent.be/astrid/bewoners.php?beginAcjaar=2002 when I select...
4
by: dot | last post by:
I'm trying to use the following hack to create an expanding/contracting list of links: <html> <head> <script language="Javascript" type="text/javascript"> function ChangeText(obj,...
2
by: COHENMARVIN | last post by:
Folks, I wrote a simple javascript script, but it doesn't work. First I have a web page that uses frames: ===================== <html> <frameset rows="10%,90%"> <frame src="frame1.htm"...
0
by: Benny Raymond | last post by:
I'm trying to clear out my dataset and start fresh if the user hits "New". However the ID columns of my dataset are not being reset to 1. Can anyone tell me what i'm doing wrong? Here's the code:...
0
by: Web Developer | last post by:
I'm creating an instance of a web service proxy, and setting the PreAuthenticate property to true, but it doesn't seem to have any effect. After creating the proxy instance, I make two web service...
3
by: Joey | last post by:
I am working on an asp.net 1.1 web app in C#. I downloaded some sample code that is supposed to allow for me to persist session state. The code is as follows: private void PersistSessionState()...
7
by: amygdala | last post by:
Hi, I'm trying to let PHP write a 'sitemap.xml' sitemap for Google and other searchengines. It's working, except that the content in the XML file doesn't seem to be UTF8. (Which it should be,...
1
by: Samuel | last post by:
The MaxLenght property doesn't seem to have any effect
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
0
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...
0
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
Oralloy
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,...
0
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...

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.