python删除列表元素

1.需求

num = [1,2,2,2,3,4,2,2,2,2,2,2,22,2]
把列表中的有2的元素全部删除

2.编程代码

nums = [1,2,2,2,3,4,2,2,2,2,2,2,22,2]print("打印删除前的元素:")print(nums)temp = []for i in nums:    if i !=2 and "2" is not str(i):        temp.append(i)print("打印删除后的元素:")print(temp)

3.编程结果



原文地址:https://www.cnblogs.com/yunlongaimeng/p/8647500.html

时间: 2024-08-29 03:08:07

python删除列表元素的相关文章

Python在迭代器中删除列表元素

在迭代器中删除列表元素是非常危险的,因为迭代器是直接对列表的数据进行引用 把列表拷贝给迭代器,然后对原列表进行删除操作就没问题了 pos=turtle.move() for each_fish in fish[:]: if each_fish.move()==pos: #鱼儿被吃掉 turtle.eat() fish.remove(each_fish) print('有一条鱼被吃') Python的List的底层是实现是一个PyObject*数组.如果每次增加一个元素都扩张内存的话效率太低,在增

python——删除列表中的元素

在python中,删除列表元素的方法有三种,分别为remove(),del(),pop()函数 (1)remove() >>> name = ['小明','小华','小红','小李','小霞','小文'] >>> name.remove('小红') >>> name ['小明', '小华', '小李', '小霞', '小文'] remove()函数里面的参数必须是列表中已有的元素值. (2)del() >>> name = ['小明'

python删除列表中得重复得数据

解决思想:将列表转换为 集合,利用集合删除重复数据得特性删除重复数据,然后将集合转换为列表 #删除列表中得重复元素 def delect_1 (lt): s = set(lt) lt = list(s) print(lt)delect_1([1,2,3,4,1,3,4,5]) 原文地址:https://www.cnblogs.com/chaojiyingxiong/p/9174791.html

Python当中list列表的使用(创建列表,删除列表元素,添加列表元素,插入列表元素)

程序如下: #这里我们将进行列表的学习,这个列表不能和R当中的列表弄混了 classmate=['bob','Python','Java'] b=["wife","mother"] print(len(classmate)) #下面开始进行元素的访问,python当中的首元素是0,而不是R当中的1. print(classmate[0]) print(classmate[0:]) print(classmate[1:2]) print("print the

python中列表元素连接方法join用法

创建列表: >>> music = ["Abba","Rolling Stones","Black Sabbath","Metallica"] >>> print music 输出: ['Abba', 'Rolling Stones', 'Black Sabbath', 'Metallica'] 通过join函数通过空格连接列表中的元素: >>> print ' '.joi

python 删除列表中重复的数字

方法一:将列表转化成集合,再转化成列表 Li = [1,1,2,2,3,3,4,4] print(list(set(Li))) 方法二:创建一个新列表,遍历列表是否重复,不重复插入新列表 def UniqueInt(Lists): temp_li = [] for i in Lists: if i not in temp_li: temp_li.append(i) return temp_li Li = [1,1,2,2,3,3,4,4] print(UniqueInt(Li)) 原文地址:ht

python 删除元组元素

#create a tuple tuplex = "w", "j" ,"c", "e" print(tuplex) #tuples are immutable, so you can not remove elements #using merge of tuples with the + operator you can remove an item and it will create a new tuple tuplex

python 删除字典元素

myDict = {'a':1,'b':2,'c':3,'d':4} print(myDict) if 'a' in myDict: del myDict['a'] print(myDict) 原文地址:https://www.cnblogs.com/sea-stream/p/9985497.html

Python中列表元素排列组合

1 # 排列 2 from itertools import product 3 l = [1, 2, 3] 4 print(list(product(l, l))) 5 print(list(product(l, repeat=4))) 6 7 # 组合 8 from itertools import combinations 9 print(list(combinations([1,2,3,4,5], 3))) 原文地址:https://www.cnblogs.com/hahasd/p/12