我有两张单子
list_one = ['aaa1a', 'bbb21ba', 'ccc4ba', 'qqq55ca']
list_two = ['eee21a', 'sws21ba', 'pop4ba', 'qqq55de']
最后一个数字后面的字母代表一个区域;我已经可以用regex找到这个区域了
regex = re.compile(r"[^[0-9]+$")
reg_list = []
for i in list_one:
reg_list.append(regex.findall(i))
这会给
reg_list = [u'a', u'ba', u'ba', u'ca']
我想搜索list\u two以检查它的任何项是否与我的注册表中的任何项匹配,如果匹配,请将其从该列表中删除。所以我会以
list_two = ['qqq55de']
因为“de”是唯一不在列表中的位置。我现在的代码是
for i in list_one:
for j in list_two:
find_location = regex.findall(j)
if a == find_location:
list_two.pop(j)
但我得到了错误
TypeError: expected string or buffer
有没有更好的方法?你知道吗
假设您已经获得了
reg_list
,现在可以使用filter
:我不喜欢在看不懂的时候把所有的东西都衬起来。只需考虑最具可读性(当然也是最有效的)解决方案。你知道吗
在迭代列表时不能修改列表。但是你可以创建一个新的
结果:
您可以使用列表理解作为一种更简短的替代方法:
输出:
相关问题 更多 >
编程相关推荐