mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
mismatch between example and first footnote in #unique-items
This commit is contained in:
@@ -142,7 +142,7 @@ if __name__ == '__main__':
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>set(''.join(words))</kbd> <span class=u>④</span></a>
|
||||
<samp class=pp>{'E', 'D', 'M', 'O', 'N', 'S', 'R', 'Y'}</samp></pre>
|
||||
<ol>
|
||||
<li>Given a list of several strings, the <code>set()</code> function will return a set of unique strings from the list. This makes sense if you think of it like a <code>for</code> loop. Take the first item from the list, put it in the set. Second. Third. Fourth — wait, that’s in the set already, so it only gets listed once, because Python sets don’t allow duplicates. Fifth. Sixth — again, a duplicate, so it only gets listed once. The end result? All the unique items in the original list, without any duplicates. The original list doesn’t even need to be sorted first.
|
||||
<li>Given a list of several strings, the <code>set()</code> function will return a set of unique strings from the list. This makes sense if you think of it like a <code>for</code> loop. Take the first item from the list, put it in the set. Second. Third. Fourth. Fifth — wait, that’s in the set already, so it only gets listed once, because Python sets don’t allow duplicates. Sixth. Seventh — again, a duplicate, so it only gets listed once. The end result? All the unique items in the original list, without any duplicates. The original list doesn’t even need to be sorted first.
|
||||
<li>The same technique works with strings, since a string is just a sequence of characters.
|
||||
<li>Given a list of strings, <code>''.join(<var>a_list</var>)</code> concatenates all the strings together into one.
|
||||
<li>So, given a list of strings, this line of code returns all the unique characters across all the strings, with no duplicates.
|
||||
|
||||
Reference in New Issue
Block a user