Help with Dictionaries and Classes requested please.
Steve Holden
steve at holdenweb.com
Thu Aug 9 08:33:43 EDT 2007
special_dragonfly wrote:
> "special_dragonfly" <Dominic at PLEASEASK.co.uk> wrote in message
> news:46baf384$0$11943$7b0f0fd3 at mistral.news.newnet.co.uk...
>> "Bruno Desthuilliers" <bruno.42.desthuilliers at wtf.websiteburo.oops.com>
>> wrote in message news:46baf183$0$433$426a74cc at news.free.fr...
>>> special_dragonfly a écrit :
>>> (snip)
>>>> I've managed to solve the problem, I really was just being a dunce.
>>>> Here's how incase anyone is wondering:
>>>>
>>>> class MyClass:
>>>> def __init__(self):
>>>> name=""
>>>> dict={}
>>>> dict[0]=[]
>>>> dict[0].append(MyClass())
>>>> dict[0][0].name="Hello"
>>>> print dict[0][0].name
>>>>
>>>> I'm sorry if I've wasted anyones time, although if there's a better way
>>>> of doing the above I'd still be interested to know.
>>> # unless you need pre 2.3.x compat, better to use newstyle classes
>>> class MyClass(object):
>>> # use the initializer to initialize your instance
>>> def __init__(self, name=''):
>>> # the use of 'self' is mandatory, else you only have a local var
>>> self.name = name
>>>
>>> # don't use builtin names as identifiers - unless you really want to
>>> # shadow the builtins
>>> d = {0:[MyClass('hello')}
>>> d[0].append(MyClass('goodbye'))
>>> d.setdefault(1, []).append(MyClass('Yo'))
>>> print d
>>>
>>> HTH
>
>
> Is there anyway for python to consider the values within a string when
> entering the data into a dictionary. I know that isn't very clear so here's
> an example:
>
> class MyClass(object):
> def __init__(self,name="",age=""):
> self.name=name
> self.age=age
>
> data="Gary,50"
> d={0:[MyClass(data)]}
> data="Adam,25"
> d[0].append(MyClass(data))
>
> The data is coming from a text file working on a line by line basis. I've
> just tried and I'm just getting the full string in the first field. That
> seems logical, now I don't want it to though!
>
If you mean you would like to store a list of values then you could try
something like
d = {0: MyClass(*data.split(",")}
to create the objects. The .split() method turns the string into a list
of substrings, and the * unary operator turns that list into individual
arguments passed to the __init__() method.
However, your recent question history indicates that you would do well
to study the Python tutorial and other beginner material, as you will
find many valuable hints as to Python best practices which will save you
a lot of time.
Also there is the python-help at python.org list if you are having problems
of a "beginner" nature, where you might get more practical help in your
initial use of the language.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
More information about the Python-list
mailing list