英文:
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']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论