473,791 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of dict or lists or ....?

Pat

I can't figure out how to set up a Python data structure to read in data
that looks something like this (albeit somewhat simplified and contrived):
States
Counties
Schools
Classes
Max Allowed Students
Current enrolled Students

Nebraska, Wabash, Newville, Math, 20, 0
Nebraska, Wabash, Newville, Gym, 400, 0
Nebraska, Tingo, Newfille, Gym, 400, 0
Ohio, Dinger, OldSchool, English, 10, 0

With each line I read in, I would create a hash entry and increment the
number of enrolled students.

I wrote a routine in Perl using arrays of hash tables (but the syntax
was a bear) that allowed me to read in the data and with those arrays of
hash tables to arrays of hash tables almost everything was dynamically
assigned.

I was able to fill in the hash tables and determine if any school class
(e.g. Gym) had exceeded the number of max students or if no students had
enrolled.

No, this is not a classroom project. I really need this for my job.
I'm converting my Perl program to Python and this portion has me stumped.

The reason why I'm converting a perfectly working program is because no
one else knows Perl or Python either (but I believe that someone new
would learn Python quicker than Perl) and the Perl program has become
huge and is continuously growing.
Oct 6 '08
13 1506
On Oct 7, 10:15 pm, Pat <P...@junk.netw rote:
Dennis Lee Bieber wrote:
On Mon, 06 Oct 2008 19:45:07 -0400, Pat <P...@junk.comd eclaimed the
following in comp.lang.pytho n:
I can't figure out how to set up a Python data structure to read in data
that looks something like this (albeit somewhat simplified and contrived):
States
Counties
Schools
Classes
Max Allowed Students
Current enrolled Students
Nebraska, Wabash, Newville, Math, 20, 0
Nebraska, Wabash, Newville, Gym, 400, 0
Nebraska, Tingo, Newfille, Gym, 400, 0
Ohio, Dinger, OldSchool, English, 10, 0
<snip>
The structure looks more suited to a database -- maybe SQLite since
the interface is supplied with the newer versions of Python (and
available for older versions).
Seconded.
I don't understand why I need a database when it should just be
a matter of defining the data structure.
Picking an appropriate data structure depends on the kind of
functionality you want to provide. So far you basically described just
one requirement: keep a tally of how many students are in each class
and compare it to the max allowed (and zero). If that's the only kind
of query you want to run against your data, there's no reason to index
separately each state, county, or school; all you care about are
classes. A simple data structure that satisfies perfectly the
requirement could then be:

# mapping of {class-info : (max,enrolled)}

data = {
('Nebraska', 'Wabash', 'Newville', 'Math') : (20, 0),
('Nebraska', 'Wabash', 'Newville', 'Gym') : (400, 0),
('Nebraska', 'Tingo', 'Newville', 'Gym') : (400, 0),
('Ohio', 'Dinger', 'OldSchool', 'English') : (10, 0),
}

Of course this data structure is pretty bad at answering a query like
"how many classes are there in Nebraska" or "what's the average number
of enrolled students in Newville". The more general information you
might want to get from the data, the more obvious it becomes that you
need a real database.

HTH,
George
Oct 7 '08 #11
George Sakkis <ge***********@ gmail.comwrites :
On Oct 7, 10:15 pm, Pat <P...@junk.netw rote:
I don't understand why I need a database when it should just be a
matter of defining the data structure.

Picking an appropriate data structure depends on the kind of
functionality you want to provide.
[…]
The more general information you might want to get from the data,
the more obvious it becomes that you need a real database.
Thanks very much for posting this answer; I tried to do something
similar but couldn't get at the essential points the way you did here.

Perhaps the original poster is confusing “you should use a database”
with “you should use a database stored in a fully-concurrent
dedicated database management system”.

Far from it: with Python 2.5 you have SQLite (in the ‘sqlite3’
module), which would be ideal for implementing a powerful relational
SQL database used directly by one program instance, without needing a
full-blown database management system in a separately-administrated
server application.

--
\ “Patience, n. A minor form of despair, disguised as a virtue.” |
`\ —Ambrose Bierce, _The Devil's Dictionary_, 1906 |
_o__) |
Ben Finney
Oct 8 '08 #12
En Tue, 07 Oct 2008 23:15:54 -0300, Pat <Pa*@junk.netes cribi:
Dennis Lee Bieber wrote:
>On Mon, 06 Oct 2008 19:45:07 -0400, Pat <Pa*@junk.comde claimed the
following in comp.lang.pytho n:
>>I can't figure out how to set up a Python data structure to read in
data that looks something like this (albeit somewhat simplified and
contrived):
States
Counties
Schools
Classes
Max Allowed Students
Current enrolled Students

Nebraska, Wabash, Newville, Math, 20, 0
Nebraska, Wabash, Newville, Gym, 400, 0
Nebraska, Tingo, Newfille, Gym, 400, 0
Ohio, Dinger, OldSchool, English, 10, 0
<snip>
>The structure looks more suited to a database -- maybe SQLite since
the interface is supplied with the newer versions of Python (and
available for older versions).

I don't understand why I need a database when it should just be a matter
of defining the data structure. I used a fictional example to make it
easier to (hopefully) convey how the data is laid out.
You don't need a full-blown-multiuser-concurrent-petabyte-capable-server
database, just one that does the job. SQLite is very small and comes with
Python 2.5
The Perl routine works fine and I'd like to emulate that behavior but
since I've just starting learning Python I don't know the syntax for
designing the data structure. I would really appreciate it if someone
could point me in the right direction.
So none of the previously posted alternatives worked for you?

--
Gabriel Genellina

Oct 8 '08 #13
Pat wrote:
I can't figure out how to set up a Python data structure to read in data
that looks something like this (albeit somewhat simplified and contrived):

States
Counties
Schools
Classes
Max Allowed Students
Current enrolled Students

Nebraska, Wabash, Newville, Math, 20, 0
Nebraska, Wabash, Newville, Gym, 400, 0
Nebraska, Tingo, Newfille, Gym, 400, 0
Ohio, Dinger, OldSchool, English, 10, 0

With each line I read in, I would create a hash entry and increment the
number of enrolled students.

You might want something like this:
>>import collections, functools
int_dict = functools.parti al(collections. defaultdict, int)
curr = functools.parti al(collections. defaultdict, int)
# builds a dict-maker where t = curr(); t['name'] += 1 "works"
for depth in range(4):
# add a layer with a default of the preceding "type"
curr = functools.parti al(collections. defaultdict, curr)
>>base = curr() # actually make one
base['Nebraska']['Wabash']['Newville']['Math']['max'] = 20
base['Nebraska']['Wabash']['Newville']['Math']['curr'] += 1
base['Nebraska']['Wabash']['Newville']['Math']['curr']
1
>>base['Nebraska']['Wabash']['Newville']['English']['curr']
0
--Scott David Daniels
Sc***********@A cm.Org
Oct 9 '08 #14

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

Similar topics

1
1878
by: Alexander Kervero | last post by:
Hi ,today i was reading diveinto python book,in chapter 5 it has a very generic module to get file information,html,mp3s ,etc. The code of the example is here : http://diveintopython.org/object_oriented_framework/index.html One thing that i have some doubs is this part : class FileInfo(UserDict): "store file metadata"
8
2153
by: bearophileHUGS | last post by:
I'm frequently using Py2.4 sets, I find them quite useful, and I like them, even if they seem a little slower than dicts. Sets also need the same memory of dicts (can they be made to use less memory, not storing values? Maybe this requires too much code rewriting). I presume such sets are like this because they are kind of dicts. If this is true, then converting a dict to a set (that means converting just the keys; this is often useful for...
7
3101
by: Marcio Rosa da Silva | last post by:
Hi! In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are stored using its own rules. Ex: >>> d = {3: 4, 1: 2} >>> d {1: 2, 3: 4}
11
2080
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more Pythonic) way of doing this. It's important that I can read the contents of the dict without flagging it as modified, but I want it to set the flag the moment I add a new element or alter an existing one (the values in the dict are mutable), this...
9
24682
by: py | last post by:
I have two lists which I want to use to create a dictionary. List x would be the keys, and list y is the values. x = y = Any suggestions? looking for an efficent simple way to do this...maybe i am just having a brain fart...i feel like this is quit simple. thanks.
5
2287
by: bruce | last post by:
hi... i'm trying to deal with multi-dimension lists/arrays i'd like to define a multi-dimension string list, and then manipulate the list as i need... primarily to add lists/information to the 'list/array' and to compare the existing list information to new lists i'm not sure if i need to import modules, or if the base python install i have is sufficient. an example, or pointer to examples would be good...
16
3661
by: agent-s | last post by:
Basically I'm programming a board game and I have to use a list of lists to represent the board (a list of 8 lists with 8 elements each). I have to search the adjacent cells for existing pieces and I was wondering how I would go about doing this efficiently. Thanks
20
2429
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001: ,
6
2731
by: Ernst-Ludwig Brust | last post by:
Given 2 Number-Lists say l0 and l1, count the various positiv differences between the 2 lists the following part works: dif= da={} for d in dif: da=da.get(d,0)+1 i wonder, if there is a way, to avoid the list dif
0
9669
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10427
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
10207
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
10155
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
9995
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
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.