Python string from list comprehension
Question:
I am trying to get this string as a result:
"&markers=97,64&markers=45,84"
From the Python code below:
markers = [(97,64),(45,84)]
result = ("&markers=%s" %x for x in markers)
return result
How do I do this as the below does not give me the actual string?
Answers:
You need to join your string like this:
markers = [(97,64),(45,84)]
result = ''.join("&markers=%s" % ','.join(map(str, x)) for x in markers)
return result
UPDATE
I didn’t initially have the ','.join(map(str, x))
section in there to turn each tuple into strings. This handles varying length tuples, but if you will always have exactly 2 numbers, you might see gatto’s comment below.
The explanation of what’s going on is that we make a list with one item for each tuple from markers, turning the tuples into comma separated strings which we format into the &markers=
string. This list of strings is then joined together separated by an empty string.
While the first answer is doing what’s expected, I’d make it a bit more “pythonic” by getting rid of map
and nested expressions:
def join(seq, sep=','):
return sep.join(str(i) for i in seq)
result = ''.join('&markers=%s' % join(m) for m in markers)
(if that’s for urls like it seems, you can also take a look at urllib.urlencode
)
Here’s another approach that hopefully makes the intent the most clear by specifying the location of each of your values explicitly:
markers = [(97,64),(45,84)]
print ''.join('&markers=%s,%s' % pair for pair in markers)
Try creating an empty string adding to it then removing the last comma
result = ”
for i in a:
result+='&markers'
for j in i:
result += str(j) + ','
result = result[:len(result)-1]
return result
In Python 3.6 you could write:
markers = [(97,64),(45,84)]
result = ''.join(f'&markers={pair}' for pair in markers)
return result
I am trying to get this string as a result:
"&markers=97,64&markers=45,84"
From the Python code below:
markers = [(97,64),(45,84)]
result = ("&markers=%s" %x for x in markers)
return result
How do I do this as the below does not give me the actual string?
You need to join your string like this:
markers = [(97,64),(45,84)]
result = ''.join("&markers=%s" % ','.join(map(str, x)) for x in markers)
return result
UPDATE
I didn’t initially have the ','.join(map(str, x))
section in there to turn each tuple into strings. This handles varying length tuples, but if you will always have exactly 2 numbers, you might see gatto’s comment below.
The explanation of what’s going on is that we make a list with one item for each tuple from markers, turning the tuples into comma separated strings which we format into the &markers=
string. This list of strings is then joined together separated by an empty string.
While the first answer is doing what’s expected, I’d make it a bit more “pythonic” by getting rid of map
and nested expressions:
def join(seq, sep=','):
return sep.join(str(i) for i in seq)
result = ''.join('&markers=%s' % join(m) for m in markers)
(if that’s for urls like it seems, you can also take a look at urllib.urlencode
)
Here’s another approach that hopefully makes the intent the most clear by specifying the location of each of your values explicitly:
markers = [(97,64),(45,84)]
print ''.join('&markers=%s,%s' % pair for pair in markers)
Try creating an empty string adding to it then removing the last comma
result = ”
for i in a:
result+='&markers'
for j in i:
result += str(j) + ','
result = result[:len(result)-1]
return result
In Python 3.6 you could write:
markers = [(97,64),(45,84)]
result = ''.join(f'&markers={pair}' for pair in markers)
return result