检查列表中的字符串部分是否存在于另一个列表中
我有以下列表:
x1 = ['Apples:Red',
'Apples:Green',
'Bananas:Yellow',
'Grapes:Purple',
'Grapes:Green']
x2 = ['Green', 'Yellow']
我想检查列表中的冒号之后的子弦x1
与x2
中的任何字符串匹配。
寻找这样的输出:
['Apples:Green',
'Bananas:Yellow',
'Grapes:Green']
I have the following lists below:
x1 = ['Apples:Red',
'Apples:Green',
'Bananas:Yellow',
'Grapes:Purple',
'Grapes:Green']
x2 = ['Green', 'Yellow']
I would like to check if the substring after the colon in list x1
matches with the any of the strings in x2
.
Looking for an output like this:
['Apples:Green',
'Bananas:Yellow',
'Grapes:Green']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做这样的事情(
RHS
=右侧):You can do something like this (
rhs
= right-hand side):如果
x2
以任何重要的方式很重要,请在进行查找之前将其转换为set
:列表理解应该做:
通过
maxSplit = 1
到str.split
确保仅第一个:
导致拆分。If
x2
is in any way significant, turn it into aset
before doing lookups:A list comprehension should do the rest:
Passing
maxsplit=1
tostr.split
ensures that only the first:
results in a split.