rggod
rggod

Reputation: 629

creating a table out of list of data

so i have 3 list of data,

person=['bobby','tim','sarah','tim','sarah','bobby,'tim','sarah','bobby,]
places=["loc1","loc1","loc2","loc1","loc2","loc2","loc1","loc2",'loc1"]

i have to use this data to show how time a person visited a certain place. how would i be able to use the above list to get something like that

person          loc1          loc2
bobby            2              1
tim              3              0
sarah            0              3

if the list the list was like bobby=['loc1','loc1','loc2'] i could use bobby.count(loc1). to find the data need but it is different here, also I have no idea how to make the table i am not entire codes , i just need to know how i should start .

Upvotes: 0

Views: 93

Answers (3)

slider
slider

Reputation: 12990

I would use dictionaries. Start by creating a dictionary of dictionaries for each name like so (see how to create it below):

data = { 'bobby' : {'loc1':0, 'loc2':0},
         'sarah' : {'loc1':0, 'loc2':0},
         'tim'   : {'loc1':0, 'loc2':0} }

If you don't know your places and person list, you can use sets to make the data dictionary in the above form (there are ways, but here's my way):

from sets import Set
data = dict( (name, dict( (place, 0) for place in Set(places) ) ) for name in Set(person) )

And then simply loop through your lists, incrementing relevant places:

for i in range(len(person)):
    data[ person[i] ][ places[i] ] += 1

When your data dictionary is ready, you can print it however you want.

Here's how the data dictionary looks like after running the above code on the lists you provided.

>>> for key in sorted(data): print key, data[key]
... 
bobby {'loc2': 1, 'loc1': 2}
sarah {'loc2': 3, 'loc1': 0}
tim {'loc2': 0, 'loc1': 3}

Upvotes: 0

flyer
flyer

Reputation: 9846

If you have to have two lists to store persons and theirs places, you can have the following code to construct a dict that indicates which person has visited which places and the times.

persons = ['bobby', 'tim', 'sarah', 'tim', 'sarah', 'bobby', 'tim', 'sarah', 'bobby',]
places = ['loc1', 'loc1', 'loc2', 'loc1', 'loc2', 'loc2', 'loc1', 'loc2', 'loc1']

person_to_places = {}
total = len(persons)

for i in xrange(total):
    if not person_to_places.has_key(persons[i]):
        person_to_places[persons[i]] = []
    person_to_places[person[i]].append(places[i])

for name, place in person_to_places.items():
    person_to_places[name] = {}
    for p in places:
        if not person_to_places[name].has_key(p):
            person_to_places[name][p] = 0
        person_to_places[name][p] += 1

then, you have a dict *person_to_places* like this:

{'bobby': {'loc1': 2, 'loc2': 1}, 'sarah': {'loc2': 3}, 'tim': {'loc1': 3}}

After that, output the table as you like.

Upvotes: 0

aIKid
aIKid

Reputation: 28352

Use a temporary dict to store the values:

d = {}
for name, loc in zip(person, places):
    d.setdefault(name, []).append(loc)

And to print it:

>>> for k, v in d.items():
    print(k, end='\t')
    print(v.count('loc1'), end='\t')
    print(v.count('loc2'))


tim     3   0
bobby   2   1
sarah   0   3

Hope this helps!

Upvotes: 2

Related Questions