ml_student ml_student - 27 days ago 5x
Python Question

How to inserting string to each tokens 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="">the</a>', '<a href="">quick</a>','<a href="">fox</a>', '<a href="">the</a>','<a href="">the</a>', '<a href="">quick</a>']

So far, I tried the following:

list_words = ['<a href="{}">{}</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="">the</a>',
'<a href="">quick</a>',
'<a href="">fox</a>']

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

[map('<a href="{}">{}</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?


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="{}">{}</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="{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="">the</a>',
 '<a href="">quick</a>',
 '<a href="">fox</a>',
 '<a href="">the</a>',
 '<a href="">the</a>',
 '<a href="">quick</a>']

Go with the comprehension, obviously.