简体   繁体   English

如何使用其他列表拆分字符串列表?

[英]How to split list of strings using other lists?

v1 = ['Elisa', 'Liza', 'Izabela', 'Elisabeth', 'Lisa']
v11 = ['Carmine', 'Crimson', 'Madder']
v12 = ['Foxtrot', 'Romeo', 'Delta']

v2 = ['Abbey', 'Abbie', 'Abigail', 'Abby', 'Gail']
v21 = ['Orchid', 'Thistle', 'Mauve', 'Carmine']
v22 = ['Alpha', 'Romeo', 'Foxtrot']

v3 = ['Jonathan', 'Jon', 'John', 'Jonny', 'Johnny', 'Nathan']
v31 = ['Ultramarine','Midnight blue','Navy blue','Orchid']
v32 = ['India','Echo','Sierra']

vloc = ["USA", "Mexico", "Canada", "Greenland", "Panama", "Cuba", "Costa Rica"]

List 'a' has values one value appearing from v1, v2, and v3.列表“a”具有从 v1、v2 和 v3 出现的值一个。 Also it has any one value appearing from each of these lists - v11, v12, v21, v22, v31, and v32.它还具有从这些列表中出现的任何一个值 - v11、v12、v21、v22、v31 和 v32。 But these values can be surrounded by other words.但是这些值可以被其他词包围。

v11 and v12 are dependent on presence of values from v1. v11 和 v12 取决于是否存在来自 v1 的值。

v21 and v22 are dependent on presence of values from v2. v21 和 v22 取决于是否存在来自 v2 的值。

v31 and v32 are dependent on presence of values from v3. v31 和 v32 取决于是否存在来自 v3 的值。

so if -因此,如果 -

a = ["Abbie", "Carmine - Buffalo, New York, USA", "Foxtrot : XYZ LLC inc.", "Lisa", "Romeo: John Doe", "Carmine: ABC, Vancouver, British Columbia, Canada", "Jon", "Echo: Johnny Doe", "Orchid : Male"]

How to get expected output using above lists for say variables - a1, a2, a3, a1_vloc, and a2_vloc in the following manner:如何以下列方式使用上述变量列表 - a1、a2、a3、a1_vloc 和 a2_vloc 获得预期的 output:

print(a1)
>>> ["Lisa", "Romeo: John Doe", "Carmine: ABC, Vancouver, British Columbia, Canada"]

print(a2)
>>> ["Abbie", "Carmine - Buffalo, New York, USA", "Foxtrot : XYZ LLC inc."]

print(a3)
>>> ["Jon", "Echo: Johnny Doe", "Orchid : Male"]

print(a1_vloc)
>>> Canada

print(a2_vloc)
>>> USA

Short answer:简短的回答:

a1 = ["Lisa", "Romeo: John Doe", "Carmine: ABC, Vancouver, British Columbia, Canada"]

a2 = ["Abbie", "Carmine - Buffalo, New York, USA", "Foxtrot : XYZ LLC inc."]

vloc = ["USA", "Mexico", "Canada", "Greenland", "Panama", "Cuba", "Costa Rica"]

def check(haystack, needles):
    return [needle for needle in needles if any(needle in s for s in haystack)]

print(check(a1, vloc))  # prints ['Canada']

print(check(a2, vloc))  # prints ['USA']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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