Python在数据结构代码解释上迭代

Python在数据结构代码解释上迭代,python,dictionary,active-directory,ldap,python-ldap,Python,Dictionary,Active Directory,Ldap,Python Ldap,我当时正在使用ldapsearch用python编写代码,在中遇到了这个链接 我无法理解这里的这段代码results=[输入dn,输入result if is instance(输入,dict)] 我看不到上面代码中定义的dn,那么它从何而来 isinstance(条目、指令)做什么 当我尝试执行此操作时,我可以看到它返回了所有ldap条目的列表,以及与之关联的相应属性。初始的结果也返回一个列表。有人能解释一下结果是什么吗 你看到的是一个列表理解。 它们是允许从其他序列构建序列的构造 在列表

我当时正在使用ldapsearch用python编写代码,在中遇到了这个链接

我无法理解这里的这段代码
results=[输入dn,输入result if is instance(输入,dict)]

  • 我看不到上面代码中定义的
    dn
    ,那么它从何而来
  • isinstance(条目、指令)
    做什么

当我尝试执行此操作时,我可以看到它返回了所有ldap条目的列表,以及与之关联的相应属性。初始的
结果也返回一个列表。有人能解释一下
结果是什么吗

你看到的是一个列表理解。 它们是允许从其他序列构建序列的构造

在列表理解之前未看到
dn
的原因,因为它是一个占位符(类似于
循环中
列表中项目的
中的

我不是一个理解方面的专家,所以我提供了一些链接,可以帮助你对这件事有更多的了解



谢谢。这个链接很有用,谢谢。分解版本有助于理解这一点。
import ldap

l = ldap.initialize("ldap://ldap.example.com")
try:
    l.protocol_version = ldap.VERSION3
    l.set_option(ldap.OPT_REFERRALS, 0)

    bind = l.simple_bind_s("me@example.com", "password")

    base = "dc=example, dc=com"
    criteria = "(&(objectClass=user)(sAMAccountName=username))"
    attributes = ['displayName', 'company']
    result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)

    results = [entry for dn, entry in result if isinstance(entry, dict)]
    print results
finally:
    l.unbind()
somelist = [entry for dn, entry in result if isinstance(entry, dict)] 
translates to 

for entry, dn in result:
   if isinstance(entry, dict): # this checks if entry is of type dictionry
      somelist.append(entry)