简体   繁体   English

如何使用 Python 中的 function 在两个列表之间进行过滤?

[英]How can you filter between two lists using a function in Python?

I am attempting to get Python to filter values between two lists.我试图让 Python 过滤两个列表之间的值。 It is pasting the values of the original list and ignoring the instructions.它正在粘贴原始列表的值并忽略说明。 Thanks in advance for your input.提前感谢您的意见。

string_val = ['Tom','Kyle','Carl','Tom','Mike','Austin']
delval = ['Tom','Kyle']


def filter_list(string_val, delval):
    new_string =[]
    for v in string_val:
        if (string_val) != (delval):
            new_string.append(v)
    return new_string

result = filter_list(string_val, delval)
print(result)

You're comparing wrong value, you should compare each value instead of entire delval.您正在比较错误的值,您应该比较每个值而不是整个 delval。

def filter_list(string_val, delval):
    new_string =[]
    for v in string_val:
        found = False
        if v not in delval:
            new_string.append(v)
    return new_string

result = filter_list(string_val, delval)
print(result)

First of all you are iterating through string_val which has names.首先,您正在遍历具有名称的string_val You want to check if each name is in delval .您想检查每个名称是否在delval中。 Do this using in I think your looking for something like this (list comprehension)使用in我认为您正在寻找类似的东西(列表理解)

string_val = ['Tom','Kyle','Carl','Tom','Mike','Austin']
delval = ['Tom','Kyle']
def filterList(string_val,delval):
    return [names for names in string_val if names not in delval]
print(filterList(string_val,delval))

output output

['Carl', 'Mike', 'Austin']

Here's the Python one-liner code to find the disjoint elements.这是用于查找不相交元素的 Python 单行代码。

def filter_list(string_val, delval):
    return list(set(string_val) - set(delval))

Output Output

['Austin', 'Mike', 'Carl']

暂无
暂无

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

相关问题 如何构建一个 function,它使用 python 从两个不同的列表中找到两个值之间的所有整数? - How to build a function that finds all integers between two values from two different lists using python? 如何使用 function 在 Python 中过滤? - How do you filter in Python using a function? 如何在 python 中使用 365 天格式计算两个日期/时间列表之间的时差? - How can I calculate the time difference between two lists of dates/times using 365 day format in python? 如何优化具有两个元素的列表之间的交集,并在python中生成没有重复的列表列表? - How can I optimize the intersections between lists with two elements and generate a list of lists without duplicates in python? 如何在 Python 中的两个列表之间共享排序 - How to share a ordering between two lists in Python 如何使用Python在两个字符串之间提取文本? - How do you extract texts between two strings using Python? 在 Python 中使用 Zip function 比较两个列表 - Comparing two Lists using Zip function in Python 如何过滤两个模型之间的差异? 使用 django - how can I filter the difference between the two models? using django 如何在不使用内置函数的情况下减去python中具有相同长度的两个列表中的值(仅使用循环) - How can I subtract the values in two lists with the same lengths in python without using built-in function (only using loops) 如何在Python中找到包含列表的两个列表之间的区别? - How to find the difference between two lists that contain lists in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM