Hi
what will be the fastest solution to following problem
class a:
def __init__(self):
self.varA = 0
self.varB = 0
s.o. ...
listA = [a]*10
varA = []
varB = []
for l in listA:
varA.append(l.varA)
varB.append(l.varB)
s.o. ...
I could think of:
[varA.append(l.varA) for l in listA]
[varB.append(l.varB) for l in listA]
s.o. ...
But is this faster? After all I have to iterate over listA several times.
Any better solutions?
Oliver 1 2661
Oliver Eichler wrote: what will be the fastest solution to following problem
class a: def __init__(self): self.varA = 0 self.varB = 0 s.o. ...
listA = [a]*10
varA = [] varB = []
for l in listA: varA.append(l.varA) varB.append(l.varB) s.o. ...
I could think of: [varA.append(l.varA) for l in listA] [varB.append(l.varB) for l in listA] s.o. ...
But is this faster? After all I have to iterate over listA several times.
Any better solutions?
A different approach, a "virtual list" could even be faster if client code
must access only a fraction of the items in the virtual list. If you are
lucky, you never have to iterate over the complete list. class A:
.... def __init__(self, a):
.... self.a = a
.... def __repr__(self):
.... return "A(%r)" % self.a
.... items = map(A, range(10)) items
[A(0), A(1), A(2), A(3), A(4), A(5), A(6), A(7), A(8), A(9)] class List:
.... def __init__(self, items, getter):
.... self.items = items
.... self.getter = getter
.... def __getitem__(self, index):
.... return self.getter(self.items[index])
.... def __iter__(self):
.... return imap(self.getter, self.items)
.... from operator import attrgetter from itertools import imap items_a = List(items, attrgetter("a")) items_a
<__main__.List instance at 0x402ae2cc> items_a[2]
2 list(items_a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] items[2] = A(42) items
[A(0), A(1), A(42), A(3), A(4), A(5), A(6), A(7), A(8), A(9)] items_a[2]
42
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: anongroupaccount |
last post by:
class CustomType
{
public:
CustomType(){_i = 0;}
CustomType(int i) : _i(i) {}
private:
int _i;
};
class MyClass
|
by: SKumar |
last post by:
Hi All,
I have a list which contains my class objects. My class is
having two variables. I want to sort the list based on these two
variables.
Ex :
class Foo
{
...
...
|
by: ahart |
last post by:
I'm pretty new to python and am trying to write a fairly small
application to learn more about the language. I'm noticing some
unexpected behavior in using lists in some classes to hold child...
|
by: WXS |
last post by:
Vote for this idea if you like it here:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
|
by: mangalalei |
last post by:
A static data member can be of the same class type as that of which it
is a member. A nonstatic data member is restricted to being declared as
a pointer or a reference to an object of its class.
...
|
by: Bit byte |
last post by:
I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables...
|
by: Valeriu Catina |
last post by:
Hi,
consider the Shape class from the FAQ:
class Shape{
public:
Shape();
virtual ~Shape();
virtual void draw() = 0;
|
by: jerry.teshirogi |
last post by:
I have the following class and main:
//////////////////////////////////////////////////////////
#include <iostream.h>
class myVector
{
public:
double x, y, z:
|
by: Immortal Nephi |
last post by:
My project grows large when I put too many member functions into one
class. The header file and source code file will have approximately
50,000 lines when one class contains thousand member...
|
by: Henri.Chinasque |
last post by:
Hi all,
I am wondering about thread safety and member variables. If I have
such a class:
class foo {
private float m_floater = 0.0;
public void bar(){
m_floater = true;
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: 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,...
|
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,...
| |