Python 删除列表中的逗号
这是我的节目:Python 删除列表中的逗号,python,python-3.x,Python,Python 3.x,这是我的节目: def Prob2( rows, columns ): for i in range(1, rows+1): print(list(range(i, (columns*i)+1, i))) Prob2( rows = int(input("Enter number of rows here: ")), columns = int(input("Enter number of columns here: "))) 本质上,它接受用户对行和列的输入,并根据这些输入生成以
def Prob2( rows, columns ):
for i in range(1, rows+1):
print(list(range(i, (columns*i)+1, i)))
Prob2( rows = int(input("Enter number of rows here: ")), columns = int(input("Enter number of columns here: ")))
本质上,它接受用户对行和列的输入,并根据这些输入生成以1开头的倍数列表
例如,如果用户键入4行5列,程序将输出如下内容:
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[3, 6, 9, 12, 15]
[4, 8, 12, 16, 20]
我的问题是,我需要去掉逗号,数字之间只有空格。这可能吗?可以将列表转换为字符串,并使用re.sub方法删除逗号
import re
def Prob2(rows, columns):
for i in range(1, rows + 1):
numbers = re.sub(",", "", str(range(i, (columns * i) + 1, i)))
print(numbers)
Prob2(rows=int(input("Enter number of rows here: ")),
columns=int(input("Enter number of columns here: ")))
输出:
[1 2 3 4 5]
[2 4 6 8 10]
[3 6 9 12 15]
[4 8 12 16 20]
可以将列表转换为字符串,并使用re.sub方法删除逗号
import re
def Prob2(rows, columns):
for i in range(1, rows + 1):
numbers = re.sub(",", "", str(range(i, (columns * i) + 1, i)))
print(numbers)
Prob2(rows=int(input("Enter number of rows here: ")),
columns=int(input("Enter number of columns here: ")))
输出:
[1 2 3 4 5]
[2 4 6 8 10]
[3 6 9 12 15]
[4 8 12 16 20]
如标题所示: 删除列表中的逗号 我将给出它的一般版本
>>> l = [1,2,3,4]
>>> l
[1, 2, 3, 4]
>>> s = ' '.join(str(x) for x in l)
>>> s
'1 2 3 4'
这里,由于列表包含int,我们使用列表理解在加入之前将每个个体转换为str
假设列表中包含str,我们可以直接执行以下操作:
>>> l = ['1','2','3','4']
>>> l
['1', '2', '3', '4']
>>> s = ' '.join(l)
>>> s
'1 2 3 4'
如标题所示: 删除列表中的逗号 我将给出它的一般版本
>>> l = [1,2,3,4]
>>> l
[1, 2, 3, 4]
>>> s = ' '.join(str(x) for x in l)
>>> s
'1 2 3 4'
这里,由于列表包含int,我们使用列表理解在加入之前将每个个体转换为str
假设列表中包含str,我们可以直接执行以下操作:
>>> l = ['1','2','3','4']
>>> l
['1', '2', '3', '4']
>>> s = ' '.join(l)
>>> s
'1 2 3 4'
你可以这样做:
def Prob2( rows, columns ):
for i in range(1, rows+1):
print('['+', '.join(map(str, list(range(i, (columns*i)+1, i))))+']')
Prob2( rows = int(input("Enter number of rows here: ")), columns = int(input("Enter number of columns here: ")))
使用“”连接是一种技巧,它允许您将列表转换为字符串,其中mapstr迭代该列表中的每个值,并对其应用str函数。您可以这样做:
def Prob2( rows, columns ):
for i in range(1, rows+1):
print('['+', '.join(map(str, list(range(i, (columns*i)+1, i))))+']')
Prob2( rows = int(input("Enter number of rows here: ")), columns = int(input("Enter number of columns here: ")))
def Prob2( rows, columns ):
for i in range(1, rows+1):
print('['+' '.join(str(val) for val in range(i, (columns*i)+1, i))+']')
Prob2( rows = int(input("Enter number of rows here: ")), columns = int(input("Enter number of columns here: ")))
使用“”连接是一种技巧,它允许您将列表转换为字符串,其中mapstr迭代该列表中的每个值并对其应用str函数
def Prob2( rows, columns ):
for i in range(1, rows+1):
print('['+' '.join(str(val) for val in range(i, (columns*i)+1, i))+']')
Prob2( rows = int(input("Enter number of rows here: ")), columns = int(input("Enter number of columns here: ")))
输出:
[1 2 3 4 5]
[2 4 6 8 10]
[3 6 9 12 15]
[4 8 12 16 20]
输出:
[1 2 3 4 5]
[2 4 6 8 10]
[3 6 9 12 15]
[4 8 12 16 20]
您可以将列表转换为字符串,并在打印前调用str.replace','',''。打印“”。范围i中x的joinstrx,列*i+1,i。这有一百万个副本…您可以将列表转换为字符串,并在打印前调用str.replace','',''。打印“”。范围i中x的joinstrx,列*i+1,这个有一百万个副本…当然,很乐意帮忙当然,很乐意帮忙