On Wed, 17 Mar 2010 13:34:51 +0100, Peter Otten wrote:
> Hatem Oraby wrote:
>
>> Hello, I want to swap the content of two dictionaries, the obvious way
>> to do it is:
>> a = {1:"I'am A"}
>> b = {2:"I'm B"}
>> temp = a
>> a = b
>> b = temp
>
> That can be simplified to
>
> a, b = b, a
>
> and
Hatem Oraby, 17.03.2010 12:26:
However, consider the case in which the dictionary we are referencing lives
in another module:
#external.py
#
#a = {1:"I'am A}
import external
temp = external.a
external.a = b
b = temp
Looks like the interface of your module is broken. It shouldn't export the
tw
Hatem Oraby wrote:
> Hello, I want to swap the content of two dictionaries, the obvious way to
> do it is:
> a = {1:"I'am A"}
> b = {2:"I'm B"}
> temp = a
> a = b
> b = temp
That can be simplified to
a, b = b, a
and is almost certainly the right approach.
> tempKeys = a.keys()
> diffKeys = a.k
Peter Otten wrote:
Terry Reedy wrote:
If the names of superclasses is resolved when classes are instantiated,
the patching is easy. If, as I would suspect, the names are resolved
when the classes are created, before the module becomes available to the
importing code, then much more careful a
Wow, thank you all. Lots of ideas and things to try! I wish I knew
which one is going to work best. The module I'm trying to (monkey!)
patch is pxdom, and as it is a bit long (5700 lines of code in one
file!) I'm not quite sure if the simplest patching method will work or
the more complicated o
Terry Reedy wrote:
> Steven D'Aprano wrote:
>> On Sat, 16 May 2009 09:55:39 -0700, Emanuele D'Arrigo wrote:
>>
>>> Hi everybody,
>>>
>>> let's assume I have a module with loads of classes inheriting from one
>>> class, from the same module, i.e.:
>> [...]
>>> Now, let's also assume that myFile.py
Try this:
class Base(object):
pass
class C(Base):
pass
class NewBase(object):
pass
C.__bases__ = (NewBase,)
help(C)
--
http://mail.python.org/mailman/listinfo/python-list
Steven D'Aprano wrote:
On Sat, 16 May 2009 09:55:39 -0700, Emanuele D'Arrigo wrote:
Hi everybody,
let's assume I have a module with loads of classes inheriting from one
class, from the same module, i.e.:
[...]
Now, let's also assume that myFile.py cannot be changed or it's
impractical to do
On Sat, 16 May 2009 09:55:39 -0700, Emanuele D'Arrigo wrote:
> Hi everybody,
>
> let's assume I have a module with loads of classes inheriting from one
> class, from the same module, i.e.:
[...]
> Now, let's also assume that myFile.py cannot be changed or it's
> impractical to do so. Is there a w
"Emanuele D'Arrigo" writes:
> On May 16, 8:17 pm, Arnaud Delobelle wrote:
>> # Insert Wedge into each subclass of modfoo.Base
>> for subclass in modfoo.Base.__subclasses__():
>> if subclass.__module__ != 'modfoo': continue
>> attrs = dict(item for item in subclass.__dict__.items()
>>
On May 16, 8:17 pm, Arnaud Delobelle wrote:
> # Insert Wedge into each subclass of modfoo.Base
> for subclass in modfoo.Base.__subclasses__():
> if subclass.__module__ != 'modfoo': continue
> attrs = dict(item for item in subclass.__dict__.items()
> if item[0][:2] !=
"Emanuele D'Arrigo" writes:
> Hi everybody,
>
> let's assume I have a module with loads of classes inheriting from one
> class, from the same module, i.e.:
>
> ## myFile.py
> class SuperClass(object)
> class SubClass1(SuperClass)
> class SubClass2(SuperClass)
> class SubClass3(SuperClass)
>
> In
On 2009-01-31, Aahz wrote:
>>But could the swapping be done using less extra memory than this? What
>>is the minimum amount of extra memory required to exchange two 32-bit
>>quantities? What would be the pseudocode that achieves this minimum?
>
> This looks like a homework problem to me
It l
In article ,
Eric Kang wrote:
>
>In python, I set:
>
>x=1
>y=3
>
>z = x
>x = y
>y = z
>
>
>This gave me 3 1, which are the values of x and y swapped.
>The following would have given me the same result:
>x, y = y, x
>
>
>
>But could the swapping be done using less extra memory than this? What
>is
> Grant Edwards wrote:
> > On 2009-01-30, MRAB wrote:
> >
> >>> What is the minimum amount of extra memory required to exchange two
> >>> 32-bit quantities? What would be the pseudocode that achieves this
> >>> minimum?
> >> x ^= y
> >> y ^= x
> >> x ^= y
> >>
> >> This is really only of use when
Steven D'Aprano schrieb:
> Ints in Python are *objects*, not 32-bit quantities. An int is 12 bytes
> (96 bits) in size; a long will use as much memory as needed. If your
> application needs to optimize a swap of two ints, then Python is probably
> going to be much too memory-intensive for you.
Grant Edwards wrote:
> On 2009-01-30, MRAB wrote:
>
>>> What is the minimum amount of extra memory required to exchange two
>>> 32-bit quantities? What would be the pseudocode that achieves this
>>> minimum?
>> x ^= y
>> y ^= x
>> x ^= y
>>
>> This is really only of use when working in assembly l
On Thu, 29 Jan 2009 21:23:48 -0800, Kottiyath wrote:
> Is it possible to swap two floats without a variable?
In Python? Sure.
f = 1.23
g = 2.87
f, g = g, f
This idiom is independent of the types of the objects:
x = "hello world"
y = [1, 2.0, None, "xyz", {}]
x, y = y, x
In other languages?
On Jan 30, 3:31 am, Steven D'Aprano
wrote:
> On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote:
> > On Jan 30, 12:29 am, Eric Kang wrote:
> >> In python, I set:
>
> >> x=1
> >> y=3
>
> >> z = x
> >> x = y
> >> y = z
>
> >> This gave me 3 1, which are the values of x and y swapped. The
> >> f
On 2009-01-30, MRAB wrote:
>> What is the minimum amount of extra memory required to exchange two
>> 32-bit quantities? What would be the pseudocode that achieves this
>> minimum?
>
> x ^= y
> y ^= x
> x ^= y
>
> This is really only of use when working in assembly language.
And rarely then. ;)
On Jan 30, 8:31 am, Steven D'Aprano
wrote:
> On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote:
> > On Jan 30, 12:29 am, Eric Kang wrote:
> >> In python, I set:
>
> >> x=1
> >> y=3
>
> >> z = x
> >> x = y
> >> y = z
>
> >> This gave me 3 1, which are the values of x and y swapped. The
> >> f
On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote:
> On Jan 30, 12:29 am, Eric Kang wrote:
>> In python, I set:
>>
>> x=1
>> y=3
>>
>> z = x
>> x = y
>> y = z
>>
>> This gave me 3 1, which are the values of x and y swapped. The
>> following would have given me the same result: x, y = y, x
>>
On Thu, 29 Jan 2009 16:29:11 -0800, Eric Kang wrote:
> In python, I set:
>
> x=1
> y=3
>
> z = x
> x = y
> y = z
>
>
> This gave me 3 1, which are the values of x and y swapped. The following
> would have given me the same result: x, y = y, x
Yes.
> But could the swapping be done using less
On Jan 30, 12:29 am, Eric Kang wrote:
> In python, I set:
>
> x=1
> y=3
>
> z = x
> x = y
> y = z
>
> This gave me 3 1, which are the values of x and y swapped.
> The following would have given me the same result:
> x, y = y, x
>
> But could the swapping be done using less extra memory than this?
Eric Kang wrote:
In python, I set:
x=1
y=3
z = x
x = y
y = z
This gave me 3 1, which are the values of x and y swapped.
The following would have given me the same result:
x, y = y, x
But could the swapping be done using less extra memory than this?
What is the minimum amount of extra memory
En Sat, 18 Aug 2007 02:58:23 -0300, Beema shafreen
<[EMAIL PROTECTED]> escribi�:
> result:
> klp5bub1
>
> apn1apn2
>
> but i have do the same for the revere ,to check the result like this for
> eg:
> apn2apn1
> what is the concept to do this
I don't understand exactly what you want
>;
Sent: Wednesday, August 23, 2006 12:19 PM
Subject: Re: swapping numeric items in a list
> At Wednesday 23/8/2006 15:32, Jiang Nutao wrote:
>
>>This is what I got in the debugger:
>>
>>(Pdb) aa=array('b', [126, 55, 71, 112])
>>(Pdb) aa
>>array(
At Wednesday 23/8/2006 15:32, Jiang Nutao wrote:
This is what I got in the debugger:
(Pdb) aa=array('b', [126, 55, 71, 112])
(Pdb) aa
array('b', [126, 55, 71, 112])
(Pdb) aa.byteswap()
(Pdb) aa
array('b', [126, 55, 71, 112])
Oh, sorry, to swap by two bytes "H" was the right typecode. But your
Jiang Nutao wrote:
> array.byteswap() won't work for me easily. I tried this before my 1st post.
> I defined
>
> aa = array('H', [0x12, 0x34, 0x56, 0x78])
>
> Then did byteswap aa.byteswap(). The result was
>
> array('H', [0x1200, 0x3400, 0x5600, 0x7800])
>
> You can see it byteswappe
]>
To: "Jiang Nutao" <[EMAIL PROTECTED]>
Cc:
Sent: Wednesday, August 23, 2006 11:28 AM
Subject: Re: swapping numeric items in a list
> At Wednesday 23/8/2006 14:44, Jiang Nutao wrote:
>
>>array.byteswap() won't work for me easily. I tried this before my 1st
>&g
At Wednesday 23/8/2006 14:44, Jiang Nutao wrote:
array.byteswap() won't work for me easily. I tried this before my 1st post.
I defined
aa = array('H', [0x12, 0x34, 0x56, 0x78])
Then did byteswap aa.byteswap(). The result was
array('H', [0x1200, 0x3400, 0x5600, 0x7800])
You can see it
Thank you all guys for the help. Guess I'm gonna pick bearophile's way. It's
fast, neat, and easy to read.
array.byteswap() won't work for me easily. I tried this before my 1st post.
I defined
aa = array('H', [0x12, 0x34, 0x56, 0x78])
Then did byteswap aa.byteswap(). The result was
ar
Jiang Nutao wrote:
> Hi,
>
> I simplify my problem like below
>
> To convert list
> aa = [0x12, 0x34, 0x56, 0x78]
> into
> [0x34, 0x12, 0x78, 0x56]
>
> How to do it fast? My real list is huge.
Mark Rintsch's suggestion appears best if applicable, but just to cite yet
other
ways to do
In <[EMAIL PROTECTED]>, Jiang Nutao
wrote:
> To convert list
> aa = [0x12, 0x34, 0x56, 0x78]
> into
> [0x34, 0x12, 0x78, 0x56]
>
> How to do it fast? My real list is huge.
Use the `array` module and the `array.byteswap()` method.
Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.py
Jiang Nutao:
> To convert list
> aa = [0x12, 0x34, 0x56, 0x78]
> into
> [0x34, 0x12, 0x78, 0x56]
> How to do it fast? My real list is huge.
Note that:
>>> a = range(6)
>>> a
[0, 1, 2, 3, 4, 5]
>>> a[::2]
[0, 2, 4]
>>> a[1::2]
[1, 3, 5]
So you can do:
>>> a[::2], a[1::2] = a[1::2], a[::2
Jiang Nutao wrote:
> Hi,
>
> I simplify my problem like below
>
> To convert list
> aa = [0x12, 0x34, 0x56, 0x78]
> into
> [0x34, 0x12, 0x78, 0x56]
>
> How to do it fast? My real list is huge.
>
> Thanks a lot.
> Jason
Here's simple and probably fast enough way (but it won't work right on
for i in xrange(0, len(your_list), 2):
your_list[i], your_list[i + 1] = your_list[i + 1], your_list[i]
Jiang Nutao wrote:
> Hi,
>
> I simplify my problem like below
>
> To convert list
> aa = [0x12, 0x34, 0x56, 0x78]
> into
> [0x34, 0x12, 0x78, 0x56]
>
> How to do it fast? My real li
37 matches
Mail list logo