473,387 Members | 1,621 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,387 software developers and data experts.

change an element of a list

Hi!

I have a list of lists and in some of these lists are elements which I
want to change.
Here an example:
lists=[('abc', 4102, 3572), ('def', 2707, 'None'), ('ghi', 'None',
4102)]

'None' should be replaced by 0 or NULL or something else. But as far as
I know the replace function of the module string does not work for
lists.

Any ideas? Thanks for some help!

Dirk

Sep 15 '05 #1
3 5894
Dirk Hagemann wrote:
Hi!

I have a list of lists and in some of these lists are elements which I
want to change.
Here an example:
lists=[('abc', 4102, 3572), ('def', 2707, 'None'), ('ghi', 'None', 4102)]

'None' should be replaced by 0 or NULL or something else.
Your list is a list of tuples, and what you want here is to replace an
element of a tuple - which is not directly possible since tuples are
immutables (but of course there's a way... !-)
But as far as
I know the replace function of the module string does not work for
lists.


Nope, but you can still replace or modify an element of a list.

here a (very Q&D and probably naive and suboptimal) possible solution:

def my_replace(alist, target, replacement):
"""Takes a list of tuples and for each tuple
'replace' target with 'replacement

"""
for i, t in enumerate(alist):
l = list(t)
while target in l:
l[l.index(target)] = replacement
alist[i] = tuple(l)

my_replace(lists, 'None', 'NULL')

HTH
--
bruno desthuilliers
ruby -e "print 'o****@xiludom.gro'.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 15 '05 #2
THANKS! That works :-)

But meanwhile I found another solution that works in my case. Out of
this list of tuples I generated a SQL-Statement which is a simple
string. Then I simply checked this string for 'None'. May be too
easy...

Dirk

Sep 15 '05 #3
Dirk Hagemann wrote:
THANKS! That works :-)
Of course it works. Why wouldn'it it work ?-)
But meanwhile I found another solution that works in my case. Out of
this list of tuples I generated a SQL-Statement which is a simple
string. Then I simply checked this string for 'None'. May be too
easy...


Nope, why ? If you need to turn this list into a string anyway, doing a
replace on the string is of course the simplest and most obvious solution.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 15 '05 #4

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

Similar topics

2
by: Paul | last post by:
Hi, How do I on the change event of a list box open a new url in the current window in which the list box resides. I'm not using frames. Thanks, Paul
2
by: futurepy | last post by:
By default color of the list marker, disc or circle or square, is black. Is there a way to change the color, for example, to red or blue? Thanks.
2
by: Isz | last post by:
Hi Group: I would like to know if it is possible to change the name of an attribute to something else. My setup is like this: I have serveral SQL tables that I nest and join so that it all...
2
by: Daniel Lidström | last post by:
Hi, I would like to know the cleanest way to change the serialization of my Line class from: <Line staStart="2327.02" length="10.00000003390744"> <End>549016.570965 57945.741122</End>...
5
by: zirkon | last post by:
I have a text file, which contains a list of element and their weight, and i need to split the list up and sort it by the weight of the elements. So i need to change the weight to a float. The...
6
by: Jakob Bieling | last post by:
Hi, I want to move an element from a std::list to the end of the same list. To get this done, I thought I'd just do something like: std::list <intlst; lst.push_back (0); lst.push_back (1);...
3
by: Just Another Victim of the Ambient Morality | last post by:
How do you change certain elements in a list? I'm looking to do the Python equivalent of this Ruby code: - first = = - second = first = - first.map! {|i| i + 1} =
3
by: Alan J. Salmoni | last post by:
Hello everyone, I searched through groups to find an appropriate answer to this one but could only find these which didn't meet my program's needs:...
7
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.