[Tutor] Help with a Dictionary

Garry Bettle garry.bettle at gmail.com
Thu Jan 7 17:12:32 CET 2010


On Thu, Jan 7, 2010 at 15:26,  Garry Bettle wrote:
>
> Howdy all,
>
> I hope this message finds you all well.
>
> I have a list that I output in the following order:
>
> 2010-01-07 1103 Sund A7 450m
> 2010-01-07 1111 Sheff A7 500m
> 2010-01-07 1119 Sund A6 450m
> 2010-01-07 1128 Sheff A6 500m
> 2010-01-07 1134 Sund A5 450m
> 2010-01-07 1142 Sheff A7 500m
> 2010-01-07 1148 Sund A5 450m
> 2010-01-07 1157 Sheff A6 500m
> 2010-01-07 1204 Sund A4 450m
> 2010-01-07 1212 Sheff A5 500m
> 2010-01-07 1218 Sund A4 450m
> 2010-01-07 1227 Sheff A3 500m
> 2010-01-07 1232 Sund A4 450m
> 2010-01-07 1242 Sheff A4 500m
> 2010-01-07 1247 Sund A4 450m
> 2010-01-07 1258 Sheff A3 500m
> 2010-01-07 1304 Sund A3 450m
> 2010-01-07 1312 Sheff A4 500m
> 2010-01-07 1319 Sund HC 640m
> 2010-01-07 1327 Sheff A5 500m
> 2010-01-07 1333 Sund A3 450m
> 2010-01-07 1344 Sheff A3 500m
> 2010-01-07 1351 Sund A2 450m
> 2010-01-07 1403 Sheff A2 500m
> 2010-01-07 1408 Romfd A7 400m
> 2010-01-07 1418 Crayfd S8 540m
> 2010-01-07 1427 Romfd A6 400m
> 2010-01-07 1437 Crayfd H3 380m
> ... etc.
>
> The above are RaceDate + RaceTime + Fixture + RaceDetails, and are
> output in RaceTime order.
>
> What I'd like to do, is output a transposed-like summary of just the
> Fixture + RaceTime.
>
> i.e.
>
> Sund   1103 1119 1134 1148 1204 1218 1232 1247 1304 1319 1333 1351
> Sheff   1111 1128 1142 1157 1212 1227 1242 1258 1312 1327 1344 1403
> Romfd 1408 1427 ...
> Crayfd 1418 1437 ...
>
> Do I need to use a Dictionary?
>
> Many thanks!
>
> Cheers,
>
> Garry


This is what I've come up with.  Sorry, python is something I touch on
occasionally:  must do more!

As the races are output, I build a dictionary of key=FixtureName and
value=RaceTimes:

RaceTime = marketResp.market.displayTime.time()
cRaceTime = RaceTime.strftime("%H%M")
FixtureName = MarketSummary.marketName.strip()
MarketName = marketResp.market.name
if FixtureName not in FixtureList:
    FixtureList[FixtureName] = cRaceTime
else:
    FixtureList[FixtureName]+= " " + cRaceTime

And then, when I want the summary to be printed:

for fixture in FixtureList:
    print fixture.ljust(6), FixtureList[fixture]

It works, but have I done it the "python" way?  Can't I unpack both
the key and value from FixtureList?
This doesn't work:

for fixture, racetimes in FixtureList:
    print fixture, racetimes

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    for fixture, racetimes in FixtureList:
ValueError: too many values to unpack

Another thing I'd like to change is the ljust().  I believe it's
depreciated later in 3.0+, so I should really find an alternative.

Cheers,

G.


More information about the Tutor mailing list