请考虑以下事项:
variable = "TELKOM"
registry = {,
"CELLC": CellCSubscriber,
"TELKOM": TelkomSubscriber,
"HELLO MOBILE": HelloMobile,
"VODACOM": VodacomSubscriber,
"MTN": MTNSubscriber,
}
for k, v in registry:
if variable == k and variable == "TELKOM":
#do something to variable
有更好的方法吗?
我有:
if variable in registry:
#do something to variable
但问题是,我无法将其与"TELKOM", or am I wrong?
答案 0 :(得分:4)
你可以这样做:
if variable == "TELKOM" and variable in registry:
# do something to variable
答案 1 :(得分:0)
我不确切地知道你要做什么,但你可以说是传递属性
for k, v in registry:
if 'TELKOM' == k:
# do something
那是同样的事情。
如果我不得不猜测,我觉得你只是想做:
if 'TELKOM' in registry:
# do something