我将一个列表传递给一个函数并查看输出。当列表被硬编码时,我得到了预期的输出。但是当我从一个字符串构建列表,并将具有相同内容的列表传递给函数时,我没有得到预期的输出。
第一次通话:
tech = [ "Django", "Zend", "SQLite", "foo" ]
for tech_item in tech:
print( test_main( tech_item ) )
第二次调用:
raw_txt = "Django,Zend,SQLite,foo"
tech = raw_txt.split( "," )
# At this point if we print "tech" we have:
# [ "Django", "Zend", "SQLite", "foo" ]
for tech_item in tech:
print( test_main( tech_item ) )
所以,输入是/似乎是相同的,但输出是不同的。
当我比较两个列表的内容时(重命名第二个列表:tech2)我有:
print( tech[0], tech2[0] ) #Django Django
print( tech[0] == tech2[0] ) #True
print( type(tech[0]), type(tech2[0]) ) #<class 'str'> <class 'str'>
print( len(tech[0]), len(tech2[0]) ) #6 6
我错过了什么? 您知道如何找到/解决这个问题吗?
编辑:
第一种情况的输出:
frameworks
frameworks
SQL
None
第二种情况的输出:
None
None
None
None
测试主函数
我给了你 test_main 函数,但恐怕它会让你感到困惑。 所以,“looking_for”每次都是一样的。但是“tmp”在这两种情况下都是不同的。
def test_main( looking_for ):
global tmp
tmp = None
get_recursively( languages_tech, looking_for )
return tmp
get_recursively 函数
def get_recursively( data, looking_for, last_key="" ):
if not isinstance( data, (list, dict) ):
if data is looking_for: #item
global tmp
tmp = last_key
else:
if isinstance( data, dict ): #Dictionaries
for key, value in data.items():
get_recursively( value, looking_for, key )
else:
for item in data: #list
get_recursively( item, looking_for, last_key )
语言技术
languages = { "languages": [
"Ruby", "Python", "JavaScript", "ASP.NET", "Java", "C", "C++", "C#", "Swift", "PHP", "Visual Basic", "Bash" ] }
frameworks = { "frameworks" : [
"Django", "Flask", "React", "React Native", "Vue", "Ember", "Meteor", "AngularJS", "Express" , "Laravel", "Symfony", "Zend", "Ruby on Rails" ] }
databases = { "databases" : [
{ "SQL": ["MariaDB", "MySQL", "SQLite", "PostgreSQL", "Oracle", "MSSQL Server"] },
{ "NoSQL": ["Cassandra", "CouchDB", "MongoDB", "Neo4j", "OrientDB", "Redis", "Elasticsearch"] },
{ "ORM Framework": [ "SQLAlchemy", "Django ORM" ] } ] }
languages_tech = { "languages_tech": [ languages, frameworks, databases ] }
请您参考如下方法:
简答
get_recursively() 函数中的以下行是错误的
if data is looking_for:
改用这个
if data == looking_for:
长答案
a is b 仅当 a 和 b 具有相同的 id 时才计算为 。也就是说,true
(a is b) == (id(a) == id(b))
默认情况下,字符串文字被分配相同的 id。例如,
>>> a = "Trebuchet"
>>> b = "Trebuchet"
>>> id(a), id(b)
(4416391792, 4416391792)
请注意,两个 id 都是 4416391792。这也包含在 list 中(即使列表不是同一个对象,因此没有相同的 id)。
>>> a = ["Trebuchet", "Catapult", "Ballista"]
>>> b = ["Trebuchet", "Catapult", "Ballista"]
>>> id(a), id(b)
(4416392200, 4416861640)
>>> id(a[0]), id(b[0])
(4416391792, 4416391792)
请注意,4416391792 与上一个示例中的数字完全相同。这将显示字符串如何指向同一对象。
但是当你引入 str.split() 函数时......
>>> a = "Trebuchet;Catapult;Ballista".split(';')
>>> b = ["Trebuchet", "Catapult", "Ballista"]
>>> id(a[0]), id(b[0])
(4416392240, 4416391792)
id(b[0]) 保持在 4416391792,我们之前已经看到了。但是现在,请注意 str.split() 在列表中创建了一个带有 id = 4416392240 的新字符串对象!!!
这构成了为什么 data is looking_for 计算结果为 false 的原理。
当然,是也有它的优点。例如,我们执行 a is None 而不是 a == None ( read more )。但重要的是要辨别何时使用 is 以及何时使用 ==。比较字符串、列表或元组等值的文字时,使用 ==。
进一步阅读:
Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?
Another example where variables can have congruent string literals but different ids.
