473,503 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

another dictionary q

can this be tidied up ?
verb={}
verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''

,''],['','','','','',''],['','','','','',''],['','','','','','']]

it is to create a dictionary entry with six empty lists of six items as
values.

or is it unnecesary ? They represent six tenses with six persons in each for
an italian verb translator.

ruari
Jul 18 '05 #1
3 1304
Le Sat, 01 Nov 2003 23:03:24 -0800, ruari mactaggart a écrit*:
can this be tidied up ?
verb={}
verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''

,''],['','','','','',''],['','','','','',''],['','','','','','']]


verb[infinitive]=[['']*6]*6
Jul 18 '05 #2
On Sun, 02 Nov 2003 01:16:36 +0100, Benoit Dejean <bn**@ifrance.com> wrote:
Le Sat, 01 Nov 2003 23:03:24 -0800, ruari mactaggart a écrit*:
>verb={}
>verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''

,''],['','','','','',''],['','','','','',''],['','','','','','']]


verb[infinitive]=[['']*6]*6


These are *not* the same thing. The use of "*" on mutable sequences is a nasty
trap for the unwary, and highlights the difference between copy and reference
semantics in Python. The "*" operator does not copy.

In the first example, the inner lists are distinct, whereas in the second
example, the same inner list is present multiple times in the outer list. I'll
use length two for brevity:
a=[['',''],['','']]
b=[['']*2]*2
a [['', ''], ['', '']] b [['', ''], ['', '']]

They *look* the same, but ...
a[0][0]=1
a [[1, ''], ['', '']] b[0][0]=1
b [[1, ''], [1, '']]

While b *looks* the same as a, it contains one list multiple times, so if you
update one list, it affects all the others.

List comprehensions of list comprehensions are a convenient and succinct way of
creating lists of distinct lists:
c=[['' for j in range(2)] for i in range(2)]
c [['', ''], ['', '']] c[0][0]=1
c

[[1, ''], ['', '']]

The indices (i and j) in the list comprehensions are not used in this case:
they are chosen to indicate the meaning of the expression. The innermost
elements of c are accessed by c[i][j] (the dimensions in the ranges are in
reverse order).

--
Ben Caradoc-Davies <be*@wintersun.org>
http://wintersun.org/
Imprisonment on arrival is the authentic Australian immigration experience.
Jul 18 '05 #3
ruari mactaggart wrote:
can this be tidied up ?
verb={}
verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''
,''],['','','','','',''],['','','','','',''],['','','','','','']]

it is to create a dictionary entry with six empty lists of six items as
values.


verb[infinitive] = [ [""]*6 for i in range(6) ]

is more concise and saves you from counting errors.

or is it unnecesary ? They represent six tenses with six persons in each
for an italian verb translator.


Whether it's necessary or not depends on how your application uses those
data, so I'm not sure how I can answer the question (despite being
Italian:-).
Alex

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3575
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
12
2816
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the...
6
13929
by: john_williams_800 | last post by:
Hi; I am writing an html page that will live on one server in an ms windows network, but access pictures from a directory on another ms windows server in the network. I know in html the...
1
9237
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
8
13271
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more...
5
1465
by: blisspikle | last post by:
I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are...
2
1913
by: Carnell, James E | last post by:
I am thinking about purchasing a book, but wanted to make sure I could get through the code that implements what the book is about (Artificial Intelligence a Modern Approach). Anyway, I'm not a...
6
10017
by: star-italia | last post by:
Hi, Is it possible to include a XAML file into another XAML file? For example If i have - Window1.xaml - Window2.xaml and both have a treeview with a custom Style, can i describe the style...
0
1093
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
1
2266
by: sachin2 | last post by:
I am using 3 types of dictionaries. 1) Dictionary<string, string > d = new Dictionary<string, string>(); 2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 3)...
0
7205
marktang
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,...
0
7093
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...
0
7287
Oralloy
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,...
0
7348
jinu1996
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...
1
5021
isladogs
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...
0
4685
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...
0
3175
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
397
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.