Newbie help with array handling

Shane Geiger sgeiger at ncee.net
Thu Apr 12 14:37:23 EDT 2007



"""

Since you are a beginner and since RCCILA (Runnable, Commented Code Is 
Least Ambiguous) I'm proving this example code to help you get used to 
manipulating data with python.  This should give you an idea of how to 
juggle a bit.  After you learn how to do this you likely still will not 
be juggling in the optimal way, but you will at least be able to manage 
your data.

Not having an ordered dictionary causes beginners some grief because 
they haven't yet wrapped their minds around the way the basic data types 
work.  (I don't recally understand why ordered dictionaries are not part 
of the standard library.  It seems that this would be a commonly used 
feature.)  However, UserDict is part of the standard library, and it can 
be used to create an ordered dict.  Due to Python's attempt to be 
accessible to all people, I would think a little more support for an 
ordered dictionary would be in Python already.

Nevertheless, beginners often stumble because they think they need an 
ordered dict when they really just need to learn to use various data 
types together.  I think this covers the basics.  If you can think of 
something not covered, let me know so I can add it to my examples.

"""

## dictionary
d1 = {
    'keyvalue2':[ 'value21', 'value22', 'value23'],
    'keyvalue3':[ 'value31', 'value32', 'value33'],
    'keyvalue1':['value11', 'value12', 'value13']
        }

d2 = {
    'keyvalue4':[ 'value41', 'value42', 'value43'],
    'keyvalue6':[ 'value61', 'value62', 'value63'],
    }

# merge the two dictionaries
d1.update(d2)


print d1.has_key('keyvalue3')  # True

# get the values of a dictionary key
v1,v2,v3 = d1['keyvalue3']
print v1,v2,v3   # value31 value32 value33


# now they are combined
print d1  # {'keyvalue4': ['value41', 'value42', 'value43'], 
'keyvalue6': ['value61', 'value62', 'value63'], 'keyvalue1': ['value11', 
'value12', 'value13'], 'keyvalue3': ['value31', 'value32', 'value33'], 
'keyvalue2': ['value21', 'value22', 'value23']}

# now they are a list of tuples
myList = list( d1.items() )

print myList #   [('keyvalue4', ['value41', 'value42', 'value43']), 
('keyvalue6', ['value61', 'value62', 'value63']), ('keyvalue1', 
['value11', 'value12', 'value13']), ('keyvalue3', ['value31', 'value32', 
'value33']), ('keyvalue2', ['value21', 'value22', 'value23'])]

myList.sort()  # sorts on the first item in the tuple
print myList  # [('keyvalue1', ['value11', 'value12', 'value13']), 
('keyvalue2', ['value21', 'value22', 'value23']), ('keyvalue3', 
['value31', 'value32', 'value33']), ('keyvalue4', ['value41','value42', 
'value43']), ('keyvalue6', ['value61', 'value62', 'value63'])]

newDictionary = {}

# loop through all the items in the list and create a dictionary
for key, values in myList:
    newDictionary[key] = values

print newDictionary  # 'keyvalue4': ['value41', 'value42', 'value43'], 
'keyvalue6': ['value61', 'value62', 'value63'], 'keyvalue1': ['value11', 
'value12', 'value13'], 'keyvalue3': ['value31', 'value32', 'value33'], 
'keyvalue2': ['value21', 'value22', 'value23']}


> do something like this
> {keyvalue1:[ value1, value2, value3],keyvalue2:[value1,value2, 
> value3],keyvalue3,:[value1,value2,value3]}
>
> On 12 Apr 2007 00:58:54 -0700, *loial* < admin at loial.co.uk 
> <mailto:admin at loial.co.uk>> wrote:
>
>     I am new to python and am converting an awk script to python
>
>     I need to store some data in an array/table of some form
>
>     keyvalue1, value1, value2, value3
>     keyvalue2, value1,value2, value3
>     keyvalue3, value1,value2,value3
>     etc
>
>     I will later need to sort in keyvalue order and also need to be able
>     to check if a key already exists
>
>     It is not clear how to do this in python. All the examples I see have
>     just a key and a single value
>
>     --
>     http://mail.python.org/mailman/listinfo/python-list
>
>
>
>
> -- 
> Regards--
> Rishi Pathak
> National PARAM Supercomputing Facility
> Center for Development of Advanced Computing(C-DAC)
> Pune University Campus,Ganesh Khind Road
> Pune-Maharastra 

-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070412/25cf9c54/attachment.vcf>


More information about the Python-list mailing list