从python中选择dict_keys的所需元素时问题
我有一个字典键['a_report1','a_report2',...,'a_report10','b_report1','b_report2',...,'b_report10',]
。我想从此dict_keys提取所有“ report1”。换句话说,我只能获得'a_report1'
和'b_report1'
。
这是我尝试过的代码:
[report for report in list(dictionary.keys()) if 'report1' in report]
问题:它将返回'a_report10'
和'b_report10'
,我也只想report1。无论如何要解决这个问题?
I have a list of dictionary keys ['A_report1', 'A_report2', ..., 'A_report10','B_report1', 'B_report2', ..., 'B_report10',]
. I want to extract all 'report1' from this dict_keys. In other words, I should only get 'A_report1'
and 'B_report1'
.
Here's the code I tried:
[report for report in list(dictionary.keys()) if 'report1' in report]
Issue: It'll return 'A_report10'
and 'B_report10'
as well, I only want report1's. Anyway to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用正则表达式如何?
How about using a regular expression?
这是一个简单但不舒适的解决方案,基于以下假设:您的所有报告名称都遵循格式
。
here is a simple but non-robust solution based on the assumption that all your report names follow the format
.*report<any int number>
: