【发布时间】:2016-08-07 23:16:33
【问题描述】:
我正在尝试使多维数组正常工作,其中用户字符串填充在单元格中。 我一直在寻找更新多维数组中用户值的方法
def createMultiArray(self,usrstrng,Itmval):
#creates a multidimensional array, where usrstrng=user input, Itmval=width
ArrayMulti=[[" " for x in range(Itmval)]for x in range(Itmval)]
# need to update user values, therefore accessing index to update values.
for row in ArrayMulti:
for index in range(len(row)):
for Usrchr in usrstrng:
row[index]= Usrchr
print "This is updated array>>>",ArrayMulti
输入
funs
我得到的当前输出
This is updated array>>> [['s', 's', 's'], ['s', 's', 's'], ['s', 's', 's']]
我在找什么
This is updated array>>> [['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]
可以用*填空
【问题讨论】:
-
你为什么要遍历
usrstrng来替换空格字符? -
@James 我试图用字符串的每个字符替换单元格的每个值
-
但是一旦你替换,字符串中将不再有空格,因此后续对
replace的调用将无济于事。 -
@dbliss 我已经更新了当前问题中的原始代码
标签: python python-2.7 python-3.x