Re: [Tutor] sorting file paths made up of of strings and numbers

2009-04-15 Thread عماد نوفل
On Tue, Apr 14, 2009 at 10:36 PM, Bill Campbell  wrote:

> On Tue, Apr 14, 2009, Emad Nawfal ( ) wrote:
> >
> >   Hi tutors,
> >   How can I sort the following list in a way that takes care of the
> >   right order of numbers? The sorted function compares strings here as
> >   far as I can see, but I want to have filepath2 follow filepath1. Your
> >   help is appreciated.
>
> There is at least one example of a method of dealing with this in
> the O'Reilly ``Python cookbook''.
>
> >   >>> myList
> >   ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89',
> >   'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15',
> >   'filepath88', 'filepath70', 'filepath33', 'filepath58', 'filepath28',
> >   'filepath61', 'filepath23', 'filepath64', 'filepath72', 'filepath11',
> >   'filepath63', 'filepath76', 'filepath87', 'filepath45', 'filepath41',
> >   'filepath68', 'filepath81', 'filepath75', 'filepath62', 'filepath46',
> >   'filepath36', 'filepath39', 'filepath5', 'filepath34', 'filepath8',
> >   'filepath67', 'filepath14', 'filepath82', 'filepath29', 'filepath47',
> >   'filepath79', 'filepath13', 'filepath16', 'filepath71', 'filepath27',
> >   'filepath100', 'filepath20', 'filepath92', 'filepath57', 'filepath73',
> >   'filepath40', 'filepath44', 'filepath83', 'filepath50', 'filepath66',
> >   'filepath94', 'filepath86', 'filepath22', 'filepath17', 'filepath84',
> >   'filepath38', 'filepath12', 'filepath53', 'filepath6', 'filepath48',
> >   'filepath60', 'filepath51', 'filepath90', 'filepath4', 'filepath78',
> >   'filepath69', 'filepath35', 'filepath97', 'filepath93', 'filepath24',
> >   'filepath26', 'filepath1', 'filepath3', 'filepath96', 'filepath77',
> >   'filepath98', 'filepath18', 'filepath43', 'filepath19', 'filepath56',
> >   'filepath65', 'filepath95', 'filepath55', 'filepath7', 'filepath99',
> >   'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10',
> >   'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74',
> >   'filepath21']
> >   >>> sorted(myList)
> >   ['filepath0', 'filepath1', 'filepath10', 'filepath100', 'filepath11',
> >   'filepath12', 'filepath13', 'filepath14', 'filepath15', 'filepath16',
> >   'filepath17', 'filepath18', 'filepath19', 'filepath2', 'filepath20',
> >   'filepath21', 'filepath22', 'filepath23', 'filepath24', 'filepath25',
> >   'filepath26', 'filepath27', 'filepath28', 'filepath29', 'filepath3',
> >   'filepath30', 'filepath31', 'filepath32', 'filepath33', 'filepath34',
> >   'filepath35', 'filepath36', 'filepath37', 'filepath38', 'filepath39',
> >   'filepath4', 'filepath40', 'filepath41', 'filepath42', 'filepath43',
> >   'filepath44', 'filepath45', 'filepath46', 'filepath47', 'filepath48',
> >   'filepath49', 'filepath5', 'filepath50', 'filepath51', 'filepath52',
> >   'filepath53', 'filepath54', 'filepath55', 'filepath56', 'filepath57',
> >   'filepath58', 'filepath59', 'filepath6', 'filepath60', 'filepath61',
> >   'filepath62', 'filepath63', 'filepath64', 'filepath65', 'filepath66',
> >   'filepath67', 'filepath68', 'filepath69', 'filepath7', 'filepath70',
> >   'filepath71', 'filepath72', 'filepath73', 'filepath74', 'filepath75',
> >   'filepath76', 'filepath77', 'filepath78', 'filepath79', 'filepath8',
> >   'filepath80', 'filepath81', 'filepath82', 'filepath83', 'filepath84',
> >   'filepath85', 'filepath86', 'filepath87', 'filepath88', 'filepath89',
> >   'filepath9', 'filepath90', 'filepath91', 'filepath92', 'filepath93',
> >   'filepath94', 'filepath95', 'filepath96', 'filepath97', 'filepath98',
> >   'filepath99']
> >   >>> ```
> >   --
> >   áÇ ÃÚÑÝ ãÙáæãÇ ÊæÇØà ÇáäÇÓ Úáí åÖãå æáÇ ÒåÏæÇ Ýí ÅäÕÇÝå
> >   ßÇáÍÞíÞÉ.ãÍãÏ ÇáÛÒÇáí
> >   "No victim has ever been more repressed and alienated than the truth"
> >   Emad Soliman Nawfal
> >   Indiana University, Bloomington
> >   
>
> >___
> >Tutor maillist  -  Tutor@python.org
> >http://mail.python.org/mailman/listinfo/tutor
>
>
> --
> Bill
> --
> INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
> URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
> Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
> Fax:(206) 232-9186  Skype: jwccsllc (206) 855-5792
>
> An almost hysterical antagonism toward the gold standard is one issue which
> unites statists of all persuasions.  They seem to sense that gold and
> economic freedom are inseparable.  -- Alan Greenspan
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


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

Emad Soliman Nawfal
Indiana University, Bloomington
-

Re: [Tutor] sorting file paths made up of of strings and numbers

2009-04-14 Thread Bill Campbell
On Tue, Apr 14, 2009, Emad Nawfal ( ) wrote:
>
>   Hi tutors,
>   How can I sort the following list in a way that takes care of the
>   right order of numbers? The sorted function compares strings here as
>   far as I can see, but I want to have filepath2 follow filepath1. Your
>   help is appreciated.

There is at least one example of a method of dealing with this in
the O'Reilly ``Python cookbook''.

>   >>> myList
>   ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89',
>   'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15',
>   'filepath88', 'filepath70', 'filepath33', 'filepath58', 'filepath28',
>   'filepath61', 'filepath23', 'filepath64', 'filepath72', 'filepath11',
>   'filepath63', 'filepath76', 'filepath87', 'filepath45', 'filepath41',
>   'filepath68', 'filepath81', 'filepath75', 'filepath62', 'filepath46',
>   'filepath36', 'filepath39', 'filepath5', 'filepath34', 'filepath8',
>   'filepath67', 'filepath14', 'filepath82', 'filepath29', 'filepath47',
>   'filepath79', 'filepath13', 'filepath16', 'filepath71', 'filepath27',
>   'filepath100', 'filepath20', 'filepath92', 'filepath57', 'filepath73',
>   'filepath40', 'filepath44', 'filepath83', 'filepath50', 'filepath66',
>   'filepath94', 'filepath86', 'filepath22', 'filepath17', 'filepath84',
>   'filepath38', 'filepath12', 'filepath53', 'filepath6', 'filepath48',
>   'filepath60', 'filepath51', 'filepath90', 'filepath4', 'filepath78',
>   'filepath69', 'filepath35', 'filepath97', 'filepath93', 'filepath24',
>   'filepath26', 'filepath1', 'filepath3', 'filepath96', 'filepath77',
>   'filepath98', 'filepath18', 'filepath43', 'filepath19', 'filepath56',
>   'filepath65', 'filepath95', 'filepath55', 'filepath7', 'filepath99',
>   'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10',
>   'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74',
>   'filepath21']
>   >>> sorted(myList)
>   ['filepath0', 'filepath1', 'filepath10', 'filepath100', 'filepath11',
>   'filepath12', 'filepath13', 'filepath14', 'filepath15', 'filepath16',
>   'filepath17', 'filepath18', 'filepath19', 'filepath2', 'filepath20',
>   'filepath21', 'filepath22', 'filepath23', 'filepath24', 'filepath25',
>   'filepath26', 'filepath27', 'filepath28', 'filepath29', 'filepath3',
>   'filepath30', 'filepath31', 'filepath32', 'filepath33', 'filepath34',
>   'filepath35', 'filepath36', 'filepath37', 'filepath38', 'filepath39',
>   'filepath4', 'filepath40', 'filepath41', 'filepath42', 'filepath43',
>   'filepath44', 'filepath45', 'filepath46', 'filepath47', 'filepath48',
>   'filepath49', 'filepath5', 'filepath50', 'filepath51', 'filepath52',
>   'filepath53', 'filepath54', 'filepath55', 'filepath56', 'filepath57',
>   'filepath58', 'filepath59', 'filepath6', 'filepath60', 'filepath61',
>   'filepath62', 'filepath63', 'filepath64', 'filepath65', 'filepath66',
>   'filepath67', 'filepath68', 'filepath69', 'filepath7', 'filepath70',
>   'filepath71', 'filepath72', 'filepath73', 'filepath74', 'filepath75',
>   'filepath76', 'filepath77', 'filepath78', 'filepath79', 'filepath8',
>   'filepath80', 'filepath81', 'filepath82', 'filepath83', 'filepath84',
>   'filepath85', 'filepath86', 'filepath87', 'filepath88', 'filepath89',
>   'filepath9', 'filepath90', 'filepath91', 'filepath92', 'filepath93',
>   'filepath94', 'filepath95', 'filepath96', 'filepath97', 'filepath98',
>   'filepath99']
>   >>> ```
>   --
>   áÇ ÃÚÑÝ ãÙáæãÇ ÊæÇØà ÇáäÇÓ Úáí åÖãå æáÇ ÒåÏæÇ Ýí ÅäÕÇÝå
>   ßÇáÍÞíÞÉ.ãÍãÏ ÇáÛÒÇáí
>   "No victim has ever been more repressed and alienated than the truth"
>   Emad Soliman Nawfal
>   Indiana University, Bloomington
>   

>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor


-- 
Bill
-- 
INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186  Skype: jwccsllc (206) 855-5792

An almost hysterical antagonism toward the gold standard is one issue which
unites statists of all persuasions.  They seem to sense that gold and
economic freedom are inseparable.  -- Alan Greenspan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting file paths made up of of strings and numbers

2009-04-14 Thread Kent Johnson
2009/4/14 Emad Nawfal (عماد نوفل) :
> Hi tutors,
> How can I sort the following list in a way that takes care of the right
> order of numbers? The sorted function compares strings here as far as I can
> see, but I want to have filepath2 follow filepath1. Your help is
> appreciated.
 myList
> ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89',
> 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15',


The sort() and sorted() functions have an optional key= parameter. The
parameter should be a function whose argument is a list element and
whose result is the desired key. So for your list, you need a function
that creates an integer from the list item:

In [1]: myList = ['filepath54', 'filepath25', 'filepath49',
'filepath0', 'filepath89', 'filepath52', 'filepath37', 'filepath32', '
filepath2', 'filepath15']

In [2]: def index(fp): return int(fp[8:])

Now sort the list using the key:

In [3]: myList.sort(key=index)

In [4]: myList
Out[4]:
['filepath0',
 'filepath2',
 'filepath15',
 'filepath25',
 'filepath32',
 'filepath37',
 'filepath49',
 'filepath52',
 'filepath54',
 'filepath89']

Note that the key parameter takes the function object itself, there
are no parentheses. i.e. key=index, not key=index().

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting file paths made up of of strings and numbers

2009-04-14 Thread عماد نوفل
On Tue, Apr 14, 2009 at 8:10 PM, wesley chun  wrote:

> > How can I sort the following list in a way that takes care of the right
> > order of numbers? The sorted function compares strings here as far as I
> can
> > see, but I want to have filepath2 follow filepath1. Your help is
> > appreciated.
>  myList
> > ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89',
> > 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15',
> >:
> > 'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10',
> > 'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74',
> > 'filepath21']
>
>
> unfortunately, we're restricted from solving your homework problems
> for you, however, we can tell you that you can still use sorted().
> just combine that with operator.itemgetter(), and you should be well
> on your way to solving your problem!
>
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
>

Thank you so much Wesley for the tip. I'll look into itemgetter()
This is not a homework problem though,  but I'm working on a paper in which
this is useful. So, it is related to school, but is by no means a homework.

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

Emad Soliman Nawfal
Indiana University, Bloomington

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting file paths made up of of strings and numbers

2009-04-14 Thread wesley chun
> How can I sort the following list in a way that takes care of the right
> order of numbers? The sorted function compares strings here as far as I can
> see, but I want to have filepath2 follow filepath1. Your help is
> appreciated.
 myList
> ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89',
> 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15',
>:
> 'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10',
> 'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74',
> 'filepath21']


unfortunately, we're restricted from solving your homework problems
for you, however, we can tell you that you can still use sorted().
just combine that with operator.itemgetter(), and you should be well
on your way to solving your problem!

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sorting file paths made up of of strings and numbers

2009-04-14 Thread عماد نوفل
Hi tutors,
How can I sort the following list in a way that takes care of the right
order of numbers? The sorted function compares strings here as far as I can
see, but I want to have filepath2 follow filepath1. Your help is
appreciated.
>>> myList
['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89',
'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15',
'filepath88', 'filepath70', 'filepath33', 'filepath58', 'filepath28',
'filepath61', 'filepath23', 'filepath64', 'filepath72', 'filepath11',
'filepath63', 'filepath76', 'filepath87', 'filepath45', 'filepath41',
'filepath68', 'filepath81', 'filepath75', 'filepath62', 'filepath46',
'filepath36', 'filepath39', 'filepath5', 'filepath34', 'filepath8',
'filepath67', 'filepath14', 'filepath82', 'filepath29', 'filepath47',
'filepath79', 'filepath13', 'filepath16', 'filepath71', 'filepath27',
'filepath100', 'filepath20', 'filepath92', 'filepath57', 'filepath73',
'filepath40', 'filepath44', 'filepath83', 'filepath50', 'filepath66',
'filepath94', 'filepath86', 'filepath22', 'filepath17', 'filepath84',
'filepath38', 'filepath12', 'filepath53', 'filepath6', 'filepath48',
'filepath60', 'filepath51', 'filepath90', 'filepath4', 'filepath78',
'filepath69', 'filepath35', 'filepath97', 'filepath93', 'filepath24',
'filepath26', 'filepath1', 'filepath3', 'filepath96', 'filepath77',
'filepath98', 'filepath18', 'filepath43', 'filepath19', 'filepath56',
'filepath65', 'filepath95', 'filepath55', 'filepath7', 'filepath99',
'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10',
'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74',
'filepath21']
>>> sorted(myList)
['filepath0', 'filepath1', 'filepath10', 'filepath100', 'filepath11',
'filepath12', 'filepath13', 'filepath14', 'filepath15', 'filepath16',
'filepath17', 'filepath18', 'filepath19', 'filepath2', 'filepath20',
'filepath21', 'filepath22', 'filepath23', 'filepath24', 'filepath25',
'filepath26', 'filepath27', 'filepath28', 'filepath29', 'filepath3',
'filepath30', 'filepath31', 'filepath32', 'filepath33', 'filepath34',
'filepath35', 'filepath36', 'filepath37', 'filepath38', 'filepath39',
'filepath4', 'filepath40', 'filepath41', 'filepath42', 'filepath43',
'filepath44', 'filepath45', 'filepath46', 'filepath47', 'filepath48',
'filepath49', 'filepath5', 'filepath50', 'filepath51', 'filepath52',
'filepath53', 'filepath54', 'filepath55', 'filepath56', 'filepath57',
'filepath58', 'filepath59', 'filepath6', 'filepath60', 'filepath61',
'filepath62', 'filepath63', 'filepath64', 'filepath65', 'filepath66',
'filepath67', 'filepath68', 'filepath69', 'filepath7', 'filepath70',
'filepath71', 'filepath72', 'filepath73', 'filepath74', 'filepath75',
'filepath76', 'filepath77', 'filepath78', 'filepath79', 'filepath8',
'filepath80', 'filepath81', 'filepath82', 'filepath83', 'filepath84',
'filepath85', 'filepath86', 'filepath87', 'filepath88', 'filepath89',
'filepath9', 'filepath90', 'filepath91', 'filepath92', 'filepath93',
'filepath94', 'filepath95', 'filepath96', 'filepath97', 'filepath98',
'filepath99']
>>> ```

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

Emad Soliman Nawfal
Indiana University, Bloomington

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor