1

我有以下两个数组,我试图查看 invalid_id_arr 中的元素是否存在于 valid_id_arr 如果它不存在,那么我将形成 diff 数组。但是从下面的代码中,我在 diff 数组中看到以下内容['id123', 'id124', 'id125', 'id126', 'id789', 'id666'],我希望输出["id789","id666"]是我在这里做错了什么

tag_file= {}
tag_file['invalid_id_arr']=["id123-3431","id124-4341","id125-4341","id126-1w","id789-123","id666"] 
tag_file['valid_id_arr']=["id123-12345","id124-1122","id125-13232","id126-12332","id1new","idagain"] 
diff = [ele.split('-')[0] for ele in tag_file['invalid_id_arr'] if str(ele.split('-')[0]) not in tag_file['valid_id_arr']]

电流输出:

 ['id123', 'id124', 'id125', 'id126', 'id789', 'id666']

预期输出:

 ["id789","id666"]
4

3 回答 3

4

使用集合更有效,但您的主要问题是您没有删除 valid_id_arr 中的后半部分元素。

invalid_id_arr=["id123-3431","id124-4341","id125-4341","id126-1w","id789-123","id666"] 
valid_id_arr=["id123-12345","id124-1122","id125-13232","id126-12332","id1new","idagain"]
valid_id_set = set(ele.split('-')[0] for ele in valid_id_arr)
diff = [ele for ele in invalid_id_arr if ele.split('-')[0] not in valid_id_set]
print diff

输出:

['id789-123', 'id666']

http://ideone.com/Q9JBw

于 2012-06-13T08:33:18.277 回答
3

尝试sets

invalid_id_arr = ["id123-3431","id124-4341","id125-4341","id126-1w","id789-123","id666"] 
valid_id_arr = ["id123-12345","id124-1122","id125-13232","id126-12332","id1new","idagain"] 

set_invalid = set(x.split('-')[0] for x in invalid_id_arr)
print set_invalid.difference(x.split('-')[0] for x in valid_id_arr)
于 2012-06-13T08:35:58.977 回答
0
    >>> a = ["id123-3431","id124-4341","id125-4341","id126-1w","id789-123","id666"]
    >>> b = ["id123-12345","id124-1122","id125-13232","id126-12332","id1new","idagain"]
    >>> c = (s.split('-')[0] for s in b)
    >>> [ele.split('-')[0] for ele in a if str(ele.split('-')[0]) not in c]

        ['id789', 'id666']
    >>>  
于 2012-06-13T08:39:58.657 回答