[英]How to remove a row from a list of lists if one of the words in it does not contain certain characters in Python?
I have a list of lists that contains lexicons. 我有一个包含词典的列表列表。 I want to delete lists from the list if they have lexicons that do not contain a pattern of characters such as "(atè|atwa|atif)$".
如果列表中的词典不包含“(atè| atwa | atif)$”等字符模式,我想从列表中删除它们。 For example :
例如 :
list = [['blablatè', 'blabla'], ['klak'], ['matwa', 'mat'], ['ma', 'mat'], ['ratif']]
The expected result would be : 预期结果将是:
[['blablatè', 'blabla'], ['matwa', 'mat'], ['ratif']]
I thought I could make it with a loop and re.search(), but it doesn't work. 我以为可以通过循环和re.search()来做到这一点,但这是行不通的。 Here's my code :
这是我的代码:
import re
result = [['blablatè', 'blabla'], ['klak'], ['matwa', 'mat'], ['ma', 'mat'], ['ratif']]
rendp = "(atè|atwa|atif)$"
for row in result :
if re.search(rendp, row) == None :
result.remove(row)
joined = '\n'.join(' - '.join(map(str, row)) for row in result)
print(joined)
Here's the error : 这是错误:
Traceback (most recent call last):
File "C:\Users\alice\OneDrive\Documents\Visual Studio 2017\Projects\CréoleDB\CréoleDB\CréoleDB.py", line 65, in <module>
if re.search(rendp, row) == None :
File "C:\Users\alice\Anaconda3\lib\re.py", line 182, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object
I could really use some help. 我真的可以使用一些帮助。 Thanks a lot !
非常感谢 !
Currently, you are passing a list ( row
) to re.search
; 当前,您正在将列表(
row
)传递给re.search
; however, only a string can be used for pattern matching. 但是,只能将字符串用于模式匹配。 Try filtering the list using a comprehension:
尝试使用理解来过滤列表:
import re
result = [['blablaté', 'blabla'], ['klak'], ['matwa', 'mat'], ['ma', 'mat'], ['ratif']]
rendp = "(até|atwa|atif)$"
final_list = [i for i in result if any(re.findall(rendp, b) for b in i)]
Output: 输出:
[['blablaté', 'blabla'], ['matwa', 'mat'], ['ratif']]
With re.compile()
, regex.search()
and any()
functions: 使用
re.compile()
, regex.search()
和any()
函数:
import re
lists = [['blablaté', 'blabla'], ['klak'], ['matwa', 'mat'], ['ma', 'mat'], ['ratif']]
pat = re.compile(r'(até|atwa|atif)$') # compiled regular expression object
result = [l for l in lists if any(pat.search(i) for i in l)]
print(result)
The output: 输出:
[['blablaté', 'blabla'], ['matwa', 'mat'], ['ratif']]
PS Please don't give your variables names as list
, dict
, str
etc. as those are built-in Python data types PS:请不要给变量命名为
list
, dict
, str
等,因为它们是内置的Python数据类型
The error you see is because you are trying to search for the pattern in the inner list. 您看到的错误是因为您试图在内部列表中搜索模式。 re.search only works when you search for pattern in a string.
仅当您在字符串中搜索模式时,re.search才有效。 You can try having a nested loop like this:
您可以尝试使用这样的嵌套循环:
for word_list in result:
for word in word_list:
if re.search(rendp, word) == None:
word_list.remove(word)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.