我有一个字典,其中字母对应于数字和字符串,并且如果每个字母出现在字符串中的次数等于或等于少于字典中与该字母相关的数字。当这个问题不那么复杂并且没有计算事件发生的限制时,我使用all(x in string for x in dictionary)
。有没有类似的简洁的方式来测试这个基于字典中的int值?确定是否可以从另一个字符串中的字符子集创建一个字符串
编辑:道歉,这是我们正在看。如果我现在需要它,除非该字母出现的次数是<返回False 字符串中的字母任何实例都显示为手,一本字典关键
def isValidWord(word, hand, wordList):
"""
Returns True if word is in the wordList and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or wordList.
word: string
hand: dictionary (string -> int)
wordList: list of lowercase strings
"""
if word.lower() in wordList and all(x in hand for x in word):
return True
else:
return False
此代码返回True =的该键的整数值。我已经非常混乱地完成了这个任务,并且想知道是否有办法将这种特定级别的特征合并到all
方法或类似的简洁方法中。
请向我们提供样本输入和预期输出。你的问题还不够完整,无法给出明确的答案。 – abccd
请发布您的一些数据,以及您期望从中获得什么? –
对不起,有一个带有docstring的代码块 – Max