在Python 中拆分一个字符串,将其反转并重新连接
Python 中要拆分字符串,将其反转并重新连接:
-
使用
str.split()
方法将字符串拆分为列表。 -
使用
list.reverse()
方法反转列表的元素。 -
使用
str.join()
方法将列表连接成一个字符串。
my_str = 'one two three four'
my_list = my_str.split(' ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
my_list.reverse()
print(my_list) # 👉️ ['four', 'three', 'two', 'one']
my_str_again = ' '.join(my_list)
print(my_str_again) # 👉️ four three two one
第一步是使用 str.split()
方法将字符串拆分为列表。
我们使用空格作为分隔符,但我们可以使用任何其他分隔符(例如逗号或连字符)。
str.split()
方法使用定界符将字符串拆分为子字符串列表。
my_str = 'one two three four'
my_list = my_str.split(' ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
该方法采用以下 2 个参数:
- separator 在每次出现分隔符时将字符串拆分为子字符串
- maxsplit 最多完成 maxsplit 拆分(可选)
下一步是使用 list.reverse()
方法反转列表的元素。
my_str = 'one two three four'
my_list = my_str.split(' ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
my_list.reverse()
print(my_list) # 👉️ ['four', 'three', 'two', 'one']
list.reverse()
方法原地反转列表的元素。
该方法返回 None 因为它改变了原始列表。
如果我们不想就地改变原始列表,请使用步长为 -1 的列表切片语法。
my_str = 'one two three four'
my_list = my_str.split(' ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
reversed_list = my_list[::-1]
print(reversed_list) # 👉️ ['four', 'three', 'two', 'one']
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
我们没有为 start 和 stop 指定值,但为 step 指定 -1 以获得新的反向列表。
最后一步是使用 str.join()
方法将反向列表连接成一个字符串。
my_str = 'one two three four'
my_list = my_str.split(' ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
my_list.reverse()
print(my_list) # 👉️ ['four', 'three', 'two', 'one']
my_str_again = ' '.join(my_list)
print(my_str_again) # 👉️ four three two one
str.join
方法将一个可迭代对象作为参数并返回一个字符串,该字符串是可迭代对象中字符串的串联。
我们使用空格分隔符加入列表,但我们可以使用任何其他分隔符。
my_str = 'one two three four'
my_list = my_str.split(' ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
my_list.reverse()
print(my_list) # 👉️ ['four', 'three', 'two', 'one']
my_str_again = ','.join(my_list)
print(my_str_again) # 👉️ four,three,two,one
如果我们不需要分隔符而只想将列表的元素连接到一个字符串中,请对空字符串调用 join()
方法。
my_str = 'one two three four'
my_list = my_str.split(' ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
my_list.reverse()
print(my_list) # 👉️ ['four', 'three', 'two', 'one']
my_str_again = ''.join(my_list)
print(my_str_again) # 👉️ fourthreetwoone
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。