简体   繁体   English

numpy-如何方便地比较自定义dtype?

[英]numpy - How to compare custom dtypes conveniently?

I have a dtype that has more than 30 fields. 我有一个超过30个字段的dtype。 I want to compare two objects with that dtype so that I know exactly which fields are unequal. 我想比较具有该dtype的两个对象,以便我确切地知道哪些字段不相等。 A trivial solution would be to hard-code each field comparison in a series of if statements: 一个简单的解决方案是在一系列if语句中对每个字段比较进行硬编码:

if (obj1['field1']==obj2['field1']) DO_SOMETHING
if (obj1['field2']==obj2['field2']) DO_SOMETHING
# ...  

Is there a better way to compare two objects with custom dtypes and know exactly which fields match or not? 有没有更好的方法比较两个具有自定义dtypes的对象并确切知道哪些字段匹配或不匹配?

You can access an object's dtype fields by OBJECT.dtype.names . 您可以通过OBJECT.dtype.names访问对象的OBJECT.dtype.names字段。 So: 所以:

# obj1 and obj2 are elements in a numpy array with a custom dtype
for field in obj1.dtype.names:
    if obj1[field]==obj2[field]:
        # DO_SOMETHING

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM