微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

试图找到两个列表之间的差异,并将差异附加到第三列表中,我的第三列表是在Python中生成的嵌套表格

如何解决试图找到两个列表之间的差异,并将差异附加到第三列表中,我的第三列表是在Python中生成的嵌套表格

当我在列表中使用有限值在本地进行编码时,它正在工作,并且未创建任何嵌套列表。但是当我用主代码运行代码时,差异列表(第三列表)以嵌套形式出现。

def non_match_elements(first_list,second_list):
        third_list = []
        for item in first_list:
            if item not in second_list:
                third_list.append(item)
def main():
    first_list = [1,2,3,4,5,6,10,15,22]
    second_list = [2,14,18]
    third_list = (non_match_elements(first_list,second_list))
    print(third_list))


if __name__ == "__main__":
    main()

代码运行良好,但是当我的原始列表出现时,它会将第三个列表作为嵌套列表。我如何找出问题所在?我的原始列表有很多字符串形式的元素,并且都是非嵌套列表。这些列表的数据来自不同的功能

解决方法

例如,您有很多错别字:

  • print(third_list))-不必要的括号

  • )-额外的third_list

  • 没有从函数中返回def non_match_elements(first_list,second_list): third_list = [] for item in first_list: if item not in second_list: third_list.append(item) return third_list first_list = ["Something.1.0.00643-NEW","Something.1.0.00654-NEW","Something.1.0.00671-NEW","Something.1.0.00687-NEW","Something.1.0.00718-NEW","Something.1.0.00737-NEW","Something.1.0.00747-NEW","Something.1.0.00758-NEW","Something.1.0.00774-NEW","Something.1.0.00777-NEW","Something.1.0.00799-NEW","Something.1.0.00814-NEW","Something.1.0.00821-NEW","Something.1.0.00843-NEW","Something.1.0.00855-NEW","Something.1.0.00867-NEW","Something.1.0.00886-NEW","Something.1.0.00900-NEW","Something.1.0.00920-NEW","Something.1.0.00925-NEW","Something.1.0.00942-NEW"] second_list = [] third_list = non_match_elements(first_list,second_list) print(third_list) print(first_list==third_list)

好奇它如何为您工作?

修改后-对我来说很好:

['Something.1.0.00643-NEW','Something.1.0.00654-NEW','Something.1.0.00671-NEW','Something.1.0.00687-NEW','Something.1.0.00718-NEW','Something.1.0.00737-NEW','Something.1.0.00747-NEW','Something.1.0.00758-NEW','Something.1.0.00774-NEW','Something.1.0.00777-NEW','Something.1.0.00799-NEW','Something.1.0.00814-NEW','Something.1.0.00821-NEW','Something.1.0.00843-NEW','Something.1.0.00855-NEW','Something.1.0.00867-NEW','Something.1.0.00886-NEW','Something.1.0.00900-NEW','Something.1.0.00920-NEW','Something.1.0.00925-NEW','Something.1.0.00942-NEW']
True

输出:

{{1}}

因为first_list中的所有项目都不在second_list中-所有这些都已打印出来,所以我也打印出了first_list == third_list的检查。没有看到任何问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。