Python:满足特定条件时从列表创建新列表 [英] Python: Create a new list from a list when a certain condition is met

查看:79
本文介绍了Python:满足特定条件时从列表创建新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个单词列表中创建一个新列表;当单词的某个条件得到满足时.在这种情况下,我想将长度为9的所有单词添加到新列表中.

I want to make a new list from another list of words; when a certain condition of the word is met. In this case I want to add all words that have the length of 9 to a new list.

我用过:

resultReal = [y for y in resultVital if not len(y) < 4]

删除所有长度小于4的条目.但是,我现在不想删除这些条目.我想用这些单词创建一个新列表,但将其保留在旧列表中.

to remove all entries that are under the length of 4. However, I do not want to remove the entries now. I want to create a new list with the words, but keeping them in the old list.

也许是这样的:

if len(word) == 9:
     newlist.append()

谢谢.

推荐答案

抱歉,意识到您想要的长度为9,而不是9或更大.

Sorry, realized you wanted length, 9, not length 9 or greater.

newlist = [word for word in words if len(word) == 9]

这篇关于Python:满足特定条件时从列表创建新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆