如何替换多维数组中的值? [英] How to replace values in multidimensional array?
问题描述
我正在尝试使多维数组工作,其中用户字符串填充在单元格中.我一直在寻找更新多维数组中用户值的方法
def createMultiArray(self,usrstrng,Itmval):#创建一个多维数组,其中usrstrng=用户输入,Itmval=widthArrayMulti=[[" " for x in range(Itmval)]for x in range(Itmval)]# 需要更新用户值,因此访问索引来更新值.对于 ArrayMulti 中的行:对于范围内的索引(len(行)):对于 usrstrng 中的 Usrchr:行[索引]= Usrchrprint "这是更新的数组>>>",ArrayMulti
输入
乐趣
我得到的当前输出
这是更新的数组>>>[['s', 's', 's'], ['s', 's', 's'], ['s', 's', 's']]
我在找什么
这是更新的数组>>>[['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]
空格可以用*填
string.replace 不起作用,因为它不影响原始值.
<预><代码>>>>测试 = "你好">>>test.replace("a", " ")'你好'>>>测试'你好'相反,您需要通过索引访问列表:
用于 ArrayMulti 中的行:对于范围内的索引(len(行)):行[索引] = "a"
如果您提供更精确的问题并将您想要达到的输出添加到问题中,我可以给您更精确的答案.
我废弃了之前的解决方案,因为它不是您想要的
def UserMultiArray(usrstrng, Itmval):ArrayMulti=[[" " for x in range(Itmval)] for x in range(Itmval)]对于索引,枚举中的字符(usrstrng):ArrayMulti[index//Itmval][index%Itmval] = char返回 ArrayMulti>>>stack.UserMultiArray("乐趣", 3)[['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]
这个小技巧使用整数除法:
<块引用>[0, 1 ,2 ,3 ,4]//3 -> 0, 0, 0, 1, 1
和模运算符(https://en.wikipedia.org/wiki/Modulo_operation):
<块引用>[0, 1 ,2 ,3 ,4] % 3 -> 0, 1, 2, 0, 1
I am trying to get a multidimensional array working, where the user string is filled in the cell. I have been searching for ways to update user values in the multidimensional array
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
Input
funs
current output that i am getting
This is updated array>>> [['s', 's', 's'], ['s', 's', 's'], ['s', 's', 's']]
what i am looking for
This is updated array>>> [['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]
the blank can be filled in with *
string.replace won't work, since it does not affect the original value.
>>> test = "hallo"
>>> test.replace("a", " ")
'h llo'
>>> test
'hallo'
Instead you need to access the list via the indexes:
for row in ArrayMulti:
for index in range(len(row)):
row[index] = "a"
If you provide a more precise question and add the output you want to achieve to the question, I can give you a more precise answer.
I scrapped the previous solution since it wasn't what you wanted
def UserMultiArray(usrstrng, Itmval):
ArrayMulti=[[" " for x in range(Itmval)] for x in range(Itmval)]
for index, char in enumerate(usrstrng):
ArrayMulti[index//Itmval][index%Itmval] = char
return ArrayMulti
>>> stack.UserMultiArray("funs", 3)
[['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]
This little trick uses the whole-number-division:
[0, 1 ,2 ,3 ,4] // 3 -> 0, 0, 0, 1, 1
and the modulo operator(https://en.wikipedia.org/wiki/Modulo_operation):
[0, 1 ,2 ,3 ,4] % 3 -> 0, 1, 2, 0, 1
这篇关于如何替换多维数组中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!