[Tutor] arrary stastic

Dave Angel d at davea.name
Tue Oct 4 18:26:09 CEST 2011


On 10/04/2011 12:01 PM, bob gailer wrote:
> 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
>
I'll second Bob's request, and go further:  Move this discussion into  
the original thread, and repeat in one place your assumptions for input, 
requirements for output, the code you're using and how the output 
differs from what you expect.

You also need to give your target python version, and if you're 
currently getting an exception the full stack trace.  Finally, if it's 
relevant, the operating system as well.

I think you've given almost all of this, except the python version and 
the expected file output.

-- 

DaveA



More information about the Tutor mailing list