嗨,我正在尝试输入一个字符串,然后将字符串拆分为单个单词。字符串中以及“内容”的词典关键字中的唯一词从词典“文件”中检索相应的值。
如何拆分输入字符串以对照字典“概念”键检查单个单词,并在可能的情况下返回字符串中的单词,而不返回字典键?
我试图将字符串拆分成一个列表,然后将列表值直接传递到字典中,但是我很快就迷路了(那些是顶部注释掉的变量。感谢您的帮助。谢谢。
def concept(word):
# convert var(word) to list
#my_string_list=[str(i) for i in word]
# join list(my_string_list) back to string
#mystring = ''.join(my_string_list)
# use this to list python files
files = {1:"file0001.txt",
2:"file0002.txt",
3:"file0003.txt",
4:"file0004.txt",
5:"file0005.txt",
6:"file0006.txt",
7:"file0007.txt",
8:"file0008.txt",
9:"file0009.txt"}
# change keys to searchable simple keyword phrases.
concepts = {'GAMES':[1,2,4,3,3],
'BLACKJACK':[5,3,5,3,5],
'MACHINE':[4,9,9,9,4],
'DATABASE':[5,3,3,3,5],
'LEARNING':[4,9,4,9,4]}
# convert to uppercase, search var(mystring) in dict 'concepts', if not found return not found"
if word.upper() not in concepts:
print("{}: Not Found in Database" .format(word)) not in concepts
return
# for matching keys in dict 'concept' list values in dict 'files'
for pattern in concepts[word.upper()]:
print(files[pattern])
# return input box at end of query
while True:
concept(input("Enter Concept Idea: "))
print("\n")
参考方案
假设输入是用空格分隔的单词列表,则可以执行以下操作:
def concept(phrase):
words = phrase.split()
# use this to list python files
files = {1: "file0001.txt",
2: "file0002.txt",
3: "file0003.txt",
4: "file0004.txt",
5: "file0005.txt",
6: "file0006.txt",
7: "file0007.txt",
8: "file0008.txt",
9: "file0009.txt"}
# change keys to searchable simple keyword phrases.
concepts = {'GAMES': [1, 2, 4, 3, 3],
'BLACKJACK': [5, 3, 5, 3, 5],
'MACHINE': [4, 9, 9, 9, 4],
'DATABASE': [5, 3, 3, 3, 5],
'LEARNING': [4, 9, 4, 9, 4]}
for word in words:
# convert to uppercase, search var(mystring) in dict 'concepts', if not found return not found"
if word.upper() not in concepts:
print("{}: Not Found in Database".format(word))
else:
# for matching keys in dict 'concept' list values in dict 'files'
for pattern in concepts[word.upper()]:
print(files[pattern])
concept("games blackjack foo")
输出
file0001.txt
file0002.txt
file0004.txt
file0003.txt
file0003.txt
file0005.txt
file0003.txt
file0005.txt
file0003.txt
file0005.txt
foo: Not Found in Database
words = phrase.split()
行在空格处分割了字符串短语。要检查词典中是否有单词,您需要一次执行一个操作,因此循环for word in words
遍历短语单词。
更多
在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…
Python uuid4,如何限制唯一字符的长度 - python在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…
无法注释掉涉及多行字符串的代码 - python基本上,我很好奇这为什么会引发语法错误,以及如何用Python的方式来“注释掉”我未使用的代码部分,例如在调试会话期间。''' def foo(): '''does nothing''' ''' 参考方案 您可以使用三重双引号注释掉三重单引…
Python:BeautifulSoup-根据名称属性获取属性值 - python我想根据属性名称打印属性值,例如<META NAME="City" content="Austin"> 我想做这样的事情soup = BeautifulSoup(f) //f is some HTML containing the above meta tag for meta_tag in soup(…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…