2011-06-04 143 views
5

類Item有一個成員函數text(),它返回一個字符串列表。 類Dictionary有一個成員函數items(),返回一個Items列表。 dict是Dictionary的一個實例。 我想測試字典中所有項目中所有字符串中的所有字符是否都是ASCII。 我試圖Python,迭代列表理解

all(ord(ch) < 128 for ch in s for s in item.text() for item in dict.items()) 

這給了錯誤消息「的全局名稱的「沒有定義」。 什麼是正確的方法?

回答

5

for子句的順序需要相反。最裏面的循環最後,最外面的循環最先。

all(ord(ch) < 128 for item in dict.items() for s in item.text() for ch in s)