[Tutor] swapping list elements based on some criterion
Emad Nawfal (عمـ نوفل ـاد)
emadnawfal at gmail.com
Sat Oct 8 08:37:33 CEST 2011
Hi Steven ans Tutors,
Thanks for the code.
Actually I'm not a student at all. I'm an assistant professor of Arabic at
Suez Canal University in Egypt. I would appreciate more contributions from
you and the other list members.
2011/10/8 Steven D'Aprano <steve at pearwood.info>
> Emad Nawfal (عمـ نوفل ـاد) wrote:
>
>> Hello Tutors,
>> It's been quite some time since I last posted something here, and now I'm
>> back with a question:
>>
>> I want to re-structure English so that the adjectives appear after the
>> nouns, instead of before.
>> If I have a sentence like:
>>
>> The tall man plays well
>> I need to change it to
>> The man tall plays well
>> So far, I have thought of the following function, but I'm not sure it's
>> the
>> best solution. It assumes that the sentence has been augmented with part
>> of
>> speech tags
>> (Also plz note that there may be multiple adjectives)
>>
>
> And what do you expect to happen with multiple adjectives?
>
> "the tall fat bald man"
>
> => "the man tall fat bald"
> => "the man fat tall bald"
> => "the man bald fat tall"
> => "the man fat bald tall"
> => something else?
>
> The approach I would take is this:
>
>
> Walk along the sentence, inspecting each word.
> Collect all consecutive adjectives into a list of adjectives
> Other words get copied straight into the buffer.
> When you reach a noun, copy the noun into the buffer, plus the list
> of adjectives (in whichever order you like!), then clear the list
> of adjectives ready for the next noun.
>
> Does that help?
>
> Something like this:
>
> def swap(sentence):
> buffer = []
> adjectives = []
> for word in sentence.split():
> if word.endswith('/ADJ'):
> adjectives.append(word)
> elif word.endswith('/N'):
> buffer.extend(adjectives)
> buffer.append(word)
> adjectives = []
> else:
> buffer.append(word)
> return ' '.join(buffer)
>
>
> P.S. Since this looks like homework, I have left a deliberate mistake in my
> code for you to fix. But it should be easy to fix if you study the code and
> think about what it is doing.
>
>
> --
> Steven
> ______________________________**_________________
> 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/a8a98174/attachment-0001.html>
More information about the Tutor
mailing list