Possible Duplicate:
Function printing correct Output and None
试图在一个假装的twitter feed程序中打印共同用户。作业。数字是用户(例如1111
是用户)。字典里下面的一组是钥匙的朋友。你知道吗
我想要这个输出:
Most friends in common
1111: 222 33333 5454
222: 1111 33333 5454
33333: 1111 222
5454: 1111 222
而是有这样的输出:
Most friends in common
1111 : 222 33333 5454 1111 None
222 : 1111 33333 5454 222 None
33333 : 1111 222 33333 None
5454 : 1111 222 5454 None
这是我的程序:
all_users = {'1111': set(['5454', '222']), '33333': set(['5454']), '5454': set(['1111', '33333', '222']), '222': set(['1111', '5454'])}
def findmutual(id, all_users):
users = []
for key in all_users:
mutual = all_users[id].intersection(all_users[key])
rank = len(mutual)
templist = [rank, key.strip()]
users.append(templist)
users = sorted(users)
print users
for item in users:
if id == item[1]:
print "" ,
if item[0] == 0:
print "",
else:
print item[1],
users = []
for key in all_users:
users.append(key)
users = sorted(users)
print "Most friends in common"
for item in users:
print item, ":\t", findmutual(item, all_users)
请帮助我理解这个问题!你知道吗
findmutual
不返回任何内容,这在Python中相当于返回None
。所以在输出中出现“None”是因为您告诉它打印findmutual(item, all_users)
的返回值,它总是None
。你知道吗数字仍在打印,因为
findmutual
本身打印它们。这很少是您想要的,因为它使您无法灵活地调用findmutual
来查找用户id并对其执行任何其他操作;每次调用它时,它都会打印它们(即使您不希望这样做),并且您无法访问Python中的数字来进一步操作。你知道吗这里发生了什么?你知道吗
你可能想改变步骤3-4! 而不是所有打印语句,例如:
更新:
如果我没说错的话,你是想根据谁最受欢迎来确定排名?你知道吗
在这种情况下,您可以看看解决方案:
相关问题 更多 >
编程相关推荐