How to insert integers into a list without indexing using python?

2024/10/14 21:21:59

I am trying to insert values 0 - 9 into a list without indexing. For example if I have the list [4, 6, 'X', 9, 0, 1, 5, 7] I need to be able to insert the integers 0 - 9 into the placeholder 'X' and test it with a function I have already written. How would I go about doing this without using the insert() function or any other function that uses indexing?

So far I have been able to retrieve the position of 'X in the list using the code below:

unknown = list("46X90157")
known = []
position = 0
for item in unknown:known.append(item)if item == 'X':locked = 0locked = positionposition = position + 1

The desired output would be the same list repeated 10 times with the 10 different values (0-9) in place of 'X':

[4, 6, 0, 9, 0, 1, 5, 7], [4, 6, 1, 9, 0, 1, 5, 7], [4, 6, 2, 9, 0, 1, 5, 7], [4, 6, 3, 9, 0, 1, 5, 7], [4, 6, 4, 9, 0, 1, 5, 7], [4, 6, 5, 9, 0, 1, 5, 7], [4, 6, 6, 9, 0, 1, 5, 7], [4, 6, 6, 9, 0, 1, 5, 7], [4, 6, 7, 9, 0, 1, 5, 7], [4, 6, 8, 9, 0, 1, 5, 7], [4, 6, 9, 9, 0, 1, 5, 7]
Answer
unknown = list("46X90157")
unknown = ''.join(unknown)
for i in range(10):print([int(i) for i in unknown.replace("X", str(i))])
https://en.xdnf.cn/q/117911.html

Related Q&A

Separate/reposition/translate shapes in image with pillow in python

I need to separate or translate or replace pixels in an image with python so as to all the shapes to share the same distance between each other and the limits of the canvas.background is white, shapes …

django.db.utils.OperationalError: (1045, Access denied for user user@localhost

I cant get my Django project to load my database correctly. It throws this error. Im running MariaDB with Django, and I uninstalled all MySQL I added the user by running:create database foo_db; create …

Paho Python Client with HiveMQ

i am developing a module in python that will allow me to connect my raspberry pi to a version of hivemq hosted on my pc.it connects normally but when i add hivemqs file auth plugin it doesnt seem to wo…

Comparison of value items in a dictionary and counting matches

Im using Python 2.7. Im trying to compare the value items in a dictionary.I have two problems. First is the iteration of values in a dictionary with a length of 1. I always get an error, because python…

how to send cookies inside post request

trying to send Post request with the cookies on my pc from get request #! /usr/bin/python import re #regex import urllib import urllib2 #get request x = urllib2.urlopen("http://www.example.com) #…

Flask-Uploads gives AttributeError?

from flask import Flask from flask.ext.uploads import UploadSet, configure_uploads, IMAGESapp = Flask(__name__)app.config[UPLOADED_PHOTOS_DEST] = /home/kevin photos = UploadSet(photos, IMAGES)configure…

Python: Alternate way to covert from base64 string to opencv

Im trying to convert this string in base64 (http://pastebin.com/uz4ta0RL) to something usable by OpenCV. Currently I am using this code (img_temp is the string) to use the OpenCV functions by convertin…

Move 3D plot to avoid clipping by margins

Im trying to figure out how I can get the 3D matplotlib images below to plot higher on the canvas so it doesnt get clipped. Here is the code Im using to create the plot. I couldnt find a way to attach …

HTML Link parsing using BeautifulSoup

here is my Python code which Im using to extract the Specific HTML from the Page links Im sending as parameter. Im using BeautifulSoup. This code works fine for sometimes and sometimes it is getting st…

XML format change using XSL file in a Python code

Have written a Python code to transform a XML file to a particular format using XSL stylesheet. Python code below:#!/usr/bin/env python # -*- coding:utf-8 -*- from lxml import etree def transform(xmlP…