[Tutor] improve the code
lina
lina.lastname at gmail.com
Tue Nov 1 16:35:20 CET 2011
On Tue, Nov 1, 2011 at 11:28 PM, Peter Otten <__peter__ at web.de> wrote:
> lina wrote:
>
>> On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel <d at davea.name> wrote:
>>> On 11/01/2011 10:11 AM, lina wrote:
>
>>> Just use the sort() method of the list object. In particular, items()
>>> returns an unordered list, so it's ready to be sorted.
>>>
>>> for residues, numbers in new_dictionary.items().sort():
>>>
>>> That will sort such that residues are in sorted order.
>>
>> Thanks, but still something went wrong here,
>>
>> Traceback (most recent call last):
>> File "fill-gap.py", line 41, in <module>
>> build_abetadictionary(DICTIONARYFILE,orig_dictionary)
>> File "fill-gap.py", line 31, in build_abetadictionary
>> for residues, numbers in new_dictionary.items().sort():
>> AttributeError: 'dict_items' object has no attribute 'sort'
>
> Dave didn't realize that you are using Python 3 where items() no longer
> returns a list. You need to change
>
> new_dictionary.items().sort()
>
> to
>
> sorted(new_dictionary.items())
Thanks, it works, but there is still a minor question,
can I sort based on the general numerical value?
namely not:
:
:
83ILE 1
84ALA 2
8SER 0
9GLY 0
:
:
rather 8 9 ...83 84,
Thanks,
>
> as sorted() will accept an arbitrary iterable.
>
>> I have another concerns,
>> is it possible to append the output file content as a sing one,
>> such as a.new is
>> A 1
>> B 3
>>
>> b.new is
>> A 3
>> B 5
>>
>> I wish the final one like:
>>
>> A 1 3
>> B 3 5
>>
>> I will think about it. Thanks,
>
> Sorry, I can't make sense of that.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list