Newbie help with array handling

L

loial

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
 
B

bruce peng

use dir with list nested

for example:
{key1 : [value1, value2, value3], key2 : [value1, value2, vlaue4]}
 
B

bearophileHUGS

loial:
I am new to python and am converting an awk script to python

It seems there are many people trying to convert awk code 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

The problem with multiple values is easy to manage, you just put them
into a list:
[value1, value2, value3]
Such list can be used as the a value for a key:value pair inside a
dict, etc.

The problem is that Python doesn't have a built-in sorted dictionary
data structure. So if you need it there are some solutions:

1) If you don't need to cheek presence often, and you don't need to
remove many key:value pairs, then you can just use a list of lists
like this (a Python list is an array of references dynamic on the
right):

data = [[keyvalue1, value1, value2, value3], [keyvalue2,
value1,value2, value3], [keyvalue3, value1,value2,value3], ...]

Then you can test the presence of a key with something like:
key in (subl[0] for subl in data)

Such list data can be sorted too according to the key (untested), if
the keys are sortable objects:
from operator itemgetter
data.sort(key=itemgetter(0))


2) If you need the dict characteristics a lot, then you may use a dict
(but keys must be hashable objects):
ddata = {keyvalue1:[value1, value2, value3], keyvalue2:[value1,
value2, value3], ...}

This allows quick presence testing, insertions and removals, but it
can't be sorted according to the keys. So you may need to keep a
sorted list of the keys:
skeys = sorted(ddata)
Then you can use skeys as you need, fetching the values from ddata if/
when you need them (note that there is a bisect standard module too
that may help the management of a sorted list).
But you have to keep the two structures updated at the same time, or
re-create new skeys now and then...


3) If you want to be sure the two structures are always the same, then
you may need to find an ordered dict class around (or you can write it
yourself, but it may be too difficult for you), such implementation
usually keep a dict and a list inside, and keep them in sync. You can
probably use it as a normal dict, so its usage is rather clean and
easy.
Most of such implementations have a slow removal and add of items,
because removing/adding them from the list requires time. If you need
to perform many such operations on a lot of data, then there is a way
to do that too, using a different implementation of ordered dict, you
can start from this, that uses a double linked list structure, it's
slow, but such time is costant (note that this keeps the order of the
insertions, it doesn't sort keys according to their natural order, so
you need to modify the code):
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498195

Bye,
bearophile
 
B

Bruno Desthuilliers

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
 
7

7stud

loial said:
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

Any python type can be a value for a key/value pair. See if this
helps:

mydict = { "zkeyvalue1":[1, "red", 2.5], "akeyvalue2":[1.5, "black",
30] }

key = "keyvalue50"
if key in mydict:
print "found it"
else:
print "invalid key"

lst = sorted(mydict)
print lst

for key in lst:
print mydict[key],
 
L

loial

OK, thanks for the replies

One other thing...I need to update the values since they are basically
totals that I am accumulating.

How do I update a specific value for a specific key?
 
7

7stud

OK, thanks for the replies

One other thing...I need to update the values since they are basically
totals that I am accumulating.

How do I update a specific value for a specific key?

mydict["keyvalue1"]

returns the value, which in this case is a list. So, you could write:

lst = mydict["keyvalue1"]
lst[0] = 4
list.append("red")

or you can do it directly:

mydict["keyvalue1"][0] = 4
mydict["keyvalue1"].append("red")
 
S

Shane Geiger

"""

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* < (e-mail address removed)

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
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top