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 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
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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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,...
|
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"...
|
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:...
|
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...
|
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()...
|
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,...
|
by: Samuel |
last post by:
The MaxLenght property doesn't seem to have any effect
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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
|
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: 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: 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: 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,...
|
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...
| |