简体   繁体   中英

How do I fild all rows of a 2d array by matching a certain element?

So I have a 2d-array which looks something like this:

[
    ["USER1", "2020-03-25 10:41", "hello there I like r/stackoverflow"],
    ["USER2", "2020-03-25 10:42", "I prefer r/programmerhumor"],
    ["USER3", "2020-03-25 10:42", "No, I don't like reddit"]
]

And I wish to find each row that contains a message that contain r/... in them and return the whole row. The following function is designed to fetch all messages which contain a the keyphrase but I can't figure out how to return the whole row.

def get(messages, keyPhrase, column=2):
    r = re.compile("/"+keyPhrase+"/g")
    return list(filter(r.match, [message[column] for message in messages]))

Any help would be appreciated!

Edit: the regex also does not work and I'd apprectiate help on this

您需要将完整数据数组作为可迭代对象传递并在过滤器函数中提取消息:

filter(lambda row: 'r/' in row[column], messages)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM