mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
mention lstrip() and strip()
This commit is contained in:
@@ -295,7 +295,7 @@ StopIteration</samp>
|
||||
'Chris', 'Ethan', 'Sarah', 'Lizzie', 'Wesley']</samp></pre>
|
||||
<ol>
|
||||
<li>This idiom returns a list of the lines in a text file.
|
||||
<li>Unfortunately (for this example), the <code>list(open(<var>filename</var>))</code> idiom also includes the carriage returns at the end of each line. This list comprehension uses the <code>rstrip()</code> string method to strip trailing whitespace from each line.
|
||||
<li>Unfortunately (for this example), the <code>list(open(<var>filename</var>))</code> idiom also includes the carriage returns at the end of each line. This list comprehension uses the <code>rstrip()</code> string method to strip trailing whitespace from each line. (Strings also have an <code>lstrip()</code> method to strip leading whitespace, and a <code>strip()</code> method which strips both.)
|
||||
<li>The <code>sorted()</code> function takes a list and returns it sorted. By default, it sorts alphabetically.
|
||||
<li>But the <code>sorted()</code> function can also take a function as the <var>key</var> parameter, and it sorts by that key. In this case, the sort function is <code>len()</code>, so it sorts by <code>len(<var>each item</var>)</code>. Shorter names come first, then longer, then longest.
|
||||
</ol>
|
||||
|
||||
Reference in New Issue
Block a user