[Tutor] pruning and ordering a list of lists

Bob Gailer bgailer at alum.rpi.edu
Sat Mar 24 01:31:51 CET 2007


William O'Higgins Witteman wrote:
> I have a list of lists derived from a log file that I want to create a
> summary of, but I am not sure of an approach to do what I need.
>
> Here's a sample of the data:
>
> [["user1","18/Mar/2007:07:52:38 -0400"],["user1","18/Mar/2007:07:52:40 -0400"],["user2","18/Mar/2007:07:52:42 -0400"],["user3","18/Mar/2007:07:52:42 -0400"],["user2","18/Mar/2007:07:52:43 -0400"]]
>
> What I want as output is something like this:
>
> [["first user alphabetically","most recent timestamp for this user"],["second user alphabetically","most recent timestamp for this user"], ...]
>
> Can anyone suggest an approach for this?  Thanks.
>   
# following code is untested
# assume your data is in variable log:
userData = {} # setup a dictionary to collect latest timestamp for each user
for user, timestamp in log:
  if user not in userData or timestamp > userData[user]
    # note that we need a way to compare timestamps
    # the current representation does not support this
    userData[user] = timestamp
userData2 = userData.items().sorted()

-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list