这是我的代码。
def areSimilar(item1,item2):
if(len(item1) == len(item2)):
for i in range(len(item1)):
if isinstance(item1[i],list) and isinstance(item2[i],list):
if areSimilar(item1[i],item2[i])==False:
return False
else:
if type(item1[i]) != type(item2[i]):
return False
else:
return False
return True
以下是想要工作的例子。
>>> print(areSimilar(True, False)) #items are same type and are not lists
True
>>> print(areSimilar(1, 'a') #items are different types
False
>>> print(areSimilar([],[]))
True
>>> print(areSimilar([],[3])) #list lengths differ
False
>>> print(areSimilar([3.0],[3])) #lists of same length but index 0 items are not similar
False
>>> print(areSimilar([5],[3])) #lists of same length and corresponding lists items are similar
True
>>> print(areSimilar([1,2,['a','b']],[3,4, [1,2,3]])) #items at index 2 are not similar
False
>>> print(areSimilar([1,2,[False, 'b']],[3, 4, [True, 'hello']]))
True
>>> print(areSimilar([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', [1]]]]]))
True
>>> print(areSimilar([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', 0]]]]))
False
除前两个外,所有这些都有效。我在哪里
" TypeError:类型' bool / int'的对象没有len"
我该如何解决这个问题?