简体   繁体   English

如果不是数字/数字 Python,则删除 len 列表

[英]Remove len list if not Number/Digit Python

I have random list data like this我有这样的随机列表数据

data = 
[['No','Name','Job'],
['1','A','A'],
['2','B','B'],
['IS','NOT','NUMBER'],
['3','C','C']]

as you can see if i print(len(data)) it will print 5如您所见,如果我print(len(data))它将打印5

how i remove data list and decrease len list我如何删除数据列表并减少 len 列表

it will print like this它会像这样打印

print(data)
    ['1','A','A'],
    ['2','B','B'],
    ['3','C','C']

print(len(data))
3

I already try我已经尝试了

for i in range(len(data)):
    if data[i][0].isdigit() == True:
        print(data)

its just show它只是展示

print(data)
    ['1','A','A'],
    ['2','B','B'],
    ['3','C','C']

not decrease len不减少 len

data = [['No','Name','Job'], ['1','A','A'], ['2','B','B'], ['IS','NOT','NUMBER'], ['3','C','C']]

print(len(data))
5

data = [x for x in data if x[0].isdigit()]
print(len(data))
3

print(data)
[['1', 'A', 'A'], ['2', 'B', 'B'], ['3', 'C', 'C']]

Python List comprehension is being used here. Python 列表理解在这里被使用。 Refer to https://www.geeksforgeeks.org/python-list-comprehension-and-slicing/ for more info on this.有关更多信息,请参阅https://www.geeksforgeeks.org/python-list-comprehension-and-slicing/

data = [x for x in data if x[0].isdigit()]
print(len(data))

From the code that you have written, some modifications can be made and can be implemented without using list comprehensions also.从您编写的代码中,可以进行一些修改,并且可以在不使用列表推导的情况下实现。

count =0
for i in data:
    if i[0].isdigit() == True:
        count +=1
        print(i)
print(count)

Hope this answer helps you.希望这个答案对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM