Python3:检查字典中的关键字是否匹配字符串的任何部分
Python3: Checking if a key word within a dictionary matches any part of a string
我在将工作代码从列表转换为字典时遇到问题。代码的基础检查列表中任何关键字的文件名。
但是我很难理解字典来转换它。我正在尝试提取每个键的名称并将其与文件名进行比较,就像我对列表和元组所做的那样。这是我正在做的模拟版本。
fname = "../crazyfdsfd/fds/ss/rabbit.txt"
hollow = "SFV"
blank = "2008"
empty = "bender"
# things is list
things = ["sheep", "goat", "rabbit"]
# other is tuple
other = ("sheep", "goat", "rabbit")
#stuff is dictionary
stuff = {"sheep": 2, "goat": 5, "rabbit": 6}
try:
print(type(things), "things")
for i in things:
if i in fname:
hollow = str(i)
print(hollow)
if hollow == things[2]:
print("PERFECT")
except:
print("c-c-c-combo breaker")
print("\n \n")
try:
print(type(other), "other")
for i in other:
if i in fname:
blank = str(i)
print(blank)
if blank == other[2]:
print("Yes. You. Can.")
except:
print("THANKS OBAMA")
print("\n \n")
try:
print(type(stuff), "stuff")
for i in stuff: # problem loop
if i in fname:
empty = str(i)
print(empty)
if empty == stuff[2]: # problem line
print("Shut up and take my money!")
except:
print("CURSE YOU ZOIDBERG!")
虽然前两个示例我能够获得完整的 运行,但我无法无一例外地获得 运行 的字典。循环不会将空值转换为 stuff[2] 的值。遗憾地将钱留在弗莱的口袋里。让我知道我的例子是否对我的要求不够清楚。字典只是对计数列表和向其他变量添加文件的快捷方式。
A dictionary 是一个将键映射到值的无序集合。如果将 stuff
定义为:
stuff = {"sheep": 2, "goat": 5, "rabbit": 6}
您可以通过以下方式引用其元素:
stuff['sheep'], stuff['goat'], stuff['rabbit']
stuff[2]
将导致 KeyError,因为在您的字典中找不到键 2
。您不能将字符串与字典的最后一个或第三个值进行比较,因为字典未按有序序列存储(内部排序基于散列)。对有序序列使用列表或元组 - 如果您需要与最后一项进行比较。
如果要遍历字典,可以用这个作为模板:
for k, v in stuff.items():
if k == 'rabbit':
# do something - k will be 'rabbit' and v will be 6
如果您想检查字典中的键以查看它们是否匹配字符串的一部分:
for k in stuff.keys():
if k in fname:
print('found', k)
一些其他注意事项:
KeyError 会更容易被注意到...如果您取出 try/except
块。向最终用户隐藏 python 错误可能很有用。向您隐藏该信息是个坏主意 - 特别是当您在代码中调试初始传递时。
您可以比较列表或元组中的最后一项:
if hollow == things[-1]:
如果那是你想要做的。
在你的最后一个循环中:empty == str(i)
需要是 empty = str(i)
。
我在将工作代码从列表转换为字典时遇到问题。代码的基础检查列表中任何关键字的文件名。
但是我很难理解字典来转换它。我正在尝试提取每个键的名称并将其与文件名进行比较,就像我对列表和元组所做的那样。这是我正在做的模拟版本。
fname = "../crazyfdsfd/fds/ss/rabbit.txt"
hollow = "SFV"
blank = "2008"
empty = "bender"
# things is list
things = ["sheep", "goat", "rabbit"]
# other is tuple
other = ("sheep", "goat", "rabbit")
#stuff is dictionary
stuff = {"sheep": 2, "goat": 5, "rabbit": 6}
try:
print(type(things), "things")
for i in things:
if i in fname:
hollow = str(i)
print(hollow)
if hollow == things[2]:
print("PERFECT")
except:
print("c-c-c-combo breaker")
print("\n \n")
try:
print(type(other), "other")
for i in other:
if i in fname:
blank = str(i)
print(blank)
if blank == other[2]:
print("Yes. You. Can.")
except:
print("THANKS OBAMA")
print("\n \n")
try:
print(type(stuff), "stuff")
for i in stuff: # problem loop
if i in fname:
empty = str(i)
print(empty)
if empty == stuff[2]: # problem line
print("Shut up and take my money!")
except:
print("CURSE YOU ZOIDBERG!")
虽然前两个示例我能够获得完整的 运行,但我无法无一例外地获得 运行 的字典。循环不会将空值转换为 stuff[2] 的值。遗憾地将钱留在弗莱的口袋里。让我知道我的例子是否对我的要求不够清楚。字典只是对计数列表和向其他变量添加文件的快捷方式。
A dictionary 是一个将键映射到值的无序集合。如果将 stuff
定义为:
stuff = {"sheep": 2, "goat": 5, "rabbit": 6}
您可以通过以下方式引用其元素:
stuff['sheep'], stuff['goat'], stuff['rabbit']
stuff[2]
将导致 KeyError,因为在您的字典中找不到键 2
。您不能将字符串与字典的最后一个或第三个值进行比较,因为字典未按有序序列存储(内部排序基于散列)。对有序序列使用列表或元组 - 如果您需要与最后一项进行比较。
如果要遍历字典,可以用这个作为模板:
for k, v in stuff.items():
if k == 'rabbit':
# do something - k will be 'rabbit' and v will be 6
如果您想检查字典中的键以查看它们是否匹配字符串的一部分:
for k in stuff.keys():
if k in fname:
print('found', k)
一些其他注意事项:
KeyError 会更容易被注意到...如果您取出 try/except
块。向最终用户隐藏 python 错误可能很有用。向您隐藏该信息是个坏主意 - 特别是当您在代码中调试初始传递时。
您可以比较列表或元组中的最后一项:
if hollow == things[-1]:
如果那是你想要做的。
在你的最后一个循环中:empty == str(i)
需要是 empty = str(i)
。