将对象插入到特定列表索引

编程入门 行业动态 更新时间:2024-10-24 15:17:40
本文介绍了将对象插入到特定列表索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在Python中的特定位置将对象添加到列表中?这就是我的意思:

How can I add an object to a list at a specific spot in Python? This is what I mean:

我想添加字符串'String2'到列表中的 x 位置,但是不存在 x 位置.如何创建现货 x ?

I want to add the string 'String2' to spot x in a list, but spot x doesn't exist. How can I create spot x?

我希望它像这样:

list = ['String1'] x = 4 list.create(x) list[x] = 'String2' print(list)

输出:

['String1', None, None, 'String2']

推荐答案

您正在寻找的是稀疏列表,可以在任意索引处输入值.这个问题具有一些可能的解决方案,例如自定义稀疏列表实现或使用dict.

What you're looking for is a sparse list, where values can be entered at arbitrary indices. This question has some possible solutions, like a custom sparse list implementation or using a dict.

更多推荐

将对象插入到特定列表索引

本文发布于:2023-10-07 17:32:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   对象   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!