列出插入新项目时的行为 [英] List Behavior when inserting new items

查看:70
本文介绍了列出插入新项目时的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在项目列表中添加一个元素,但是我想

将它添加到大于当前大小的特定索引处:


list = [1,2,3]

list.insert(10,4)


我想要什么看来是这样的:


[1,2,3 ,,,,,, 4]


但是我看到了:


[1,2,3,4]


有没有办法轻易产生这种行为?


谢谢,

画了

I''m looking to add an element to list of items, however I''d like to
add it at a specific index greater than the current size:

list = [1,2,3]
list.insert(10,4)

What I''d like to see is something like:

[1,2,3,,,,,,4]

However I see:

[1,2,3,4]

Is there any way to produce this kind of behavior easily?

Thanks,
Drew

推荐答案

Drew schrieb:
Drew schrieb:

我想在项目列表中添加一个元素,但是我想在一个大于当前大小的特定索引处添加



list = [1,2,3]

list.insert(10,4)


什么我希望看到类似的东西:


[1,2,3 ,,,,,, 4]


但是我明白了:


[1,2,3,4]


有吗任何方式容易产生这种行为?
I''m looking to add an element to list of items, however I''d like to
add it at a specific index greater than the current size:

list = [1,2,3]
list.insert(10,4)

What I''d like to see is something like:

[1,2,3,,,,,,4]

However I see:

[1,2,3,4]

Is there any way to produce this kind of behavior easily?



使用字典?


如果知道列表最终会有多大,那每个

位置已填充,您也可以使用range(n)创建列表。


您的实际用例是什么?


diez

Use a dictionary?

If you know how large the list eventually will be, and that each
position is filled, you can also use range(n) to create a list.

What is your actual usecase?

diez


你的实际用例是什么?
What is your actual usecase?

>

diez
>
diez



问题是我不喜欢不知道列表最终会持续多长时间。

基本上我正在尝试使用2D列表来保存我将最终打印到屏幕上的行b
。列表中的空白元素将以空格打印。

我想每次添加一个元素,我都可以找到

列表大小和所需索引之间的差异,并且

填写&b之间的范围。 "价值,但我只想

看看语言是否有更自然的方式。


谢谢,

德鲁

The issue is that I don''t know how long the list will eventually be.
Essentially I''m trying to use a 2D list to hold lines that I will
eventually print to the screen. Blank elements in the list will be
printed as spaces. I suppose every time I add an element, I could find
the difference between the size of the list and the desired index and
fill in the range between with " " values, however I just wanted to
see if there was a more natural way in the language.

Thanks,
Drew


1月29日下午7:57,Drew < olso ... @ gmail.comwrote:
On Jan 29, 7:57 pm, "Drew" <olso...@gmail.comwrote:

我想在项目列表中添加一个元素,但是我想

将其添加到大于当前大小的特定索引:


list = [1,2,3]

list.insert (10,4)


我想看到的是:


[1,2,3 ,,, ,,, 4]


但是我明白了:


[1,2,3,4]


有没有办法轻易产生这种行为?


谢谢,

画了
I''m looking to add an element to list of items, however I''d like to
add it at a specific index greater than the current size:

list = [1,2,3]
list.insert(10,4)

What I''d like to see is something like:

[1,2,3,,,,,,4]

However I see:

[1,2,3,4]

Is there any way to produce this kind of behavior easily?

Thanks,
Drew



您可以编写自己的类来模拟具有所需

行为的列表,例如:


py> class llist(object):

py def __init __(self,arg = []):

py self .__ list = arg

py def __setitem __(self,key,值):

py length = len(self .__ list)

py if length< key:

py for i in range(length,key +1):

py self .__ list.append(None)

py self .__ list [key] = value

py def __str __(self):

py return str(self .__ list)

py>

py> x = llist()

py> x [10] = 1

py>打印x

[无,无,无,无,无,无,无,无,无,无,1]


其他方法添加到llist类请参阅 http://docs.python.org/

ref / sequence-types.html

You could write your own class mimicing a list with your desired
behaviour, something like:

py>class llist(object):
py def __init__(self, arg = []):
py self.__list = arg
py def __setitem__(self, key, value):
py length = len(self.__list)
py if length < key:
py for i in range(length, key +1):
py self.__list.append(None)
py self.__list[key] = value
py def __str__(self):
py return str(self.__list)
py>
py>x = llist()
py>x[10] = 1
py>print x
[None, None, None, None, None, None, None, None, None, None, 1]

for other methods to add to the llist class see http://docs.python.org/
ref/sequence-types.html


这篇关于列出插入新项目时的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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