从列表中删除具有“特定字符数”的元素。

huangapple go评论75阅读模式
英文:

Elements from a list to be removed that have a 'specific number of characters'

问题

list1 = ['123', '1bc', 'Machine Learning', 'is', 'data science', 'python']
l = ['1', '00:00:05,849 --> 00:00:13,730', 'abdomen', '2', '00:00:13,740 --> 00:00:21,269', 'afghan', '3', '00:00:21,269 --> 00:00:27,989', 'albic', '4', '00:00:27,989 --> 00:00:36,085', 'dug desert']

英文:

I currently have a list such as:

list1 = ['123', '1bc', 'a', 'Machine Learning', '', '7', 'is', '', 'c', 'data science', '', 'python']

How do I change it into a list having elements that has more than 1 character, such as

['123', '1bc', 'Machine Learning', 'is', 'data science', 'python']

that is, it should be removing all the elements that has 1 or less than 1 character.

My actual current list is something like this:

l = ['1', '00:00:05,849 --> 00:00:13,730', 'abdomen', '', '2', '00:00:13,740 --> 00:00:21,269', 'afghan', '', '3', '00:00:21,269 --> 00:00:27,989', 'albic', '', '4', '00:00:27,989 --> 00:00:36,085', 'dug desert']

I want to keep only the elements that have timestamps and its corresponding text. I want to remove everything else. To do so, the easiest way would be to remove the elements with characters 1 or less than 1, right? Because that's what all the remaining elements that are to be removed are.

答案1

得分: 1

使用列表推导与长度

print([el for el in ist1 if len(el) > 1])

示例输出

['123', '1bc', 'Machine Learning', 'is', 'data science', 'python']
英文:

use List Comprehensions: with len

print([el for el in ist1 if len (el) > 1])

sample output

['123', '1bc', 'Machine Learning', 'is', 'data science', 'python']

huangapple
  • 本文由 发表于 2023年2月6日 13:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定