[英]How to print a specific element of a list in python by searching if a word is in that element?
[英]How to print a python list, minus a specific element?
鉴于:
commands = dict(
onboarding = dict(
default = "default",
status = "APPROVED",
date = "11/18/15"
)
team = dict(
members = getTeamMembers()
)
我如何
print commands['onboarding'].keys()
没有在列表中打印键“默认”? 我试图将其隐藏在我的实现中。 如果两个版本的python中都有解释,则要加分。
谢谢!
print [key for key in commands['onboarding'] if key != 'default']
如果您from __future__ import print_function
,则可以在以下两个版本上使用以下功能:
d=commands['onboarding']
print(*[d[i] for i in d if i != 'default'])
您可以在其中使用所有适用的键构建列表,然后在print
通话中解包。 当然,如果您在输出中需要列表,请不要解压缩(即,从列表理解的开头删除*
)。
您也可以像Python 2.7那样使用filter
print filter(lambda key: key != 'default', commands['onboarding'])
或在Python 3中这样
print(list(filter(lambda key: key != 'default', commands['onboarding'])))
在Python 3中, filter
返回一个可迭代的filter
对象,而不是Python 2中的列表,因此您必须将其包装在list()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.