【发布时间】:2021-07-22 21:02:03
【问题描述】:
我想在 python 中找到另一个列表值的第一个列表值,其中我使用了match 和 set 和 findall 其他方法,但它在我的代码中效果不佳。
这里我想匹配pattern 在longlist 中并且模式总是以开头。
pattern = ['aaa','bbbb','ccc']
longlist = ['aaa845','bbbbPP44','XXX10','aaa420','ccc1','jjj7000','PPPP']
for i in longlist: # longlist is json data, So this line required. After we can do it any code
if filter(pattern.match, i):
print(i)
# other tried
if set(pattern) & set(longlist):
print(i)
期望输出:aaa845, bbbbPP44, aaa420, ccc1
谁能建议最好的编码风格,无需多次FOR 循环迭代和最快的性能?
【问题讨论】:
-
您希望
pattern中的字符串包含在longlist的字符串中,还是您希望longlist中的字符串以pattern 中的任何一个开头?对于当前示例,两者都给出相同的结果。
标签: python json string function