473,657 Members | 2,481 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 1315
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.c om> 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
3583
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 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary will contain instances of Item that are keyed off of the Item name. In __main__ I create two...
12
2831
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 Python tutorial and docs leave a touch murky to my understanding. Dictionaries/"dict" types... Am I understanding/interpreting correctly when I go with the idea that a "dict" variable can be looked at as (effectively) two parallel arrays?...
6
14007
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 values for img src use unix style path separators with root ( "/" being your parent web directory. However in windows land the path to my images would be something like
1
9248
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 DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
8
13316
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 dictionaries which themselves may contain ints, None and more dictionaries. Each of the sub-dictionaries is also arbitrarily large. When pretty printing A, in the context I'm using A for, it's rather helpful to remove all key:value pairs where value...
5
1469
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 published under them (I used the arraylist class in the publisher class). How should the code be organized, and how can the books properties like "Name" be easily called in the main code, or searched for in the main code?
2
1926
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 very good programmer and OOP is still sinking in, so please don't answer my questions like I really know anything. MY QUESTION: What is a slot? In class Object below the __init__ has a slot. Note: The slot makes use of a data object called...
6
10084
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 in a trvStyle.xaml and then include it in both windows? Thanks in advance
0
1102
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
2273
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) Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>(); Now I am using GetDictionaryType Function where I an sending a dictionary object. In GetDictionaryType() I want to find out which dictionary...
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4152
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.