How to replace values in multidimensional array?

2024/7/7 5:31:50

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]= Usrchrprint "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 *

Answer

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] = charreturn 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

https://en.xdnf.cn/q/120355.html

Related Q&A

How to deploy a python docker image to AWS Lambda?

I am trying to figure out how to deploy a flask application that I have received with a Dockerfile to AWS Lambda.In local, all I have to do to start the app is to enter docker-compose up. Thats work gr…

How to isolate titles from these image URLs?

I have a list of image urls contained in images. I am trying to isolate the title from these image urls so that I can display, on the html, the image (using the whole url) and the corresponding title. …

Columns and rows concatenation with a commun value in another column

In the below mentioned table, I want to concatenate the columns Tri_gram_sents and Value together and then all rows which has the same number in column sentence.Tri_gram_sents Value …

Python Indentation Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Allow form submission only once a day django

I want to allow users to submit a django form once, and only once everyday. After submitting the form, the form wouldnt even show (server-side checkings, I dont want to use JS or client side thing; eas…

Counting percentage of element occurence from an attribute in a class. Python

I have a class called transaction that have these attributes Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type, location])an example of the data set is as sucht…

AWS | Syntax error in module: invalid syntax

I have created python script which is uploaded as a zip file in AWS Lambda function with stompy libraries bundled in them.Logs for python 2.7:-Response: nullRequest ID: "c334839f-ee46-11e8-8970-61…

Algorithm for finding if an array is balanced [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Merging two dataframes in python pandas [duplicate]

This question already has answers here:Pandas Merging 101(8 answers)Closed 5 years ago.I have a dataframe A:a 1 a 2 b 1 b 2Another dataframe B:a 3 a 4 b 3I want my result dataframe to be like a 1 3 a …

Searching for the best fit price for multiple customers [duplicate]

This question already has an answer here:Comparing multiple price options for many customers algorithmically(1 answer)Closed 10 years ago.A restatement of Comparing multiple price options for many cust…