从给定列表中获取满足字典条件的名称列表

Get the list of names which satisfy the condition in dictionary from given list

提问人:Mohit 提问时间:1/16/2023 更新时间:1/16/2023 访问量:48

问:

我有一本字典,其中有三个条件 startwith、contains 和 endwith,下面给出,

dict1 = {'startwith':"Raja, Bina", 'contains':"Tata", "endwith":""}

如果用户在字典中给出带有逗号的值,则表示 OR,即“Raja, Bina” = Raja 或 Bina

现在我有一个名字列表在下面给出

list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

现在使用上面的字典和列表,我必须从列表中找到满足字典中给出条件的名称,从上面的示例结果应该是这样的(需要列表中的结果)

required_list = ["Raja Molli Jira", "Bina Tata Birla"]

required_list中的名称满足字典中给出的条件,即 startwith 和 contains。

示例 2

如果 dict1 和 list_of_names 是:

dict1 = {'startwith':"", 'contains':"Tata, Gola", "endwith":"Too"}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

required_list列表将是:

required_list = ["Bina Tata Birla", "Fira Kiya Too"]

required_list中的名称满足字典中给出的条件,即 contains 和 endwith

当前使用代码

如果用户使用以下代码给出单个值(不带逗号),我能够处理问题

dict1 = {'startwith':"Raja,Bina", 'contains':"Tata", "endwith":""}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", 'Fira Kiya Too']
required_list = []
fileopp  = list(dict1.values())

for i in list_of_files:
   #startswith
   if  ((fileopp[0] != "") and (fileopp[1] == "") and (fileopp[2] == "")):
        if i.startswith(fileopp[0]):
               listfilename.append(i)
   #containswith
  elif ((fileopp[0] == "") and (fileopp[1] != '') and (fileopp[2] == "")):
        if i.__contains__(fileopp[1]):
               listfilename.append(i)
   #endiswith
   elif ((fileopp[0] == "") and (fileopp[1] == '') and (fileopp[2] != "")):
        if i.endswith(fileopp[2]):
            listfilename.append(i)
   #startswith and contains with
   elif ((fileopp[0] != "") and (fileopp[1] != "") and (fileopp[2] == "")):
        if (i.startswith(fileopp[0])) and i.__contains__(fileopp[1]):
            listfilename.append(i)
   #startswith and endswith
   elif ((fileopp[0] != "") and (fileopp[1] == "") and (fileopp[2] != "")):
        if (i.startswith(fileopp[0])) and i.endswith(fileopp[2]):
            listfilename.append(i)
   #containswith and endswith
   elif ((fileopp[0] == "") and (fileopp[1] != "") and (fileopp[2] != "")):
        if (i.__contains__(fileopp[1])) and i.endswith(fileopp[2]):
            listfilename.append(i)

问题

如果用户用逗号(Raja,Bina)给出值,则上述代码无法给出结果。

给出我想要的条件和所需的结果,

第一

dict1 = {'startwith':"Fira", 'contains':"", "endwith":"Birla"}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

required_list = ["Bina Tata Birla", "Fira Kiya Too"]

第二

dict1 = {'startwith':"Fira, Raja", 'contains':"", "endwith":""}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

required_list = ["Raja Molli Jira", "Fira Kiya Too"]
python-3.x pandas python-2.7 字典

评论


答:

0赞 Keks 1/16/2023 #1

由于用逗号分隔的两个名称位于同一字符串中,因此代码无法按照您希望的方式工作,因为它不会搜索单个名称,而是搜索整个字符串“Fira,Raja”,这在list_of_names中从未给出,因为甚至没有逗号。尝试将 dict1 设为二维的,这样您就可以为不同的方面提供多个条件(例如 startswith)。

1赞 Andrej Kesely 1/16/2023 #2

您可以尝试将列表推导与以下内容结合起来:any()

dict1 = {"startwith": "Fira, Raja", "contains": "", "endwith": ""}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]


def my_filter(dct, lst):
    s = list(v for v in map(str.strip, dct.get("startwith", "").split(",")) if v != "")
    c = list(v for v in map(str.strip, dct.get("contains", "").split(",")) if v != "")
    e = list(v for v in map(str.strip, dct.get("endwith", "").split(",")) if v != "")

    return [
        i
        for i in lst
        if any(i.startswith(x) for x in s)
        or any(x in i for x in c)
        or any(i.endswith(x) for x in e)
    ]


print(my_filter(dict1, list_of_names))

指纹:

['Raja Molli Jira', 'Fira Kiya Too']