i want to initialize a constant list at the beginning of a python
program. but the list is too big for one line. is there any
possibility to spread the list over several lines, so that the code
looks neat again?
something like:
LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
[400, 401, 402], [500, 501, 502], [600, 601, 602],
[700, 701, 702], [800, 801, 802], [900, 901, 902]]
thanks in advance
dominik 20 10449 do******@student.ethz.ch (Dominik Kaspar) wrote: LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
Might I suggest trying the above and seeing what happens?
Dominik Kaspar wrote: i want to initialize a constant list at the beginning of a python program. but the list is too big for one line. is there any possibility to spread the list over several lines, so that the code looks neat again?
something like: LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
thanks in advance dominik
:) yes. Just type the code and feed it to python interpreter:
LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
[400, 401, 402], [500, 501, 502], [600, 601, 602],
[700, 701, 702], [800, 801, 802], [900, 901, 902]]
print LIST
[[100, 101, 102], [200, 201, 202], [300, 301, 302], ...
regards,
anton.
Just some coding ideas :
List = [
[100,101,102]
, [200,201,202]
, [300,301,302]
, .....
]
Can make things clearer and sometimes worse.
Boris
On Fri, 17 Oct 2003 06:56:48 -0700, Dominik Kaspar wrote: i want to initialize a constant list at the beginning of a python program. but the list is too big for one line. is there any possibility to spread the list over several lines, so that the code looks neat again?
something like: LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
thanks in advance dominik
Dominik Kaspar wrote: i want to initialize a constant list at the beginning of a python program. but the list is too big for one line. is there any possibility to spread the list over several lines, so that the code looks neat again?
something like: LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
Did you know that you can run Python directly and get an interactive
interpreter, where you could try out code like the above and
see what it does? This and other helpful things are covered in
the online tutorial at http://www.python.org/doc/current/tut/tut.html
-Peter
> LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
There are several ways to do it.
Anything within [...], (...) or {...} can span lines and needs follow
NO indent rules. So your example works just as it is.
Another way is to end any line with a backslash. Then the following
line is logically appended to the current line, and again its indent
is irrelevant. I rarely use this because I think it's ugly and because
I can put almost anything between parentheses.
Gary Herron
"Boris Boutillier" <bo**************@arteris.net> wrote in message
news:pa****************************@arteris.net... Just some coding ideas : List = [ [100,101,102] , [200,201,202] , [300,301,302] , ..... ] Can make things clearer and sometimes worse.
Boris, you are aware that the last member of a Python list can have a
trailing comma, aren't you? In my opinion, this:
List = [
[100,101,102],
[200,201,202],
[300,301,302],
]
is at least as clear, and does not force the unnatural binding of a leading
comma to what follows.
--
rzed
Dnia 17 Oct 2003 06:56:48 -0700, Dominik Kaspar napisał(a): i want to initialize a constant list at the beginning of a python program. but the list is too big for one line. is there any possibility to spread the list over several lines, so that the code looks neat again?
something like: LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
The above is correct, but use tuples instead.
--
[ Wojtek Walczak - gminick (at) underground.org.pl ]
[ <http://gminick.linuxsecurity.pl/> ]
[ "...rozmaite zwroty, matowe od patyny dawnosci." ]
Wojtek Walczak wrote: Dnia 17 Oct 2003 06:56:48 -0700, Dominik Kaspar napisał(a): i want to initialize a constant list at the beginning of a python program. but the list is too big for one line. is there any possibility to spread the list over several lines, so that the code looks neat again?
something like: LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
The above is correct, but use tuples instead.
No need. Why do you suggest that? I think the advice goes against
typical Python programming style in this case.
-Peter
Dnia Tue, 21 Oct 2003 09:17:42 -0400, Peter Hansen napisał(a): > i want to initialize a constant list at the beginning of a python > program. but the list is too big for one line. is there any > possibility to spread the list over several lines, so that the code > looks neat again? > > something like: > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], > [400, 401, 402], [500, 501, 502], [600, 601, 602], > [700, 701, 702], [800, 801, 802], [900, 901, 902]]
The above is correct, but use tuples instead.
No need. Why do you suggest that? I think the advice goes against typical Python programming style in this case.
Because the OP said: ,,i want to initialize a constant list''.
If he's sure he want a constant list, he should use tuples to accent
that the LIST is constant. Besides, tuples are faster than lists.
--
[ Wojtek Walczak - gminick (at) underground.org.pl ]
[ <http://gminick.linuxsecurity.pl/> ]
[ "...rozmaite zwroty, matowe od patyny dawnosci." ]
Wojtek Walczak wrote: Dnia Tue, 21 Oct 2003 09:17:42 -0400, Peter Hansen napisał(a): > i want to initialize a constant list at the beginning of a python > program. but the list is too big for one line. is there any > possibility to spread the list over several lines, so that the code > looks neat again? > > something like: > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], > [400, 401, 402], [500, 501, 502], [600, 601, 602], > [700, 701, 702], [800, 801, 802], [900, 901, 902]]
The above is correct, but use tuples instead.
No need. Why do you suggest that? I think the advice goes against typical Python programming style in this case.
Because the OP said: ,,i want to initialize a constant list''. If he's sure he want a constant list, he should use tuples to accent that the LIST is constant. Besides, tuples are faster than lists.
I disagree. He should instead, as he did, merely use an ALLCAPS
name for his variable, to identify it as a "constant" list. This is
considered sufficient in Python.
Using a tuple just barely "accents" that fact, anyway, since in all
the cases where the variable is to be used, it is only the name that
one can see, not the fact that it is a tuple.
Tuples are also not significantly faster than lists, as I recall
from past discussions of this. At least, not to the extent that
it should ever be a consideration when initializing "constant" lists.
And, again, I believe your advice is contrary to conventional Python
programming style, though I'm quite sure there are some people who
would agree with your approach as well...
-Peter
> Dnia 17 Oct 2003 06:56:48 -0700, Dominik Kaspar napisa?(a): i want to initialize a constant list at the beginning of a python program. but the list is too big for one line. is there any possibility to spread the list over several lines, so that the code looks neat again?
something like: LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602], [700, 701, 702], [800, 801, 802], [900, 901, 902]]
LIST = [range(x, x + 3) for x in range(100, 1000, 100)]
blindly-optimizing-the-example-you-give'ly,
Jp
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQE/lU02edcO2BJA+4YRAiYcAKCpTaEUC+FvdDpwp2Ajj1gVUV+0rg CeNjSZ
JrkT9LggBjILOr6ZhpnUNqk=
=HoH4
-----END PGP SIGNATURE-----
Peter Hansen <pe***@engcorp.com> writes: Tuples are also not significantly faster than lists, as I recall from past discussions of this. At least, not to the extent that it should ever be a consideration when initializing "constant" lists.
Tuples probably aren't faster, but they do use less memory.
Paul Rubin wrote: Peter Hansen <pe***@engcorp.com> writes: Tuples are also not significantly faster than lists, as I recall from past discussions of this. At least, not to the extent that it should ever be a consideration when initializing "constant" lists.
Tuples probably aren't faster, but they do use less memory.
Again, as I recall the past discussions, not significantly so.
They still have only four bytes (a pointer) per element, and surely for
any tuple or list where one could possibly be concerned about memory
consumption the number of elements far outweighs the overhead associated
with the structure itself (which is probably on the order of a few bytes
anyway).
-Peter
Peter Hansen <pe***@engcorp.com> writes: Tuples probably aren't faster, but they do use less memory.
Again, as I recall the past discussions, not significantly so.
They still have only four bytes (a pointer) per element, and surely for any tuple or list where one could possibly be concerned about memory consumption the number of elements far outweighs the overhead associated with the structure itself (which is probably on the order of a few bytes anyway).
Lists allocate memory for extra elements, so list.append doesn't have
to copy the whole list around every time you call it.
Paul Rubin wrote: Peter Hansen <pe***@engcorp.com> writes: Tuples probably aren't faster, but they do use less memory.
Again, as I recall the past discussions, not significantly so.
They still have only four bytes (a pointer) per element, and surely for any tuple or list where one could possibly be concerned about memory consumption the number of elements far outweighs the overhead associated with the structure itself (which is probably on the order of a few bytes anyway).
Lists allocate memory for extra elements, so list.append doesn't have to copy the whole list around every time you call it.
Ah, I see. How much would that consume in, say, the example as given?
Is that a significant amount, enough to be concerned about?
Is there a published algorithm behind this, or is it just some magic
of the Timbot's that would be found only in the source?
-Peter
"Peter Hansen" Is there a published algorithm behind this, or is it just some magic of the Timbot's that would be found only in the source?
Both ;-)
From cvs listobject.c...
/* Round up:
* If n < 256, to a multiple of 8.
* If n < 2048, to a multiple of 64.
* If n < 16384, to a multiple of 512.
* If n < 131072, to a multiple of 4096.
* If n < 1048576, to a multiple of 32768.
* If n < 8388608, to a multiple of 262144.
* If n < 67108864, to a multiple of 2097152.
* If n < 536870912, to a multiple of 16777216.
* ...
* If n < 2**(5+3*i), to a multiple of 2**(3*i).
*
* This over-allocates proportional to the list size, making room
* for additional growth. The over-allocation is mild, but is
* enough to give linear-time amortized behavior over a long
* sequence of appends() in the presence of a poorly-performing
* system realloc() (which is a reality, e.g., across all flavors
* of Windows, with Win9x behavior being particularly bad -- and
* we've still got address space fragmentation problems on Win9x
* even with this scheme, although it requires much longer lists to
* provoke them than it used to).
Emile van Sebille em***@fenx.com
In article <3F***************@engcorp.com>,
Peter Hansen <pe***@engcorp.com> wrote:
[ re lists allocating extra space to achieve constant amortized time per
append operation ] Is there a published algorithm behind this, or is it just some magic of the Timbot's that would be found only in the source?
The details are in the source, but the same algorithm can be found e.g.
in Cormen et al, "Introduction to Algorithms" (2nd ed.), section 17.4
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Emile van Sebille wrote: "Peter Hansen" Is there a published algorithm behind this, or is it just some magic of the Timbot's that would be found only in the source?
Both ;-)
From cvs listobject.c...
/* Round up: * If n < 256, to a multiple of 8.
Thanks, Emile; Paul was right...
So the above is probably the extent of the damage. In the example
given, that would mean instead of (9*3+9)*4 or 144 bytes from the
element pointers, (9*8+16)*4 or 352 bytes would be consumed instead.
My guess is that this amount is about on par with the overhead
associated with the rest of the objects involved, such as the memory
consumed by the list object itself or by the integer objects.
Given how many such "constant lists" one creates in an average program
and how insignificant these numbers really are compared to almost any
aspect of Python, such as the overhead of the .pyc in memory, I
wouldn't think this aspect should concern most Python programmers...
Now, if the items in question were triplets of numbers of some kind,
such as coordinates, then I'd agree the nine elements should be tuples
themselves, but the containing thing should still be a list, merely
for readability and consistency with conventional style.
Not that any of this amounts to a hill of beans in the greater
scheme of things....
-Peter
Peter Hansen wrote:
... They still have only four bytes (a pointer) per element, and surely for any tuple or list where one could possibly be concerned about memory consumption the number of elements far outweighs the overhead associated with the structure itself (which is probably on the order of a few bytes anyway).
One counterexample from the dark ages:
map(twoargsfunc, lotsoffirstargs, (onesecondarg,)*len(lotsoffirstargs))
the memory consumption of all the constructed tuple's elements is
fixed -- sizeof(onesecondarg) if Python has sizeof (don't you wish...;-),
as all slots in the tuple point to that one object.
So, here, the overhead of the structure itself might be important,
since the elements in that structure aren't; so a tuple MAY be a good
thing.
Today we do [twoargsfunc(x,onesecondarg) for x in lotsoffirstargs]
and we don't worry much about huge tuples made all of the same
element any more;-). But there may still be a few such cases around.
Alex
Alex Martelli wrote: Peter Hansen wrote: ... They still have only four bytes (a pointer) per element, and surely for any tuple or list where one could possibly be concerned about memory consumption the number of elements far outweighs the overhead associated with the structure itself (which is probably on the order of a few bytes anyway).
One counterexample from the dark ages:
map(twoargsfunc, lotsoffirstargs, (onesecondarg,)*len(lotsoffirstargs))
the memory consumption of all the constructed tuple's elements is fixed -- sizeof(onesecondarg) if Python has sizeof (don't you wish...;-), as all slots in the tuple point to that one object.
So, here, the overhead of the structure itself might be important, since the elements in that structure aren't; so a tuple MAY be a good thing.
Certainly there are cases, such as your example and doubtless others.
"Initializing constant lists at the beginning of the program" likely
isn't one of them, except in exceptional cases.
-Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kevin T. Ryan |
last post by:
Hi All -
I'm not sure, but I'm wondering if this is a bug, or maybe (more
likely) I'm misunderstanding something...see below:
>>> f = open('testfile', 'w')
>>> f.write('kevin\n')
>>>...
|
by: George Stout |
last post by:
First off I do not know alot about writing queries to an Access
Database from an ASP page. This is why I need help.
I have an Events database for 6 colleges in our metro area. On the
homepage I...
|
by: James Cameron |
last post by:
Hi I'm developing a program and the client is worried about future
reuse of the code. Say 5, 10, 15 years down the road. This will be a
major factor in selecting the development language. Any...
|
by: Robin Briggs |
last post by:
Hi.
I have an ASP (3.0, not .NET) app that writes cookies with hyphens and
spaces in the names and values. If I had the control over the apps that will
be looking for these cookies, I would...
|
by: Douglas Garstang |
last post by:
All,
I posted a newsgroup question here a few weeks back, asking some
questions that related to my 10 year quest (so far) to understand
pointers.
Someone suggested I write a simple emulator....
|
by: |
last post by:
I have a breakpoint in an aspx page that I'm using to try to trap some code
to see what's going on. I'm translating a page that is working in a
traditional ASP page, which takes several session...
|
by: Peter Olcott |
last post by:
Cab you write code directly in the Common Intermediate language? I need to
optimize a critical real-time function.
|
by: gonzlobo |
last post by:
Greetings, and happyNewYear to all.
I picked up Python a few weeks ago, and have been able to parse large
files and process data pretty easily, but I believe my code isn't too
efficient. I'm...
|
by: pereges |
last post by:
Hi, I'm trying to write a macro for the relative difference function
which is used to check the close enough floating point values. Is
this correct way to write it ? :
#define max(x, y) ((x)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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: 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: 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...
| |