Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较python中包含字符串的两个列表_Python_List_String Comparison - Fatal编程技术网

比较python中包含字符串的两个列表

比较python中包含字符串的两个列表,python,list,string-comparison,Python,List,String Comparison,比较这两个列表,如果匹配,则从List1中删除。是否也有办法处理列表1 List1: ["'file',", "'ist',", "'customer',"] List2: ['permission', 'ist', 'dr'] 似乎一个简单的列表就可以了 filtered_list = [string for string in List1 if string not in List2] 警告:列表1中的字符串与列表2中的字符串格式不匹配。不确定这是否是你的意图。字符串'ist',将与字

比较这两个列表,如果匹配,则从
List1
中删除。是否也有办法处理
列表1

List1: ["'file',", "'ist',", "'customer',"]

List2: ['permission', 'ist', 'dr']

似乎一个简单的列表就可以了

filtered_list = [string for string in List1 if string not in List2]

警告:列表1中的字符串与列表2中的字符串格式不匹配。不确定这是否是你的意图。字符串
'ist',
将与字符串
ist

不匹配,似乎一个简单的列表理解就能做到这一点

filtered_list = [string for string in List1 if string not in List2]

警告:列表1中的字符串与列表2中的字符串格式不匹配。不确定这是否是你的意图。字符串
'ist',
将与字符串
ist
不匹配,这将为您提供所需的输出

for i in list(List1):
    if i.strip("',") in List2:
        List1.remove(i)

这将为您提供所需的输出

for i in list(List1):
    if i.strip("',") in List2:
        List1.remove(i)


您所说的处理列表是什么意思?您能发布您的预期输出吗?处理列表是指将列表1发送到['file'、'ist'、'customer'],以简化处理@预期的输出应该从列表1中删除“ist”您所说的处理列表是什么意思?您可以发布预期的输出吗?处理列表意味着将列表1发送到[“文件”、“ist”、“客户”],以简化处理@Sexpected output应该从列表1中删除“ist”,因为这不是最有效的,因为它是O(n^2)运行时间。事实上,我知道要进行比较,列表1的格式会产生问题。如何处理。最干净的方法可能是创建一个新的列表,其中字符串已清理
str.strip
可能会执行此操作,具体取决于这些字符串中的其他格式错误
cleaned_list_1=[x.strip(['”,“'”,“,”,“]),用于列表1中的x]
@Tnguyen True。如果他使用set(List2)并在集合中而不是直接在列表2中搜索,它将在O(n)中运行。值得注意的是,这并不是最有效的,因为它是O(n^2)运行时间实际上我知道要进行比较,列表1的格式正在产生问题。如何处理。最干净的方法可能是创建一个新的列表,并清除字符串。
str.strip
可能会这样做,具体取决于这些字符串中有哪些其他格式错误。
cleaned_list_1=[x.strip(['',“,”,'])对于列表1中的x]
@Tnguyen True。如果他使用set(List2)并在set中搜索,而不是直接在List2中搜索,它将在O(n)中运行。你真是太棒了。。从早上开始就一直在做这件事。。明亮的谢谢你真是太棒了。。从早上开始就一直在做这件事。。明亮的谢谢