目录
Print a list without the commas and brackets in Python
在 Python 中打印不带逗号和括号的列表
要打印不带逗号和括号的列表:
- 使用该
str.join()
方法将列表连接成一个字符串。 - 如果列表包含数字,将它们转换为字符串。
- 使用
print()
函数打印字符串。
list_of_strings = ['bobby', 'hadz', '.com'] # ✅ Print list of strings without commas, brackets and quotes result = ''.join(list_of_strings) print(result) # 👉️ bobbyhadz.com # --------------------------------------------- # ✅ Print list of integers without commas, brackets and quotes 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'] # ✅ Print list of strings without brackets result = ', '.join(list_of_strings) print(result) # 👉️ bobby, hadz, com # --------------------------------------------- # ✅ Print list of numbers without brackets 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
如果可迭代对象中有任何非字符串值,该方法将引发 a。如果您的列表包含数字或其他类型,请在调用之前将所有值转换为字符串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 = ['bobby', 'hadz', '.com'] result = ' '.join(list_of_strings) print(result) # 👉️ bobby hadz .com # --------------------------------------------- list_of_integers = [7, 21, 44] result = ' '.join(str(item) for item in list_of_integers) print(result) # 👉️ 7 21 44
如果您不需要分隔符而只想将列表的元素连接到一个字符串中,请在join()
空字符串上调用该方法。
您还可以使用该map()
函数在调用之前将列表中的所有项目转换为字符串join()
。
list_of_integers = [7, 21, 44] result = ''.join(map(str, list_of_integers)) print(result) # 👉️ 72144
map()函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项目调用该函数。
或者,您可以在函数sep
调用中使用参数print()
。
使用 sep #打印不带逗号和括号的列表
使用sep
参数打印不带逗号和方括号的列表,例如
print(*my_list, sep='')
. 列表中的项目将在print()
函数调用中解包,并且打印时不带逗号、括号和引号。
list_of_strings = ['bobby', 'hadz', '.com'] print(*list_of_strings, sep='') # 👉️ bobbyhadz.com # --------------------------------------------- list_of_integers = [7, 21, 44] print(*list_of_integers, sep='') # 👉️ 72144
*
运算符来解包列表项print()
。*可迭代解包运算符
使我们能够在函数调用、推导式和生成器表达式中解包可迭代对象。
sep
参数是我们传递给 的参数之间的分隔符print()
。
list_of_strings = ['bobby', 'hadz', '.com'] print(*list_of_strings, sep='') # 👉️ bobbyhadz.com print(*list_of_strings, sep=' ') # 👉️ bobby hadz .com print(*list_of_strings, sep='-') # 👉️ bobby-hadz-.com
默认情况下,sep
参数设置为空格。
在 Python 中打印不带括号的列表
要打印不带括号的列表:
- 使用该
str.join()
方法将列表连接成一个字符串。 - 如果列表包含数字,将它们转换为字符串。
- 使用
print()
函数打印字符串。
list_of_strings = ['bobby', 'hadz', 'com'] # ✅ Print list of strings without brackets result = ', '.join(list_of_strings) print(result) # 👉️ bobby, hadz, com # --------------------------------------------- # ✅ Print list of numbers without brackets 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
如果可迭代对象中有任何非字符串值,该方法将引发 a。如果您的列表包含数字或其他类型,请在调用之前将所有值转换为字符串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 = ['bobby', 'hadz', 'com'] result = ' '.join(list_of_strings) print(result) # 👉️ bobby hadz com
如果您不需要分隔符而只想将列表的元素连接到一个字符串中,请在join()
空字符串上调用该方法。
list_of_numbers = [11, 33, 55] result = ''.join(str(item) for item in list_of_numbers) print(result) # 👉️ 113355
您还可以使用该map()
函数在调用之前将列表中的所有项目转换为字符串join()
。
list_of_numbers = [11, 33, 55] result = ', '.join(map(str, list_of_numbers)) print(result) # 👉️ 11, 33, 55
map()函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项目调用该函数。
或者,您可以在函数sep
调用中使用参数print()
。
使用 sep #打印不带括号的列表
使用sep
参数打印不带括号的列表,例如
print(*my_list, sep=', ')
. 列表中的项目将在对的调用中解包,print()
并将使用逗号分隔符打印,不带方括号。
list_of_strings = ['bobby', 'hadz', 'com'] # 👇️ bobby, hadz, com print(*list_of_strings, sep=', ') # ---------------------------------------------- list_of_numbers = [11, 33, 55] # 👇️ 11, 33, 55 print(*list_of_numbers, sep=', ')
*
运算符来解包列表项print()
。*可迭代解包运算符
使我们能够在函数调用、推导式和生成器表达式中解包可迭代对象。
sep
参数是我们传递给 的参数之间的分隔符print()
。
print('bobby', 'hadz', sep='') # 👉️ bobbyhadz print('bobby', 'hadz') # 👉️ bobby hadz
默认情况下,sep
参数设置为空格。
一种不太灵活的方法是将列表转换为字符串并使用字符串切片。
list_of_strings = ['bobby', 'hadz', 'com'] # 👇️ 'bobby', 'hadz', '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
or len(my_str) - 1
。
我们使用一个start
索引1
排除左方括号,并使用一个
stop
索引-1
排除右方括号。