Newbie help with array handling

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Apr 12 04:44:42 EDT 2007


loial a écrit :
> 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

data = dict(
   keyvalue1=[value11, value12, value13],
   keyvalue2=[value21, value22, value23],
   keyvalue3=[value31, value32, value33]
)


> I will later need to sort in keyvalue order

for key in sorted(data.keys()):
   print "%s : %s" % (key, data[key])

> and also need to be able
> to check if a key already exists

if somekey in data:
   print "somekey 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

Everything in Python is an object, so nothing prevents you from using 
lists as 'values'.

HTH



More information about the Python-list mailing list