Last active
December 23, 2015 01:49
-
-
Save keum/6563342 to your computer and use it in GitHub Desktop.
Python - list comprehension and string formatting procedure to take a list and add specific string to all the list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Question One: | |
Why does the join method not add ".cso" to "king" | |
""" | |
a=('seattle','chelan','king') | |
print ".cso ".join(a) # returns "seattle.cso chelan.cso king" | |
""" | |
Answer: | |
The .join() method is used here to combine multiple elements, e.g. | |
the three in your tuple, into a single string element, joined | |
together by whatever you specify (in your case, '.cso'). Since you | |
have three elements in your tuple, the join method only needs to | |
insert two '.cso' entries. | |
.join() is more commonly used to create comma (or any other type of | |
delimited) strings from a list or tuple. | |
""" | |
a=('seattle','chelan','king') | |
a_joined_string = ','.join(a) | |
print a_joined_string # returns 'seattle,chelan,king' | |
len(a_joined_string) # returns 19, the number of characters in the string | |
""" | |
Question Two: Why does wrapping a join() call within a list constructor | |
create a list of length 1? | |
""" | |
a=['seattle','chelan','harbor'] | |
b=[".cso ".join(a)] # returns ['seattlecso. chelancso. harbor'] | |
""" | |
Answer: Looking at lines 21-24, you can see that when you call the join | |
method on a list, you're saying something like. "Create a single string | |
that contains every element in this list, but seperate(join) each element | |
with a specific delimiter." | |
So when you wrap that call to join() within a list constructor (the brackets), | |
you're saying something like, "Create a single string using join(), but wrap that | |
single string in a list". | |
What you know you really want however, is to ask | |
something like, "Create a new list containing individual strings from each element in | |
list a, while adding '.cso' to the end of each element from a". | |
List comprehension is a great way to do that. | |
""" | |
a=['seattle','chelan','harbor'] | |
b = ["%s.cso" % city for city in a] # list comprehension | |
print b # returns ['seattle.cso', 'chelan.cso', 'harbor.cso'] This is what you want? | |
""" | |
Line 52 introduces two concepts here, list comprehension and string formatting. | |
Let's talk about the string formatting first. | |
As opposed to using the join() method which creates a single string, combining | |
all elements in a list, we use string formatting to modify an individual string, | |
adding '.cso' to the end. Here are the docs on that syntax: | |
http://docs.python.org/2/library/stdtypes.html#string-formatting-operations | |
Secondly is the list comprehension syntax. You're basically creatting a small | |
for-loop inside a list construction. notice in line 52 the part 'city for city in a'? | |
You're saying, "for each city, in list a, add a copy to me (list b). | |
So wrapping those two things together you get this phrase that is like 'add the suffix | |
.cso to a copy of each city from list a, then add it to me (list b) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment