如何在不使用python进行索引的情况下将整数插入列表中? [英] How to insert integers into a list without indexing using python?

查看:98
本文介绍了如何在不使用python进行索引的情况下将整数插入列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将0-9的值插入到列表中而不建立索引.例如,如果我有列表[4, 6, 'X', 9, 0, 1, 5, 7],则需要能够将0-9的整数插入占位符'X'并使用我已经编写的函数对其进行测试.我该如何在不使用insert()函数或任何其他使用索引的函数的情况下进行此操作?

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?

到目前为止,我已经可以使用以下代码在列表中检索'X的位置:

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 = 0
        locked = position
    position = position + 1

所需的输出将是同一列表重复10次,并用10个不同的值(0-9)代替"X":

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]

推荐答案

unknown = list("46X90157")
unknown = ''.join(unknown)
for i in range(10):
    print([int(i) for i in unknown.replace("X", str(i))])

这篇关于如何在不使用python进行索引的情况下将整数插入列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆