Replacing values in a list [closed]

2024/11/15 15:18:24

Basically, I'm trying to create a program that will replace the values of diceList with a random integer between 1 and 6 wherever my indexList has a 1 in it. This is what I have so far:

import random
def replaceValues(diceList, indexList):newList = diceListfor i in indexList:if indexList == 1:newList[i] = random.randint(1,6)return newList

I'm executing replaceValues([1,2,3,4,5], [0,1,0,1,0]) and what I should get is [1,x,3,x,5] where x should be a random number between 1 and 6. The problem is it currently returns [1,2,3,4,5]

Answer

Is indexList a list? If so, what does a list == 1 mean?

if indexList == 1:newList[i] = random.randint(1,6)

Did you perhaps mean i == 1 ?

Edit:

I think you're looking for something like this:

def replaceValues(diceList, indexList):newList = diceListif len(diceList) == len(indexList):for pos in range(0, len(indexList)):if indexList[pos] == 1:newList[pos] = random.randint(0,6)return newList
https://en.xdnf.cn/q/119401.html

Related Q&A

Azure Release Pipeline - Environment variables on python script

Lately Ive been requested to run a python script on my Azure Release Pipeline. This script needs some environment variables for being executed, as Ive seen that in the build pipeline, the task include …

Problem with python prepared stmt parameter passing

File C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor.py, line 1149, in execute elif len(self._prepared[parameters]) != len(params): TypeError: object of ty…

list of lists to list of tuples without loops or list comprehensions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How can I merge CSV rows that have the same value in the first cell?

This is the file: https://drive.google.com/file/d/0B5v-nJeoVouHc25wTGdqaDV1WW8/view?usp=sharingAs you can see, there are duplicates in the first column, but if I were to combine the duplicate rows, no…

i usually get this error : ValueError: invalid literal for int() with base 10

I have loaded a csv file and as i try to print it i get this error Traceback (most recent call last):File "C:\Users\FSTC\Downloads\spaceproject\main.py", line 389, in <module>world_data…

How to Draw a triangle shape in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

DataFrame from list of string dicts with array() values

So I have a list where each entry looks something like this: "{A: array([1]), B: array([2]), C: array([3])}"I am trying to get a dataframe that looks like thisA B C 0 1 2 3 1 4 …

Need Help Making Buttons to perform for loops when you input a number

I am trying to make a button in Maya using Python that when you type in a number the for loop would loop for that many times. For example, I would put 5 in the box so the for loop would loop 5 times re…

Combining multiple conditional expressions in a list comprehension

I utf-8 encode characters like \u2013 before inserting them into SQLite.When I pull them out with a SELECT, they are back in their unencoded form, so I need to re-encode them if I want to do anything w…

Arduino Live Serial Plotting with a MatplotlibAnimation gets slow

I am making a live plotter to show the analog changes from an Arduino Sensor. The Arduino prints a value to the serial with a Baudrate of 9600. The Python code looks as following: import matplotlib.pyp…