Python - 选择性连续后缀连接
当必须查找选择性连续后缀连接时,可以使用简单的迭代、“endswith”方法和“append”方法。
示例
下面是相应演示
my_list = ["Python-", "fun", "to-", "code"]
print("The list is :")
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("The result is :")
print(result)输出
The list is : ['Python-', 'fun', 'to-', 'code'] The suffix is : - The result is : ['Python-fun', 'to-code']
说明
- 定义了一个字符串列表并将其显示在控制台上。
- 定义了一个后缀值并将其显示在控制台上。
- 创建两个空列表。
- 对列表进行迭代,并将元素追加到空列表中。
- 如果元素未以特定后缀结尾,则使用“join”方法将其追加到空列表中。
- 再次清空另一个列表。
- 将其作为输出显示在控制台上。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP