从 Python 中的列表中删除方括号
使用 str.join()
方法从列表中删除方括号,例如 result = ', '.join(str(item) for item in my_list)
。 str.join()
方法会将列表连接成一个不带方括号的字符串。
# ✅ remove square brackets from list of strings
list_of_strings = ['www', 'jiyik', 'com']
result = ', '.join(list_of_strings)
print(result) # 👉️ www, jiyik, com
# ---------------------------------------------
# ✅ remove square brackets from list of integers
list_of_numbers = [44, 22, 66]
result = ', '.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 44, 22, 66
我们使用 str.join()
方法从列表中删除方括号。
str.join()
方法将一个可迭代对象作为参数并返回一个字符串,该字符串是可迭代对象中字符串的串联。
请注意
,如果可迭代对象中有任何非字符串值,则该方法会引发TypeError
。
如果我们的列表包含数字或其他类型,请在调用 join()
之前将所有值转换为字符串。
list_of_numbers = [44, 22, 66]
result = ', '.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 44, 22, 66
我们使用生成器表达式来迭代列表。
生成器表达式用于对每个元素执行一些操作或选择满足条件的元素子集。
在每次迭代中,我们使用 str()
类将数字转换为字符串。
调用 join()
方法的字符串用作元素之间的分隔符。
list_of_numbers = [44, 22, 66]
result = ' '.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 44 22 66
如果我们不需要分隔符而只想将列表的元素连接成一个字符串,请在空字符串上调用 join()
方法。
list_of_numbers = [44, 22, 66]
result = ''.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 442266
我们还可以在调用 join()
之前使用 map()
函数将列表中的所有项目转换为字符串。
list_of_integers = [44, 22, 66]
result = ''.join(map(str, list_of_integers))
print(result) # 👉️ 442266
map()
函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项调用该函数。
如果我们需要删除方括号以展平列表,请使用列表推导。
my_list = [['www'], ['jiyik'], ['.', 'com']]
flat_list = [x for xs in my_list for x in xs]
print(flat_list) # 👉️ ['www', 'jiyik', '.', 'com']
或者,我们可以使用字符串切片。
从 Python 中的列表中删除方括号
要从列表中删除方括号:
-
使用
str()
类将列表转换为字符串。 - 使用字符串切片从字符串中排除第一个和最后一个字符。
- 该字符串不包含方括号。
list_of_strings = ['www', 'jiyik', 'com']
result = str(list_of_strings)[1:-1]
print(result) # 👉️ 'www', 'jiyik', 'com'
# ---------------------------------------------
list_of_integers = [44, 22, 66]
result = str(list_of_integers)[1:-1]
print(result) # 👉️ 44, 22, 66
我们使用
str()
类将列表转换为字符串,并使用字符串切片排除方括号。
看下面的代码
list_of_strings = ['www', 'jiyik', 'com']
# "['www', 'jiyik', 'com']"
print(str(list_of_strings))
字符串切片的语法是 my_str[start:stop:step]
。
开始索引是包含的,而停止索引是排除的(最多,但不包括)。
Python 索引是从零开始的,因此字符串中的第一个字符的索引为 0,最后一个字符的索引为 -1 或 len(my_str) - 1
。
我们使用 1 的 start 索引来排除左方括号,并使用 -1 的 stop 索引来排除右方括号。
我们也可以使用 str.replace()
方法来获得相同的结果。
list_of_strings = ['www', 'jiyik', 'com']
result = str(list_of_strings).replace('[', '').replace(']', '')
print(result) # 👉️ 'www', 'jiyik', 'com'
如果我们需要从列表中的每个项目中删除方括号,请使用 str.replace()
方法。
my_list = ['[www]', '[jiyik]', '[com]']
result = [item.replace('[', '').replace(']', '') for item in my_list]
print(result) # 👉️ ['www', 'jiyik', 'com']
str.replace()
方法返回字符串的副本,其中所有出现的子字符串都被提供的替换替换。
该方法采用以下参数:
- old 字符串中我们要替换的子字符串
- new 每次出现 old 的替换
- count 仅替换第一个 count 事件(可选)
该方法不会更改原始字符串。 字符串在 Python 中是不可变的。
相关文章
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 日期时间。