如何在python中创建x字符列表?

如何在python中创建x字符列表?,python,list,python-3.x,Python,List,Python 3.x,我正在尝试在每个索引处写一个“.”的列表,其次数与我的游戏板中应该有行和列的次数相同。这是我正在使用的代码,但是我得到的错误告诉我,我不能将序列乘以类型为'str'的非int。我正在使用python 3.5.2 def mkBoard(rows,columns): board = ["."] * rows * columns return board #Take user input for number of rows and columns for the board an

我正在尝试在每个索引处写一个“.”的列表,其次数与我的游戏板中应该有行和列的次数相同。这是我正在使用的代码,但是我得到的错误告诉我,我不能将序列乘以类型为'str'的非int。我正在使用python 3.5.2

def mkBoard(rows,columns):
    board = ["."] * rows * columns
    return board

#Take user input for number of rows and columns for the board and converts them into integers
rows = input('Enter the number of rows:')
int(rows)
columns = input('Enter the number of columns:')
int(columns)

#Create the board
board = mkBoard(rows,columns)
你很接近

您只需要将新的int值分配给变量
int()
不更改变量,只返回一个新值

因此,要获得正确的行值,请使用:

rows = int(rows)
列也是如此

另外,你还应该看看你是如何生成董事会的。对于2x2板,您可能需要类似[[”、“]、[”、“]]的东西。我建议你看看列表的理解力,你已经很接近了

您只需要将新的int值分配给变量
int()
不更改变量,只返回一个新值

因此,要获得正确的行值,请使用:

rows = int(rows)
列也是如此


另外,你还应该看看你是如何生成董事会的。对于2x2板,您可能需要类似[[”、“]、[”、“]]的东西。我建议您查看列表理解

您的代码中还有另一个问题,其他答案没有解决

>>> ['.'] * 5 * 5
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']
电路板初始化创建一个平面列表,这可能不是您想要的。通常,您会为此使用列表列表。但是,使用重载乘法创建列表是一种非常糟糕的方法:

>>> [['.'] * 5] * 5
[['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
因为现在:

>>> board = _
>>> board[0][0] = 'hi'
>>> board
[['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.']]
每一行都是同一列表的副本。您应该更喜欢列表理解:

>>> board = [['.' for row in range(5)] for col in range(5)]
>>> board
[['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
>>> board[0][0] = 'hi'
>>> board
[['hi', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]

您的代码中还有另一个问题,而另一个答案没有解决

>>> ['.'] * 5 * 5
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']
电路板初始化创建一个平面列表,这可能不是您想要的。通常,您会为此使用列表列表。但是,使用重载乘法创建列表是一种非常糟糕的方法:

>>> [['.'] * 5] * 5
[['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
因为现在:

>>> board = _
>>> board[0][0] = 'hi'
>>> board
[['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.'], ['hi', '.', '.', '.', '.']]
每一行都是同一列表的副本。您应该更喜欢列表理解:

>>> board = [['.' for row in range(5)] for col in range(5)]
>>> board
[['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
>>> board[0][0] = 'hi'
>>> board
[['hi', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]

转换为整数,但不对新值执行任何操作将
转换为整数,但不对新值执行任何操作