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. 13 1504
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.
A python version of what you describe:
class TooManyAttendan ts(Exception): pass
class Attendence(obje ct):
def __init__(self, max):
self.max = int(max)
self.total = 0
def accrue(self, other):
self.total += int(other)
if self.total self.max: raise TooManyAttendan ts
def __str__(self):
return "%s/%s" % (self.max, self.total)
__repr__ = __str__
data = {}
for i, line in enumerate(file( "input.txt" )):
print line,
state, county, school, cls, max_students, enrolled = map(
lambda s: s.strip(),
line.rstrip("\r \n").split(", ")
)
try:
data.setdefault (
state, {}).setdefault(
county, {}).setdefault(
cls, Attendence(max_ students)).accr ue(enrolled)
except TooManyAttendan ts:
print "Too many Attendants in line %i" % (i + 1)
print repr(data)
You can then access things like
a = data["Nebraska"]["Wabash"]["Newville"]["Math"]
print a.max, a.total
If capitalization varies, you may have to do something like
data.setdefault (
state.upper(), {}).setdefault(
county.upper(), {}).setdefault(
cls.upper(), Attendence(max_ students)).accr ue(enrolled)
to make sure they're normalized into the same groupings.
-tkc
Tim Chase:
__repr__ = __str__
I don't know if that's a good practice.
try:
data.setdefault (
state, {}).setdefault(
county, {}).setdefault(
cls, Attendence(max_ students)).accr ue(enrolled)
except TooManyAttendan ts:
I suggest to decompress that part a little, to make it a little more
readable.
Bye,
bearophile
> __repr__ = __str__
>
I don't know if that's a good practice.
I've seen it in a couple places, and it's pretty explicit what
it's doing.
> try: data.setdefault ( state, {}).setdefault( county, {}).setdefault( cls, Attendence(max_ students)).accr ue(enrolled) except TooManyAttendan ts:
I suggest to decompress that part a little, to make it a little more
readable.
I played around with the formatting and didn't really like any of
the formatting I came up with. My other possible alternatives were:
try:
data \
.setdefault(sta te, {}) \
.setdefault(cou nty, {}) \
.setdefault(cls , Attendence(max_ students)) \
.accrue(enrolle d)
except TooManyAttendan ts:
or
try:
(data
.setdefault(sta te, {})
.setdefault(cou nty, {})
.setdefault(cls , Attendence(max, 0))
).accrue(enroll ed)
except TooManyAttendan ts:
Both accentuate the setdefault() calls grouped with their
parameters, which can be helpful. Which one is "better" is a
matter of personal preference:
* no extra characters but hard to read
* backslashes, or
* an extra pair of parens
-tkc
En Mon, 06 Oct 2008 22:52:29 -0300, Tim Chase
<py*********@ti m.thechases.com escribió:
>> __repr__ = __str__
[be************@ lycos.com wrote]
> I don't know if that's a good practice.
I've seen it in a couple places, and it's pretty explicit what it's
doing.
__repr__ is used as a fallback for __str__, so just defining __repr__ (and
leaving out __str__) is enough.
--
Gabriel Genellina
Tim Chase <py*********@ti m.thechases.com writes:
>> __repr__ = __str__
I don't know if that's a good practice.
I've seen it in a couple places, and it's pretty explicit what it's
doing.
But what's the point? Simply define __repr__, and both repr and str
will pick it up.
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.
One of the routines in the actual program checks a few thousand
computers to verify that certain processes are running. I didn't want
to complicate my original question by going through all of the gory
details (multiple userids running many processes with some of the
processes having the same name). To save time, I fork a process for
each computer that I'm checking. It seems to me that banging away at a
database would greatly slow down the program and make the program more
complicated.
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.
Would the following be suitable data structure:
....
struct = {}
struct["Nebraska"] = "Wabash"
struct["Nebraska"]["Wabash"] = "Newville"
struct["Nebraska"]["Wabash"]["Newville"]["topics"] = "Math"
struct["Nebraska"]["Wabash"]["Newville"]["Math"]["Max Allowed Students"] = 20
struct["Nebraska"]["Wabash"]["Newville"]["Math"]["Current enrolled Students"] = 0
....
Have an easy Yom Kippur,
Ron.
-----Original Message-----
From: Pat [mailto:Pa*@junk .net]
Sent: Wednesday, October 08, 2008 04:16
To: py*********@pyt hon.org
Subject: Re: Array of dict or lists or ....?
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 ofdefining the data structure. I used a fictional example to make it easierto (hopefully) convey how the data is laid out.
One of the routines in the actual program checks a few thousand computers to verify that certain processes are running. I didn't want to complicate my original question by going through all of the gory details (multiple userids running many processes with some of the processes having the same name).. To save time, I fork a process for each computer that I'm checking. It seems to me that banging away at a database would greatly slow down the program and make the program more complicated.
The Perl routine works fine and I'd like to emulate that behavior but sinceI'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.
On Oct 7, 10:16*am, "Barak, Ron" <Ron.Ba...@lsi. comwrote:
Would the following be suitable data structure:
...
struct = {}
struct["Nebraska"] = "Wabash"
struct["Nebraska"]["Wabash"] = "Newville"
struct["Nebraska"]["Wabash"]["Newville"]["topics"] = "Math"
struct["Nebraska"]["Wabash"]["Newville"]["Math"]["Max Allowed Students"] = 20
struct["Nebraska"]["Wabash"]["Newville"]["Math"]["Current enrolled Students"] = 0
...
That's not quite right as stated.
>>struct = {} struct["Nebraska"] = "Wabash" struct["Nebraska"]["Wabash"] = "Newville"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
-----Original Message-----
From: py************* *************** ****@python.org [mailto:python-
li************* ************@py thon.org] On Behalf Of Pat
Sent: Tuesday, October 07, 2008 10:16 PM
To: py*********@pyt hon.org
Subject: Re: Array of dict or lists or ....?
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.
states = {}
if 'georgia' not in states:
states['georgia'] = {}
states['georgia']['fulton'] = {}
states['georgia']['fulton']['ps101'] = {}
states['georgia']['fulton']['ps101']['math'] = {}
states['georgia']['fulton']['ps101']['math']['max'] = 100
states['georgia']['fulton']['ps101']['math']['current'] = 33
states['georgia']['dekalb'] = {}
states['georgia']['dekalb']['ps202'] = {}
states['georgia']['dekalb']['ps202']['english'] = {}
states['georgia']['dekalb']['ps202']['english']['max'] = 500
states['georgia']['dekalb']['ps202']['english']['current'] = 44
print states
*****
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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"
|
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...
|
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}
|
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...
|
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.
| |
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...
|
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
|
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: ,
|
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
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |