python删除list
1. 请问python如何删除list某两列以及数据数据类型
def str2num(s):
try:
return int(s)
except:
return None
lista = [
['0624', 'b', 'nan', 'nan', 'nan','nan', '1', 'true', '2', '0', '1', 'ture', '2', '0'],
['0625', 'b', 'nan','nan', 'nan', 'nan', '2', ' ture', '1', '0', '2', 'ture', '2', '0'],
['0626', 'b','nan', 'nan', 'nan', 'nan', '3', ' ture', '1', '0', '3', 'ture', '1', '0'],
['0627','b', 'nan', 'nan', 'nan', 'nan', '4', ' ture ', '1', '0', 'nan', 'nan', 'nan','nan']
]
# 要求:将第3、7、11列中具有最大数的列作为新列表listb的第3列,lista中其他列分别依次存入listb中的第1、2、4...
listb = []
for item in lista:
item11 = str2num(item.pop(10))
item7 = str2num(item.pop(6))
item3 = str2num(item.pop(2))
newitem3 = str(max([item3,item7,item11]))
item.insert(2, newitem3)
listb.append(item)
2. python如何删除list里重复的元素
代码如下:
oldList=['a','b','c','d','a','a']
newList=[]
forletterinoldList:
ifletternotinnewList:
newList.append(letter)
printnewList
运行结果内:容
['a','b','c','d']
3. python 列表删除元素问题
python 列表随机删除来一个元自素,代码如下:
import random
li = [1,2,3,4]
li.remove(random.choice(li)) #随机删除列表li中的任意一个元素
print(li) #结果肯定不是none.
4. python 移除list里的元素
def remove_section(alist,start,end):
if start > len(alist):
# 开始位置越界返回原串
return alist[:]
elif end > len(alist):
# 结束位置越界
return alist[:start]
else:
a = alist[:start]
a.extend(alist[end:])
return a
5. Python:如果想要删除list中的list中的元素该怎么做呢
List1=[
['Apple','Google','Microsoft'],
['Java','Python','Ruby','PHP'],
['Adam','Bart','Lisa']
]
foriteminList1:
ifitem.count("Python"):
item.pop(item.index("Python"))
printList1
6. python3 从一个字符串list中删除另一个字符串list中的所有元素
l1=['a','b','c']
l2=['a','b']
#计算两个集合的差集
print(list(set(a).difference(set(b))))
#['c']
7. python list怎么删除元素
有两个方法
1.pop()
默认删除最后一个元素。
也可以给定一个索引值删除索引值对应的元素。
8. Python 中如何删除一个列表 List 中多个符合条件的元素
l3=range(10)
l2=range(5)
l3=[i for i in l3 if i not in l2]
运行结果:
Python 2.7.12 (default, Nov 22 2016, 17:23:24)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "right", "credits" or "license" for more information.
>>> l3=range(10)
>>> l3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l2=range(5)
>>> l2
[0, 1, 2, 3, 4]
>>> l3=[i for i in l3 if i not in l2]
>>> l3
[5, 6, 7, 8, 9]
9. python list 怎么去除空的list
假设你要处理的列表是lst,代码如下:
L=[xforxinlstifx]
L就是你要的
10. python中的列表(List)如何一次删除多个数
a=[1,2,3,4,5]
dela[2:4]
print(a)