[Tutor] swapping list elements based on some criterion

Emad Nawfal (عمـ نوفل ـاد) emadnawfal at gmail.com
Sat Oct 8 14:18:33 CEST 2011


On Sat, Oct 8, 2011 at 12:34 PM, Andreas Perstinger <
andreas.perstinger at gmx.net> wrote:

> On 2011-10-08 11:11, Andreas Perstinger wrote:
>
>> def swap(sentence):
>>      s = sentence.split()
>>      for i in reversed(range(len(s))):
>>          if s[i].endswith("/N") and s[i-1].endswith("/ADJ"):
>>            s[i], s[i-1] = s[i-1], s[i]
>>      return s
>>
>
> Oops, noticed a little bug:
>
> The for-loop should be "for i in reversed(range(1, len(s))):" so that
> sentences starting with a noun get processed correctly:
>
> >>> swap("Joe/N went/VBZ crazy/ADJ")
> ['Joe/N', 'went/VBZ', 'crazy/ADJ']
>
> Thanks again Andreas
> Bye, Andreas
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111008/898184d0/attachment.html>


More information about the Tutor mailing list