将字符串连接到python中列表的所有元素的末尾 [英] Concatenate string to the end of all elements of a list in python

查看:29
本文介绍了将字符串连接到python中列表的所有元素的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将字符串连接到列表中所有元素的末尾.

I would like to know how to concatenate a string to the end of all elements in a list.

例如:

List1 = [ 1 , 2 , 3 ]
string = "a"

output = ['1a' , '2a' , '3a']

推荐答案

以列表理解方式重建列表,并在两个参数上均使用 str.format

rebuild the list in a list comprehension and use str.format on both parameters

>>> string="a"
>>> List1 = [ 1 , 2 , 3 ]
>>> output = ["{}{}".format(i,string) for i in List1]
>>> output
['1a', '2a', '3a']

这篇关于将字符串连接到python中列表的所有元素的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆