我试图让一个多维数组工作,其中用户字符串填充在单元格中。 我一直在寻找在多维数组中更新用户值的方法
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
输入
^{pr2}$我得到的电流输出
This is updated array>>> [['s', 's', 's'], ['s', 's', 's'], ['s', 's', 's']]
我在找什么
This is updated array>>> [['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]
<>空白可以填入*
在字符串。替换不会起作用,因为它不会影响原始值。在
相反,您需要通过索引访问列表:
^{pr2}$如果您提供一个更精确的问题,并将您想要实现的输出添加到问题中,我可以给您一个更精确的答案。我放弃了以前的解决方案,因为它不是你想要的
这个小技巧使用整数除法:
以及模运算符(https://en.wikipedia.org/wiki/Modulo_operation):
这应该行得通,只要你在矩阵中移动你的字符串,你只需要知道你在以前的迭代中使用了多少个字符
编辑
你也可以这样做
^{pr2}$相关问题 更多 >
编程相关推荐