如果我将字典定义为users = {},并且假设我在该字典中有一些数据,那么我如何搜索字典,并且如果我的搜索字符串与字典中的字符串匹配,则什么也不做。
for socket.user in MyServer.users:
if ((MyServer.users.has_key(socket.user)) == false):
MyServer.users[user].send(socket.message)
Run Code Online (Sandbox Code Playgroud)
所以这里是搜索用户字典,发现它存在,所以它应该什么都不做。我知道我的代码是错误的,但是我可以在第二行更改什么?
users = {"A": 0, "B": 1, "C": 2}
key = "B"
value = "2"
if key in users: print("users contains key", key)
if value in users.values(): print("users contains value", value)
Run Code Online (Sandbox Code Playgroud)