从另一个列表中的一个列表中查找字符串并找到返回的字符串 [英] Find String from One List within Another List and Return String Found
问题描述
我在>电源查询中的匹配列表/表中找到了我要找的内容,但我还需要更多。
I found part of what I was looking for at Matchlists/tables in power query, but I need a bit more.
使用 ListB中一行的内容可能只是该行内容的一部分请注意,在下面的查询中,ListB包含 roo,这是单词室中的前三个字母。
Using the "Flags only" example provided at Matchlists/tables in power query, I’m comparing two lists, ListA and ListB, to check if ListB’s row content appears in ListA’s row content at all. I can’t do a one-for-one match of both rows’ contents (like with List.Intersect) because the content of a row in ListB might only be part of the content of a row in ListA.
我想知道 roo在ListA的行中,该行在我的房间里。
Note that, in the query below, ListB includes "roo", which is the first three letters in the word room. I would want to know that "roo" is in ListA’s row that has "in my room."
由高级查询中的匹配列表/表已经确定 roo是ListA所在行中我的房间的一部分。 我在该示例的基础上分配了是,而不是在ListA和ListB之间存在这种匹配时为true。
The "Flags only" example provided by Matchlists/tables in power query already determines that "roo" is part of ListA’s row that has "in my room." I built on the example to assign "yes," instead of true when there is such a match between the ListA and ListB.
我想做的是用ListB中的实际值替换是,例如,值 roo。我试图简单地用wordB代替是,但是我得到一个错误,即无法识别wordB。
What I’d like to do is to replace "yes" with the actual value from ListB — the value "roo," for instance. I tried to simply substitute wordB for "yes" but I got an error that wordB wasn’t recognized.
let
ListA = {"help me rhonda", "in my room", "good vibrations", "god only knows"},
ListB = {"roo", "me", "only"},
contains_word=List.Transform(ListA, (lineA)=>if List.MatchesAny(ListB, (wordB)=>Text.Contains(lineA, wordB)) = true then "yes" else "no")
in
contains_word
当前查询的结果如下:
List
1 yes
2 yes
3 no
4 yes
我希望查询结果为:
List
1 roo
2 me
3
4 only
有什么想法吗?
(ps我对Power Query非常了解/ M)
(p.s. I'm extremely new to Power Query / M)
谢谢
推荐答案
我会这样做:
let
ListA = {"help me rhonda", "in my room", "good vibrations", "god only knows"},
ListB = {"roo", "me", "only"},
contains_word=List.Transform(ListA, (lineA)=>List.Select(List.Transform(ListB, (wordB)=>if Text.Contains(lineA, wordB) = true then wordB else null), (x)=>x <> null){0}?)
in
contains_word
[edited]
这个想法是使用List.Transform两次:内部一个更改列表B只保留匹配的值。然后第一个非null值的最新值替换列表A(外部List.Tramsform)中的字符串。
The idea is to use List.Transform twice: inner one changes list B to leave only matching values. Then 1st non-null of latest replaces string from list A (outer List.Tramsform).
这篇关于从另一个列表中的一个列表中查找字符串并找到返回的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!