Python - 选择性连续后缀连接
pythonserver side programmingprogramming更新于 2025/11/1 15:40:17
当需要找到选择性连续后缀连接时,可以使用简单的迭代、‘endswith’方法和‘append’方法。
示例
下面是相同的演示
my_list = ["Python-", "fun", "to-", "code"]
print("列表为:")
print(my_list)
suffix = '-'
print("The suffix is :")
print(suffix)
result = []
temp = []
for element in my_list:
temp.append(element)
if not element.endswith(suffix):
result.append(''.join(temp))
temp = []
print("结果为:")
print(result)
输出
列表为: ['Python-', 'fun', 'to-', 'code'] The suffix is : - 结果为: ['Python-fun', 'to-code']
解释
- 定义一个字符串列表并显示在控制台上。
- 定义一个后缀值并显示在控制台上。
- 创建两个空列表。
- 迭代列表,并将元素附加到空列表中。
- 如果元素不以特定后缀结尾,则使用‘join’方法将其附加到空列表中。
- 另一个列表再次清空。
- 这在控制台上显示为输出。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

