在Python中使用“is not”

2024-09-30 01:27:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用光标搜索access表中一个名为DEV_TYPE的字段中的一些记录。我想将每个记录与我之前在脚本中构建的已知值列表进行比较:

(devcatList)

我想打印出列表中没有出现的值。记录中的某些值也为空。我想将if语句设置为只输出列表中没有出现的值,但也不想为空值打印“None”。我的脚本如下:

if field.name == "DEV_TYPE":
for iRow in arcpy.SearchCursor(fc):
      if not iRow.DEV_TYPE is None or iRow.DEV_TYPE not in devcatList:
          print str(iRow.OBJECTID) + " - " + str(iRow.DEV_TYPE)

我把if not x is None'改为if x is not None。将“or”改为“and”(尽管这有违直觉),但我的打印输出要么返回所有值要么不返回值,要么只返回“None”……基本上我不想要的一切。我肯定我在做傻事。有人能指出我的愚蠢是什么吗?在

谢谢, 迈克


Tags: orindev脚本none列表ifis
2条回答

也许是这样的:

if field.name == "DEV_TYPE":
    for iRow in arcpy.SearchCursor(fc):
       dev_type = iRow.DEV_TYPE
       if dev_type is not None and dev_type not in devcatList:
           print "{} - {}".format(iRow.OBJECTID, iRow.DEV_TYPE)

由于None在条件上下文中等同于False,因此if dev_type is not none可以更方便地(但不太精确地)声明{}。我还冒昧地以更惯用的方式重写了print语句。在

我想你想if iRow.DEV_TYPE is not None and iRow.DEV_TYPE not in devcatList:

相关问题 更多 >

    热门问题