本文介绍了对Python列表中的每个元素执行字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python字符串列表- elements .我想在 elements 中编辑每个 element .请参见下面的代码(虽然不起作用,但是您会明白的):

I have a list of strings in Python - elements. I would like to edit each element in elements. See the code below (it doesn't work, but you'll get the idea):

for element in elements:
    element = "%" + element + "%"

有没有办法做到这一点?

Is there a way to do this?

推荐答案

elements = ['%{0}%'.format(element) for element in elements]

这篇关于对Python列表中的每个元素执行字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 17:19