Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert string to each token of a list of strings?

Lets assume I have the following list:

l = ['the quick fox', 'the', 'the quick']

I would like to transform each element of the list into a url as follows:

['<a href="http://url.com/the">the</a>', '<a href="http://url.com/quick">quick</a>','<a href="http://url.com/fox">fox</a>', '<a href="http://url.com/the">the</a>','<a href="http://url.com/the">the</a>', '<a href="http://url.com/quick">quick</a>']

So far, I tried the following:

list_words = ['<a href="http://url.com/{}">{}</a>'.format(a, a) for a in x[0].split(' ')]

The problem is that the above list-comprehension just does the work for the first element of the list:

['<a href="http://url.com/the">the</a>',
 '<a href="http://url.com/quick">quick</a>',
 '<a href="http://url.com/fox">fox</a>']

I also tried with a map but, it didn't work:

[map('<a href="http://url.com/{}">{}</a>'.format(a,a),x) for a in x[0].split(', ')]

Any idea of how to create such links from the tokens of a list of sentences?

like image 943
tumbleweed Avatar asked Jan 05 '23 07:01

tumbleweed


1 Answers

You were close, you limited your comprehension to the contents of x[0].split, i.e you were missing one for loop through the elements of l:

list_words = ['<a href="http://url.com/{}">{}</a>'.format(a,a) for x in l for a in x.split()]

this works because "string".split() yields a one element list.

This can look way prettier if you define the format string outside the comprehension and use a positional index {0} informing format of the argument (so you don't need to do format(a, a)):

fs = '<a href="http://url.com/{0}">{0}</a>'
list_words = [fs.format(a) for x in l for a in x.split()]

With map you can get an ugly little duckling too if you like:

list(map(fs.format, sum(map(str.split, l),[])))

Here we sum(it, []) to flatten the list of lists map with split produces and then map fs.format to the corresponding flattened list. Results are the same:

['<a href="http://url.com/the">the</a>',
 '<a href="http://url.com/quick">quick</a>',
 '<a href="http://url.com/fox">fox</a>',
 '<a href="http://url.com/the">the</a>',
 '<a href="http://url.com/the">the</a>',
 '<a href="http://url.com/quick">quick</a>']

Go with the comprehension, obviously.

like image 188
Dimitris Fasarakis Hilliard Avatar answered Jan 08 '23 05:01

Dimitris Fasarakis Hilliard