python - 与 Python 中的字典比较
问题描述
我有一个函数,它接受一个字符串,它计算单个字符并将字符连同它显示的时间量一起放入一个字符串中。例如:
isanagram("surfer")
返回
w1 = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 1, 'f': 1, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 2, 's': 1, 't': 0, 'u': 1, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
尽管当我将此函数与两个不同的参数进行比较时,打印语句输出True而不是False,这显然应该是。有没有人可以看看。这是我的代码:
alphabetlist = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
alphabetdict = {}
newalphadict = {}
def isanagram(aword):
for i in alphabetlist:
count = word.count(i)
alphabetdict[i] = count
return alphabetdict
print(isanagram("computer") == isanagram("science")) #outputting True. Should be outputting False.
解决方案
alphabetlist = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
def isanagram(word=''):
alphabetdict = {}
for i in alphabetlist:
count = word.count(i)
alphabetdict[i] = count
return alphabetdict
print(isanagram("computer") == isanagram("science")) #-> False
推荐阅读
- android - 无法在 Windows 10 上下载 Android Studio
- go - 使用 go get 从 WSL 2 上的私有 github 存储库中获取依赖项
- mysql - 聚合后Django ORM加入自我
- mongodb - 在 mongodb 上按年分组
- jmeter - 在mac上运行jmeter的问题
- sql - 在同一列的行之间应用条件
- azure-ad-b2c - 用户旅程正文中的 Azure B2C Rest API 为空
- php - PHP,数组-混合顺序,steam api返回不同顺序
- sas - SAS:如何测试一个值在时间之间的稳定性
- javascript - SameSite=Lax 是否应该允许 3rd-party GET?