讓我們假設我有以下列表:如何將字符串插入字符串列表的每個標記?
l = ['the quick fox', 'the', 'the quick']
我想列表中的每個元素轉變成一個網址如下:
['<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>']
到目前爲止,我試過如下:
list_words = ['<a href="http://url.com/{}">{}</a>'.format(a, a) for a in x[0].split(' ')]
問題是,上面的列表理解只是做了列表的第一個元素的工作:
['<a href="http://url.com/the">the</a>',
'<a href="http://url.com/quick">quick</a>',
'<a href="http://url.com/fox">fox</a>']
我也試圖與一個map
但是,它沒有工作:
[map('<a href="http://url.com/{}">{}</a>'.format(a,a),x) for a in x[0].split(', ')]
如何創建句子列表的令牌,鏈接任何想法?
感謝您的幫助傢伙! – tumbleweed