Python 中将逗号分隔的字符串转换为列表
使用 str.split()
方法将逗号分隔的字符串转换为列表,例如 my_list = my_str.split(',')
。 str.split()
方法将在每次出现逗号时拆分字符串,并返回包含结果的列表。
my_str = 'one,two,three,four'
# ✅ convert comma-separated string to list
my_list = my_str.split(',')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
# ----------------------------------
my_str = '1,2,3,4'
# ✅ convert comma-separated string to list of integers
my_list = [int(item) if item.isdigit() else item for item in my_str.split(',')]
print(my_list) # 👉️ [1, 2, 3, 4]
# ----------------------------------
# 👇️ If your string has leading or trailing commas
my_str = ',one,two,three,four,'
my_list = [item for item in my_str.split(',') if item]
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
我们使用 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 拆分(可选)
当没有分隔符传递给
str.split()
方法时,它会将输入字符串拆分为一个或多个空白字符。
如果在字符串中找不到分隔符,则返回仅包含 1 个元素的列表。
print('one'.split(',')) # 👉️ ['one']
如果我们的字符串具有不同的定界符,请务必调整分隔符。
# 👇️ comma and space delimiter
my_str = 'one, two, three, four'
my_list = my_str.split(', ')
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
将逗号分隔的字符串转换为整数列表:
-
使用
str.split()
方法在每次出现逗号时拆分字符串。 - 使用列表理解来迭代字符串列表。
-
使用
int()
类将每个字符串转换为整数。
my_str = '1,2,3,4'
my_list = [int(item) if item.isdigit() else item for item in my_str.split(',')]
print(my_list) # 👉️ [1, 2, 3, 4]
我们使用 str.split
方法在每次出现逗号时拆分字符串,并使用列表理解来遍历列表。
列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。
在每次迭代中,我们检查当前列表项是否为数字,并使用 int()
类将匹配的字符串转换为整数。
如果字符串中的所有字符都是数字并且至少有 1 个字符,则 str.isdigit
方法返回 True,否则返回 False。
我们不必检查是否确定该字符串只包含逗号分隔的数字。
my_str = '1,2,3,4'
my_list = [int(item) for item in my_str.split(',')]
print(my_list) # 👉️ [1, 2, 3, 4]
如果我们的字符串包含前导或尾随逗号,请使用列表推导从列表中排除空字符串元素。
my_str = ',one,two,three,four,'
my_list = [item for item in my_str.split(',') if item]
print(my_list) # 👉️ ['one', 'two', 'three', 'four']
示例中的字符串有一个前导和尾随逗号,因此按逗号拆分将返回空字符串元素。
my_str = ',one,two,three,four,'
# 👇️ ['', 'one', 'two', 'three', 'four', '']
print(my_str.split(','))
我们可以使用列表推导从列表中排除空字符串。
相关文章
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 日期时间。