mer*_*011 4 python python-2.x python-2.7
我是Python的新手,所以我欢迎其他方法.
我有一个我开头的词典列表(从文件中读取).现在我有一堆额外的词典,我想添加到此列表中,但前提是它们不在原始列表中.
但是,我要求"不在原始列表中"由自定义比较函数定义,而不是Python默认使用的任何内容.
更具体地说,我想比较字典中的某些键/值对,如果它们相同,则为表达式返回"true".
myList = ReadFromFile...
newList = ReadFromFile...
for item in newList:
if item not in myList: #I want custom behavior for this "in"
myList.append(item)
Run Code Online (Sandbox Code Playgroud)
用途any:
any(customEquals(item, li) for li in myList)
Run Code Online (Sandbox Code Playgroud)
如果myClass属于您可以控制的类型,则还可以覆盖该__contains__方法.
| 归档时间: |
|
| 查看次数: |
2778 次 |
| 最近记录: |