Lists and Sublists

D

dineshv

We have a list of N (>2,000) keywords (of datatype string). Each of
the N keywords has associated with it a list of names of varying
numbers. For example, keyword(1) will have a list of L1 names
associated with it, keyword(2) will have a list of L2 names associated
with it and so on with L1 not equal to L2 etc. All keywords and names
are immutable.

Given a keyword(n) , we want to get hold of the associated list of Ln
names.

At anytime, we also want to add keywords to the list of N keywords,
and to any of the associated Ln lists - both of which will grow to
very large sizes.

The data will be read into the Python data structure(s) from disk
storage.

I am struggling to work out what is the ideal Python data structure
for the above. Any help would be greatly appreciated.

Dinesh
 
D

dwblas

I am struggling to work out what is the ideal Python data structure
for the above. Any help would be greatly appreciated.

The normal way is to use a dictionary of lists. The key would be the
dictionary key, which would contain a list or a list of lists, that is
each name would be an element of the list unless the name is more than
one word, in which case each name would be converted to a list and
appended to the key. Some pseudo-code
if key in name_dic:
name_dic[key] += [name] ## converted to list
If this doesn't work, then SQLite can be used in memory. There are
examples on the website.
 
B

Bruno Desthuilliers

dineshv a écrit :
We have a list of N (>2,000) keywords (of datatype string). Each of
the N keywords has associated with it a list of names of varying
numbers. For example, keyword(1) will have a list of L1 names
associated with it, keyword(2) will have a list of L2 names associated
with it and so on with L1 not equal to L2 etc. All keywords and names
are immutable.

Given a keyword(n) , we want to get hold of the associated list of Ln
names.

At anytime, we also want to add keywords to the list of N keywords,
and to any of the associated Ln lists - both of which will grow to
very large sizes.

The data will be read into the Python data structure(s) from disk
storage.

I am struggling to work out what is the ideal Python data structure
for the above. Any help would be greatly appreciated.

keywords = {}
keywords['python'] = ['fun', 'simple']
keywords['another_p_language'] = ['line noise', 'cryptic']
keywords['python'].append('readable')

HTH
 
A

Amit Khemka

dineshv a écrit :
We have a list of N (>2,000) keywords (of datatype string). Each of
the N keywords has associated with it a list of names of varying
numbers. For example, keyword(1) will have a list of L1 names
associated with it, keyword(2) will have a list of L2 names associated
with it and so on with L1 not equal to L2 etc. All keywords and names
are immutable.

Given a keyword(n) , we want to get hold of the associated list of Ln
names.

At anytime, we also want to add keywords to the list of N keywords,
and to any of the associated Ln lists - both of which will grow to
very large sizes.

The data will be read into the Python data structure(s) from disk
storage.

I am struggling to work out what is the ideal Python data structure
for the above. Any help would be greatly appreciated.

keywords = {}
keywords['python'] = ['fun', 'simple']
keywords['another_p_language'] = ['line noise', 'cryptic']
keywords['python'].append('readable')

To add you may want to have a look at "shelve" module , in case you
want to store this object to a file. It can be useful if the data set
is large and does not change frequently and you would want to save on
some startup time .

cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top