如何清除python中的List? [英] how to clear up a List in python?

查看:81
本文介绍了如何清除python中的List?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新的列表,当它有大量的值时,我想要

删除其中的所有值,该怎么办?

然后,如果一个列表有801个值,我想把它的值索引从300

到400,可以用list1 [300:400],对我好吗?

解决方案

>我有一个新的列表,当它有大量的值时,我想要

删除它里面的所有值,该怎么办?


这样的事情可能会有所帮助。


x = [1,2,3,4,5,6,7,8,9 ]

y = x


list([x.pop()for x in xrange(len(x))])

打印x,y#[] []

并且,如果列表有801个值,我想将其值从300
转到400,可以使用list1 [300:400],对我好吗?




300将包含在您的切片中,而第400个索引将被排除在外。你的片中最终会有99件物品。如果你想要从索引300到400的
值,你需要说[300:401]。包含第一个

指数。最后一个索引被排除在外。


祝你好运!


python写道:

我有一个新的列表,当它有大量值时,我想要删除其中的所有值,该怎么做?
并且,如果一个列表有801个值,我想让它的值从300
到400,可以使用list1 [300:400],对我好吗?



del list1 [:]

del list1 [: - 1000]#keep max。列表中最后1000个附加项目

list1 [:] = replace_list

....


-robert


> del list1 [:]


谢谢你的回复。我从来没有想过[:]因为我是我

以为它会立即复制一份清单或者如果有的话b / b
它会删除一份副本所以我永远不会玩它。很好:)

del list1 [: - 1000]#keep max。列表中的最后1000个附加项目
list1 [:] = replace_list




非常感谢提示。他们很棒!


I have new a list , when it hava large number of values, I wonna to
delete all the values in it,how to do?
And, if a list have 801 values, I want to get its values index from 300
to 400, could use list1[300:400],are right me?

解决方案

> I have new a list , when it hava large number of values, I wonna to

delete all the values in it,how to do?
something like this will probably help.

x = [1,2,3,4,5,6,7,8,9]
y = x

list([x.pop() for z in xrange(len(x))])

print x, y # [] []
And, if a list have 801 values, I want to get its values index from 300
to 400, could use list1[300:400],are right me?



300 will be included in your slice whereas the 400th index will be
excluded. you will ultimately have 99 items in your slice. if you want
values from index 300 to 400 you''ll need to say [300:401]. the first
index is included. the last index is excluded.

good luck!


python wrote:

I have new a list , when it hava large number of values, I wonna to
delete all the values in it,how to do?
And, if a list have 801 values, I want to get its values index from 300
to 400, could use list1[300:400],are right me?


del list1[:]
del list1[:-1000] # keep max. last 1000 appended items in the list
list1[:]=replace_list
....

-robert


> del list1[:]

thank you for that reply. I never thought of [:] cause to be me I
thought it would immediately make a copy of the list or if anything
that it would delete a copy so I never played with it. nice :)

del list1[:-1000] # keep max. last 1000 appended items in the list
list1[:]=replace_list



very much thanks for the tips. they''re great!


这篇关于如何清除python中的List?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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