Hello there. I have a function which as an argument takes a tuple and
either returns that tuple or a mutated version of it. The problem is that
tuples are imutable, hence I have to create a new tuple and copy the
content of the old tuple to a new one.
But how do I do this if I only at runtime know the size of the tuple? I
wish I could pass around lists instead.. that would be so much easier, but
I'm passing "*args" and "**kwargs" around so I'm not really the one
deciding the use of tuples or lists ;)
Am I the first one with a problem like this? I'm not able to find anything
using google on this topic. Hope someone can help me ;)
-Carlo v. Dango
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ 10 5206
"Carlo v. Dango" <oe**@soetu.e u> wrote: Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new tuple and copy the content of the old tuple to a new one.
But how do I do this if I only at runtime know the size of the tuple?
Copy your input tuple to a list, then say "myTuple = tuple (myList)"
myList = []
for item in myInputTuple:
if this is the item that needs changing:
item = something else
myList.append (item)
return tuple (myList)
Carlo v. Dango asks: But how do I do this if I only at runtime know the size of the tuple? I wish I could pass around lists instead.. that would be so much easier, but I'm passing "*args" and "**kwargs" around so I'm not really the one deciding the use of tuples or lists ;)
Convert *args to a list:
args = list(args)
kwargs should be a dict anyway, not a tuple.
Emile van Sebille em***@fenx.com
"Carlo v. Dango" <oe**@soetu.e u> wrote in
news:op******** ******@news.kad net.dom: Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new tuple and copy the content of the old tuple to a new one.
But how do I do this if I only at runtime know the size of the tuple? I wish I could pass around lists instead.. that would be so much easier, but I'm passing "*args" and "**kwargs" around so I'm not really the one deciding the use of tuples or lists ;)
On entry to your function, convert the original tuple to a list, then
mutate the list and convert it back to a tuple when returning.
e.g. def ChangeIt(aTuple ):
work = list(aTuple)
work[0] = "Hello"
return tuple(work)
ChangeIt((1,2,3 ))
('Hello', 2, 3)
--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.co m> wrote:
Many thanks for your quick reply!
aaaahh so there is a tuper() construct! :) that helps a lot.. still this
seems rather inefficient to create a list and then a new tuple.. but I
guess there really is no easier way.
but is there a tuple=>list function ? so that instead of creating the list
by a for-construct I could just say
list = tuple.tolist()
list[0] = newval
return turple(list)
?
-c.v.d. Copy your input tuple to a list, then say "myTuple = tuple (myList)"
myList = [] for item in myInputTuple: if this is the item that needs changing: item = something else myList.append (item)
return tuple (myList)
--
On Tue, Oct 14, 2003 at 03:22:47PM +0200, Carlo v. Dango wrote: Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new tuple and copy the content of the old tuple to a new one.
But how do I do this if I only at runtime know the size of the tuple? I wish I could pass around lists instead.. that would be so much easier, but I'm passing "*args" and "**kwargs" around so I'm not really the one deciding the use of tuples or lists ;)
You could always just convert it to a list, and mutate that, e.g.:
mutable = list(your_tuple )
mutate(mutable)
new_tuple = tuple(mutable)
-Andrew.
"Carlo v. Dango" <oe**@soetu.e u> wrote in message
news:op******** ******@news.kad net.dom... On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.co m> wrote:
Many thanks for your quick reply!
aaaahh so there is a tuper() construct! :) that helps a lot.. still this seems rather inefficient to create a list and then a new tuple.. but I guess there really is no easier way.
but is there a tuple=>list function ? so that instead of creating the list by a for-construct I could just say
list = tuple.tolist() list[0] = newval return turple(list)
No. If you think about it, for such a method to work, it would have
to change the object in place from a tuple to a list, otherwise none
of the bindings would work. If it did it in place, then a tuple
wouldn't be immutable, would it?
If you need to mutate a tuple, then I'd begin to question
whether there isn't something else wrong with the design.
John Roth ?
-c.v.d.
Copy your input tuple to a list, then say "myTuple = tuple (myList)"
myList = [] for item in myInputTuple: if this is the item that needs changing: item = something else myList.append (item)
return tuple (myList) --
On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <ne********@jhr othjr.com>
wrote: "Carlo v. Dango" <oe**@soetu.e u> wrote in message news:op******** ******@news.kad net.dom... On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.co m> wrote:
Many thanks for your quick reply!
aaaahh so there is a tuper() construct! :) that helps a lot.. still this seems rather inefficient to create a list and then a new tuple.. but I guess there really is no easier way.
but is there a tuple=>list function ? so that instead of creating the list by a for-construct I could just say
list = tuple.tolist() list[0] = newval return turple(list) No. If you think about it, for such a method to work, it would have to change the object in place from a tuple to a list,
sorry, the code should have been: mylist = mytuple.tolist( )
If you need to mutate a tuple, then I'd begin to question whether there isn't something else wrong with the design.
I do.. I'd rather pass around a list.. but as I said I'm using the *args
mechanism in python which is a tuple and not a list.
-Carlo van Dango
On Tue, 14 Oct 2003 23:28:24 +1000, Andrew Bennetts
<an************ ***@puzzling.or g> wrote:
many thanks to you all for all the kind replies.. I'm sorry for posting
such a simple question.. but I wasn't able to read about tuple() and
list() ... without those two its a bit difficult to do what I wanted ;) You could always just convert it to a list, and mutate that, e.g.:
mutable = list(your_tuple ) mutate(mutable) new_tuple = tuple(mutable)
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
> but is there a tuple=>list function ? so that instead of creating the list by a for-construct I could just say
list = tuple.tolist() list[0] = newval return turple(list)
foo = (1,2,3)
foo_list = list(foo)
Diez This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Yomanium Yoth Taripoät II |
last post by:
HI,
1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no opérator?
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
how to do it?
thx.
|
by: Will McGugan |
last post by:
Hi,
Why is that a tuple doesnt have the methods 'count' and 'index'? It
seems they could be present on a immutable object.
I realise its easy enough to convert the tuple to a list and do this,
I'm just curious why it is neccesary..
Thanks,
|
by: fedor |
last post by:
Hi all, happy new year,
I was trying to pickle a instance of a subclass of a tuple when I ran
into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should
I rewrite my class so I can pickle it?
Thanks ,
Fedor
|
by: Steve M |
last post by:
Hello,
I'm trying to figure out the index position of a tuple member.
I know the member name, but I need to know the members index position. I
know that if I use the statement print tuple that it will print the
contents of that location. What I don't understand is if I know that foo is
a member of tuple, how do I get foo's index position.
Thanks-in-Advance
Steve
|
by: Gregor Horvath |
last post by:
Hi,
>>>type()
<type 'list'>
>>>type(('1'))
<type 'str'>
I wonder why ('1') is no tuple????
| |
by: Nick Maclaren |
last post by:
Why doesn't the tuple type have an index method? It seems such a
bizarre restriction that there must be some reason for it. Yes,
I know it's a fairly rare requirement.
Regards,
Nick Maclaren.
|
by: Alan Isaac |
last post by:
I am probably confused about immutable types.
But for now my questions boil down to these two:
- what does ``tuple.__init__`` do?
- what is the signature of ``tuple.__init__``?
These questions are stimulated by
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439
Looking at that, what fails if I leave out the following line? ::
|
by: Alex Vinokur |
last post by:
Here is some tuple (triple in this case) with uniform types (for
instance, double):
boost::tuple<double, double, doublet;
Is there any way (in boost::tuple) to define such tuples something
like
uniform_tuple <3, doublet; ?
Alex Vinokur
|
by: Hatem Nassrat |
last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote:
Well I have looked into this and it seems that using the list
comprehension is faster, which is reasonable since generators require
iteration and stop iteration and what not.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |