I've done this before:
data = [self.cong.tm[k] for k in self.cong.tm.li]
#li is list, tm is dict
instead of:
for k in self.cong.tm.li:
data.append(self.cong.tm[k])
but when I try:
self.liststore = [[item] for item in data]
instead of:
for item in data:
self.liststore.append([item])
I get an empty list! What gives??
jonathon 6 1332
j_mckitrick wrote: but when I try:
self.liststore = [[item] for item in data]
instead of:
for item in data: self.liststore.append([item])
I get an empty list! What gives??
You're probably doing something else wrong; this fragment works fine: data = [1, 2, 3, 4] [[item] for item in data]
[[1], [2], [3], [4]]
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Ferocious, aren't I?
-- Lt. Vincent Hanna
----- Original Message -----
From: "j_mckitrick" <j_*********@bigfoot.com>
Newsgroups: comp.lang.python
Sent: Thursday, June 17, 2004 9:54 PM
Subject: Why does one work, but not the other? I've done this before:
data = [self.cong.tm[k] for k in self.cong.tm.li] #li is list, tm is dict
instead of:
for k in self.cong.tm.li: data.append(self.cong.tm[k])
but when I try:
self.liststore = [[item] for item in data]
instead of:
for item in data: self.liststore.append([item])
I get an empty list! What gives??
jonathon tm = {'a':1, 'b':2, 'c':3} li = tm.keys() data = [tm[k] for k in li] data
[1, 3, 2] # also you can do data2 = [tm[k] for k in tm] data2
[1, 3, 2]
Check the dictionary, make sure it has entries?
When supplying the dictionary with elements given by the list, make sure the
keys exist.
or you can do: data = [tm.get(k) for k in li]
Will return None if a key does not exist to further debug.
Hope this helps.
Adonis
Thanks for the help, guys. I found the problem. The liststore cannot
be redefined once set up for the TreeView. I can only clear/append to
it.
But I'm still on my mission to replace 'for' with list comprehensions
where possible, according to the article on optimization on the python
site.
That being said, is there a way to write this as a comprehension? I
can't figure out how to do so and get k into the key correctly. I'm
just trying to save a dictionary via anydbm.
for k, v in self.options.items():
db[k] = str(v)
jonathon
j_mckitrick wrote: But I'm still on my mission to replace 'for' with list comprehensions where possible, according to the article on optimization on the python site.
I don't know the article, but I assume it doesn't tell list comprehensions
are always faster/better.
That being said, is there a way to write this as a comprehension? I can't figure out how to do so and get k into the key correctly. I'm just trying to save a dictionary via anydbm.
for k, v in self.options.items(): db[k] = str(v)
Yes, dk = {1:2, 3:4} options = {1:4, 2:6, 3:8} dk.update(dict([(k, str(v)) for (k, v) in options.iteritems()])) dk
{1: '4', 2: '6', 3: '8'}
but why would you trade a muddy comprehension for a clean loop? The for loop
is clearer (and faster, I suppose) here. Remember that list comprehensions
are a means rather than an end.
With 2.4 that may be a different story, as the above will reduce (I think)
to
dk.update((k, str(v)) for (k, v) in options.iteritems())
However, some overhead (generating throwaway tuples) is likely to remain.
Peter
Peter Otten <__*******@web.de> wrote in message news:<ca*************@news.t-online.com>... j_mckitrick wrote:
But I'm still on my mission to replace 'for' with list comprehensions where possible, according to the article on optimization on the python site.
I don't know the article, but I assume it doesn't tell list comprehensions are always faster/better.
from http://www.python.org/doc/essays/list2str.html:
Try to use map(), filter() or reduce() to replace an explicit for
loop, but only if you can use a built-in function: map with a built-in
function beats for loop, but a for loop with in-line code beats map
with a lambda function!
I remember another, but can't find it right now.
"j_mckitrick" <j_*********@bigfoot.com> wrote in message
news:ec**************************@posting.google.c om... I've done this before:
data = [self.cong.tm[k] for k in self.cong.tm.li] #li is list, tm is dict
instead of:
for k in self.cong.tm.li: data.append(self.cong.tm[k])
but when I try:
self.liststore = [[item] for item in data]
instead of:
for item in data: self.liststore.append([item])
I get an empty list! What gives??
For questions like this, about supposedly anomalous behavior, you usually
need to give actual input and output, reduced to the minimum code needed to
show the purported behavior. Otherwise, the easiest guess is that you did
not use the same value of data in the two snippets;-)
Terry J. Reedy This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christian Seberino |
last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python?
Python's design is godly. I'm wondering if Ruby's is godly too.
I've heard it has solid OOP design but then...
|
by: inquirydog |
last post by:
Hi-
One frusterating thing for me with xsl is that I don't know
how to make xslt throw some sort of exception when a value-of path
does not exist. For instance, suppose I have the following...
|
by: ezra epstein |
last post by:
Aother head banger for me.
Below is a complete example of the code
Using Postgres 7.4,
the function "test" gets this: psql:temp3.sql:10: ERROR: syntax error
at or near "%" at character 135...
|
by: Frank Hauptlorenz |
last post by:
Hello Out there!
I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a
NT 4 based Domain -
now it is a Win 2000 Domain. The database server is a domain member. Now...
|
by: benb |
last post by:
I have form that looks a lot like a search bar for the user to search
for records matching specified criteria (e.g. first names containing
"ben"). For robust results, an intermediary form displays...
|
by: David Mathog |
last post by:
If this:
int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array;
}
is converted to this (never mind why for the moment):
|
by: ramyach |
last post by:
Hi friends,
I need to write a parallel code in 'C' on the server that is
running SGI Irix 6.5. This server supports MIPS Pro C compiler. I don't
have any idea of parallel C languages. I looked...
|
by: Anoop |
last post by:
Hi,
I am new to this newsgroup and need help in the following questions.
1. I am workin' on a GUI application. Does C# provides Layout Managers the
way Java does to design GUI? I know that it...
|
by: Cuthbert |
last post by:
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the
'gets' function is dangerous and should not be...
|
by: webEater |
last post by:
I have a problem, it's not browser specific, and I don't get a
solution. I have an (X)HTML document, I show you a part of it:
....
<!--<div class="pad">-->
<div id="eventImages"><img src=""...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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: 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,...
|
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: 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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| | |