[Tutor] arrary stastic

bob gailer bgailer at gmail.com
Tue Oct 4 18:01:27 CEST 2011


On 10/4/2011 10:07 AM, lina wrote:
> want to do a statistic of the concurrence of E,
>
>     namely,
>     how many times the E showed up,
>
>     so the first column is 1, second column and then following.
>
>     Thanks,
>
>     I have one, which showed something like:
>
>
>
>     tokens=['E']
>
>     result=[]
>     filedata = open("try.xpm")
>     text=filedata.readlines()
>
>     for line in text:
>         result.append({t:line.count(t) for t in tokens})
>
>     for index,r in enumerate(result):
>         print(index,"-----",r)
>
>     The error message is:
>
>
>         result.append({t:line.count(t) for t in tokens})
>                                          ^
>     SyntaxError: invalid syntax
>
You are tryng to use a list comrehensioin for the dictionary value, 
without the []. Try this:

    result.append({t:[line.count(t) for t in tokens]})

You should then see:
0, '-----', {'E': [1]})
(1, '-----', {'E': [2]})
(2, '-----', {'E': [2]})

which is not what you want!

As requested before, show us the output you do want. Not a description 
but the actual output.

-- Bob Gailer 919-636-4239 Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111004/d23d31ed/attachment.html>


More information about the Tutor mailing list