match

How to check that element of a list of lists matches a condition?

只谈情不闲聊 提交于 2020-08-22 05:49:10
问题 I have a list of lists: pairs <- list( list(Name="A",Value=11), list(Name="B",Value=17), list(Name="C",Value=23) ) How can I check that pairs list contains an element with Name=="A"? And I'd also like to get that element. 回答1: If you just want to know whether any list component has Name=='A' : any(sapply(pairs,function(x) x$Name=='A')); ## [1] TRUE If you want the number of list components that have Name=='A' : sum(sapply(pairs,function(x) x$Name=='A')); ## [1] 1 If you want the Value of the

python Keyword matching(keyword list - column)

江枫思渺然 提交于 2020-08-11 02:10:02
问题 supposed dataset, Name Value 0 K Ieatapple 1 Y bananaisdelicious 2 B orangelikesomething 3 Q bluegrape 4 C appleislike and I have keyword list like [apple, banana] In this dataset, matching column 'Value' - [keyword list] *I mean matching is keyword in list in 'Value' I would like to see how the keywords in the list match column, so.. I want to find out how much the matching rate is. Ultimately, what I want to know is 'Finding match rate between keywords and columns' Percentage, If I can,

Dictionary keys match on list; get key/value pair

拜拜、爱过 提交于 2020-08-01 09:36:10
问题 In python... I have a list of elements 'my_list', and a dictionary 'my_dict' where some keys match in 'my_list'. I would like to search the dictionary and retrieve key/value pairs for the keys matching the 'my_list' elements. I tried this... if any(x in my_dict for x in my_list): print set(my_list)&set(my_dict) But it doesn't do the job. 回答1: (I renamed list to my_list and dict to my_dict to avoid the conflict with the type names.) For better performance, you should iterate over the list and

Regex matches parts of a string, but not whole string

耗尽温柔 提交于 2020-07-31 04:21:32
问题 This is the regex I'm using to validate a string that can contain lowercase and uppercase letters, numbers and dash: /([a-zA-Z0-9-])+$/ It has the following results: abd - matches abcd- - matches abcd0 - matches abcd0- - matches abc@ - doesn't match (correct) abc@efg - matches (incorrect, it shouldn't) What am I doing wrong? 回答1: I would say you need /^([a-zA-Z0-9-])+$/ . You want to match the whole string, not just a part, but you're missing the mark for the beginning of the string ^ . ^ and

Early-breaking from Rust's match

好久不见. 提交于 2020-07-29 12:20:14
问题 I want to switch through many possible cases for x and there's one case (here x == 0 ) where I want to check the result of some additional code to determine what to do next. One possibility is to return early from the match. I'd use break to do this early-returning in C, but this isn't allowed in Rust. return returns from the parent function (in this case main() ) and not from the match only (i.e. the println! at the end isn't run!). I could just negate the sub-condition (here y == 0 ) and