在 Python 中打印没有逗号和括号的列表
Python 中要打印不带逗号和括号的列表:
-
使用
str.join()
方法将列表连接成一个字符串。 - 如果列表包含数字,将它们转换为字符串。
-
使用
print()
函数打印字符串。
list_of_strings = ['fql', 'jiyik', '.com']
# ✅ 打印不带逗号、括号和引号的字符串列表
result = ''.join(list_of_strings)
print(result) # 👉️ fqljiyik.com
# ---------------------------------------------
# ✅ 打印不带逗号、括号和引号的整数列表
list_of_integers = [7, 21, 44]
result = ''.join(str(item) for item in list_of_integers)
print(result) # 👉️ 72144
# ---------------------------------------------
list_of_strings = ['bobby', 'hadz', 'com']
# ✅ 打印不带括号的字符串列表
result = ', '.join(list_of_strings)
print(result) # 👉️ bobby, hadz, com
# ---------------------------------------------
# ✅ 打印不带括号的数字列表
list_of_numbers = [11, 33, 55]
result = ', '.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 11, 33, 55
我们使用 str.join()
方法打印一个没有逗号、括号和引号的列表。
str.join
方法将一个可迭代对象作为参数并返回一个字符串,该字符串是可迭代对象中字符串的串联。
请注意
,如果可迭代对象中有任何非字符串值,该方法会引发TypeError
。
如果我们的列表包含数字或其他类型,请在调用 join()
之前将所有值转换为字符串。
list_of_integers = [7, 21, 44]
result = ''.join(str(item) for item in list_of_integers)
print(result) # 👉️ 72144
我们使用生成器表达式来遍历列表。
生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。
在每次迭代中,我们使用 str()
类将数字转换为字符串。
调用 join()
方法的字符串用作元素之间的分隔符。
list_of_strings = ['fql', 'jiyik', '.com']
result = ' '.join(list_of_strings)
print(result) # 👉️ fql jiyik .com
# ---------------------------------------------
list_of_integers = [7, 21, 44]
result = ' '.join(str(item) for item in list_of_integers)
print(result) # 👉️ 7 21 44
如果我们不需要分隔符而只想将列表的元素连接到一个字符串中,请对空字符串调用 join()
方法。
我们还可以在调用 join()
之前使用 map()
函数将列表中的所有项目转换为字符串。
list_of_integers = [7, 21, 44]
result = ''.join(map(str, list_of_integers))
print(result) # 👉️ 72144
map()
函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项目调用该函数。
或者,我们可以在调用 print()
函数时使用 sep 参数。
使用 sep 打印不带逗号和括号的列表
使用 sep
参数打印不带逗号和方括号的列表,例如 print(*my_list, sep='')
。 列表中的项目将在对 print()
函数的调用中解包,并且将在不带逗号、括号和引号的情况下打印。
list_of_strings = ['fql', 'jiyik', '.com']
print(*list_of_strings, sep='') # 👉️ fqljiyik.com
# ---------------------------------------------
list_of_integers = [7, 21, 44]
print(*list_of_integers, sep='') # 👉️ 72144
请注意
,我们在调用print()
时使用了可迭代解包*
运算符来解包列表项。
*
可迭代解包运算符使我们能够在函数调用、推导式和生成器表达式中解包可迭代对象。
sep 参数是我们传递给 print()
的参数之间的分隔符。
list_of_strings = ['fql', 'jiyik', '.com']
print(*list_of_strings, sep='') # 👉️ fqljiyik.com
print(*list_of_strings, sep=' ') # 👉️ fql jiyik .com
print(*list_of_strings, sep='-') # 👉️ fql-jiyik-.com
默认情况下,sep 参数设置为空格。
在 Python 中打印不带括号的列表
要打印不带括号的列表:
-
使用
str.join()
方法将列表连接成一个字符串。 - 如果列表包含数字,将它们转换为字符串。
-
使用
print()
函数打印字符串。
list_of_strings = ['bobby', 'hadz', 'com']
# ✅ 打印不带括号的字符串列表
result = ', '.join(list_of_strings)
print(result) # 👉️ bobby, hadz, com
# ---------------------------------------------
# ✅ 打印不带括号的数字列表
list_of_numbers = [11, 33, 55]
result = ', '.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 11, 33, 55
我们使用 str.join()
方法来打印没有方括号的列表。
str.join
方法将一个可迭代对象作为参数并返回一个字符串,该字符串是可迭代对象中字符串的串联。
请注意
,如果可迭代对象中有任何非字符串值,该方法会引发TypeError
。
如果我们的列表包含数字或其他类型,请在调用 join()
之前将所有值转换为字符串。
list_of_numbers = [11, 33, 55]
result = ', '.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 11, 33, 55
我们使用生成器表达式来遍历列表。
生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。
在每次迭代中,我们使用 str()
函数将数字转换为字符串。
调用 join()
方法的字符串用作元素之间的分隔符。
list_of_strings = ['fql', 'jiyik', 'com']
result = ' '.join(list_of_strings)
print(result) # 👉️ fql jiyik com
如果我们不需要分隔符而只想将列表的元素连接到一个字符串中,请对空字符串调用 join()
方法。
list_of_numbers = [11, 33, 55]
result = ''.join(str(item) for item in list_of_numbers)
print(result) # 👉️ 113355
我们还可以在调用 join()
之前使用 map()
函数将列表中的所有项目转换为字符串。
list_of_numbers = [11, 33, 55]
result = ', '.join(map(str, list_of_numbers))
print(result) # 👉️ 11, 33, 55
map()
函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项目调用该函数。
或者,我们可以在调用 print()
函数时使用 sep 参数。
使用 sep 打印不带括号的列表
使用 sep 参数打印不带括号的列表,例如 print(*my_list, sep=', ')
。 列表中的项目将在调用 print()
时解包,并使用逗号分隔符打印,不带方括号。
list_of_strings = ['fql', 'jiyik', 'com']
# 👇️ fql, jiyik, com
print(*list_of_strings, sep=', ')
# ----------------------------------------------
list_of_numbers = [11, 33, 55]
# 👇️ 11, 33, 55
print(*list_of_numbers, sep=', ')
请注意
,我们在调用print()
时使用了可迭代解包*
运算符来解包列表项。
*
可迭代解包运算符使我们能够在函数调用、推导式和生成器表达式中解包可迭代对象。
sep 参数是我们传递给 print()
的参数之间的分隔符。
print('fql', 'jiyik', sep='') # 👉️ fqljiyik
print('fql', 'jiyik') # 👉️ fql jiyik
默认情况下,sep 参数设置为空格。
一种不太灵活的方法是将列表转换为字符串并使用字符串切片。
list_of_strings = ['fql', 'jiyik', 'com']
# 👇️ 'fql', 'jiyik', 'com'
print(str(list_of_strings)[1:-1])
# ---------------------------------------------
list_of_numbers = [11, 33, 55]
# 👇️ 11, 33, 55
print(str(list_of_numbers)[1:-1])
我们使用 str()
类将列表转换为字符串,并使用字符串切片来排除方括号。
字符串切片的语法是 my_str[start:stop:step]
。
start
索引是包含性的,而stop
索引是排他性的(直到,但不包括)。
Python 索引是从零开始的,因此字符串中的第一个字符的索引为 0,最后一个字符的索引为 -1 或 len(my_str) - 1
。
我们使用 1 的 start 索引来排除左方括号,并使用 -1 的 stop 索引来排除右方括号。
相关文章
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 日期时间。