homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: reverse() doesn't reverse sort correctly
Type: performance Stage: resolved
Components: Demos and Tools, IDLE, Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: billje, eric.smith, mark.dickinson, terry.reedy
Priority: normal Keywords:

Created on 2012年04月10日 21:22 by billje, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
EX38.PY billje, 2012年04月10日 21:22 Idle file
Messages (7)
msg157988 - (view) Author: Bill Jefferson (billje) Date: 2012年04月10日 21:22
reverse() doesn't reverse sort correctly in Python 25 and 27. sort() works correctly, but reverse doesn't. The following uses reverse(). Incorrect. Even though the date comes first, reverse() sorts on the file name, "B57IBMCD_T.....zip"
2012年04月05日 B57IBMCD_T7.2.3.1.zip
2012年03月23日 B57IBMCD_T7.2.2.3.zip
2012年02月22日 B57IBMCD_T7.2.2.2.zip
2012年02月13日 B57IBMCD_T7.2.2.1.zip
2012年01月23日 B57IBMCD_T7.2.1.1.zip
2012年03月28日 B57IBMCD_T7.0.4.6.zip
2012年03月08日 B57IBMCD_T7.0.4.5.zip
2012年03月03日 B57IBMCD_T7.0.4.4.zip
2012年02月29日 B57IBMCD_T7.0.4.3.zip
2012年01月06日 B57IBMCD_T7.0.4.2.zip
2011年12月08日 B57IBMCD_T7.0.4.1.zip
2012年01月06日 B57IBMCD_T7.0.3.1.zip
2012年01月06日 B57IBMCD_T7.0.2.2.zip
2012年01月06日 B57IBMCD_T7.0.2.1.zip
2012年01月06日 B57IBMCD_T6.2.4.9.zip
2011年12月07日 B57IBMCD_T6.2.4.9-r1.zip
2011年09月13日 B57IBMCD_T6.2.4.8.zip
2012年03月28日 B57IBMCD_T6.2.4.12.zip
2012年02月15日 B57IBMCD_T6.2.4.11.zip
2011年12月06日 B57IBMCD_T6.2.4.10.zip
msg157990 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2012年04月10日 21:45
list.reverse() does not reverse a list, it reverses its current values.
>>> help([].reverse)
Help on built-in function reverse:
reverse(...)
 L.reverse() -- reverse *IN PLACE*
>>>
msg158026 - (view) Author: Bill Jefferson (billje) Date: 2012年04月11日 12:02
Eric.....
Thanks for answering, but I don't understand the difference between "reversing a list" and "reversing it's current values". If I have a list containing elements: A, B, C, D and reverse the list's current values, I get: D, C, B, A, which is the same as reversing the list. Please explain in more detail. Perhaps you can recommend good reference material. Since sort() sorts a list ascending (and works correctly), shouldn't reverse() sort the list descending? Am I using the wrong function? I have attached my program:
Bill.....
#EX38.PY Get listing of all files in a directory
#http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python
# Answer 37
import os, datetime
topDir = "C:/BrcmCDs"
dateFirst = True # put dates before file names
# DateFirst = False # put file names before dates
def modDate(fileN): # returns a 10-place date string
  t = os.path.getmtime(directory+"/"+fileN)
  return str(datetime.datetime.fromtimestamp(t))[0:10]
  
for directory, dirnames, filenames in os.walk(topDir):
  outList = []
  print "Directory: ", directory
  print
  for fileA in filenames:
    if (fileA[0:10] == "B57IBMCD_T" and
      fileA[-4:] == ".zip"):
      dateString = modDate(fileA)
      if dateFirst:
        fileString = dateString + " " + fileA
      else:
        fileString = fileA + " " + dateString
      
      outList.append(fileString)
      
outList.sort() # sort the file list, ascending
# outList.reverse() # reverse the file list, desending
    
print outList #show all files
# now save list to a file:
# https://bbs.archlinux.org/viewtopic.php?id=75839
f = open("fileList.TXT", "w")
f.write("\n".join(map(lambda x: str(x), outList))) 
f.close()
 
Regards from:
William Jefferson Photography
514 Daniels St., #211
Raleigh, NC 27605
Cell Phone: (919) 931-6681
EMail: shaggers3@yahoo.com
Brides & Weddings: http://www.WilliamJeffersonPhotography.com
Special Events: http://www.DancingLight.org
________________________________
 From: Eric V. Smith <report@bugs.python.org>
To: shaggers3@yahoo.com 
Sent: Tuesday, April 10, 2012 5:45 PM
Subject: [issue14542] reverse() doesn't reverse sort correctly
Eric V. Smith <eric@trueblade.com> added the comment:
list.reverse() does not reverse a list, it reverses its current values.
Help on built-in function reverse:
reverse(...)
  L.reverse() -- reverse *IN PLACE*
>>>
----------
nosy: +eric.smith
resolution: -> invalid
stage: -> committed/rejected
status: open -> closed
_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14542>
_______________________________________
msg158029 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012年04月11日 12:28
Bill,
list.reverse doesn't do any *sorting* at all; it merely *reverses* the list contents.
>>> x = [1, 3, 4, 2]
>>> x.reverse()
>>> x
[2, 4, 3, 1]
If you want to do a reverse sort, you can either first sort normally and then reverse the result, or (easier) use the 'reverse' keyword argument to the list.sort method, as follows:
>>> x = [1, 3, 4, 2]
>>> x.sort(reverse=True)
>>> x
[4, 3, 2, 1]
I suspect Eric meant to write "does not reverse sort" instead of "does not reverse".
msg158031 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2012年04月11日 13:30
Thanks, Mark.
Indeed, my answer as written is meaningless. I meant it doesn't sort in reverse, it just reverses.
msg158041 - (view) Author: Bill Jefferson (billje) Date: 2012年04月11日 15:06
Mark and Eric......
Wonderful! I got it now. I used x.sort(reverse=True) and x.sort(reverse=False) and it works just fine. Thanks for your help.
Bill......
 
Regards from:
William Jefferson Photography
514 Daniels St., #211
Raleigh, NC 27605
Cell Phone: (919) 931-6681
EMail: shaggers3@yahoo.com
Brides & Weddings: http://www.WilliamJeffersonPhotography.com
Special Events: http://www.DancingLight.org
________________________________
 From: Mark Dickinson <report@bugs.python.org>
To: shaggers3@yahoo.com 
Sent: Wednesday, April 11, 2012 8:28 AM
Subject: [issue14542] reverse() doesn't reverse sort correctly
Mark Dickinson <dickinsm@gmail.com> added the comment:
Bill,
list.reverse doesn't do any *sorting* at all; it merely *reverses* the list contents.
[2, 4, 3, 1]
If you want to do a reverse sort, you can either first sort normally and then reverse the result, or (easier) use the 'reverse' keyword argument to the list.sort method, as follows:
[4, 3, 2, 1]
I suspect Eric meant to write "does not reverse sort" instead of "does not reverse".
----------
nosy: +mark.dickinson
_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14542>
_______________________________________
msg158214 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012年04月13日 17:32
Bill, when you reply by email, please snip the signature and quoted message. They are just noise. (Exception: quote a line or two if you are specifically responding to such.) Signatures are inappropriate, and the message you are responding to is already present.
History
Date User Action Args
2022年04月11日 14:57:29adminsetgithub: 58747
2012年04月13日 17:32:46terry.reedysetnosy: + terry.reedy
messages: + msg158214
2012年04月11日 15:06:06billjesetmessages: + msg158041
2012年04月11日 13:30:52eric.smithsetmessages: + msg158031
2012年04月11日 12:28:56mark.dickinsonsetnosy: + mark.dickinson
messages: + msg158029
2012年04月11日 12:02:28billjesetmessages: + msg158026
2012年04月10日 21:45:47eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg157990

resolution: not a bug
stage: resolved
2012年04月10日 21:22:28billjecreate

AltStyle によって変換されたページ (->オリジナル) /