我写了一个程序,试图在一个字母列表(letterList)中找到一系列字母(toBeFound - 这些字母代表一个单词),但它拒绝承认当前的3个字母系列,因为它算上“我”。在第一个列表中两次,将其添加到重复列表中。
目前此代码返回"不正确",何时返回"更正"。
letterList= ['F','I', 'I', 'X', 'O', 'R', 'E']
toBeFound = ['F', 'I', 'X']
List = []
for i in toBeFound[:]:
for l in letterList[:]:
if l== i:
letterList.remove(l)
List.append(i)
if List == toBeFound:
print("Correct.")
else:
print("Incorrect.")
letterList和toBeFound是样本值,每个字母可以是任何字母。我无法设法迭代代码并成功确保忽略重复项。任何帮助将不胜感激!
答案 0 :(得分:0)
基本上,您正在查看def copy(nums):
size = len(nums)
nums_a = np.array(nums)
nums_c = nums_a.ctypes.data_as(INT_POINTER)
vector = _lib.copy_vec(nums_c, size)
return vector
是toBeFound
的子集,对吗?
这是使用letterList
s:
set
(BTW,[3]和[4]是同一运算符的不同符号。)
答案 1 :(得分:0)
我认为这可以解决您的问题。请尝试一下,让我知道
letterList= ['F','I', 'I', 'X', 'O', 'R', 'E']
toBeFound = ['F', 'I', 'X']
found_list = [i for i in toBeFound if i in letterList]
print("Correct" if toBeFound == found_list else "Incorrect")
答案 2 :(得分:0)
您可以将初始列表设为一组,但如果您想要查找“' hello'它不会起作用,因为你需要两个人。
解决此问题的一种方法是使用字典来检查我们到目前为止的工作情况。
letterList = ['H', 'E', 'L', 'X', 'L', 'I', 'O']
toBeFound = ['H', 'E', 'L', 'L', 'O']
# build dictionary to hold our desired letters and their counts
toBeFoundDict = {}
for i in toBeFound:
if i in toBeFoundDict:
toBeFoundDict[i] += 1
else:
toBeFoundDict[i] = 1
letterListDict = {} # dictionary that holds values from input
output_list = [] # dont use list its a reserved word
for letter in letterList:
if letter in letterListDict: # already in dictionary
# if we dont have too many of the letter add it
if letterListDict[letter] < toBeFoundDict[letter]:
output_list.append(letter)
# update the dictionary
letterListDict[letter] += 1
else: # not in dictionary so lets add it
letterListDict[letter] = 1
if letter in toBeFoundDict:
output_list.append(letter)
if output_list == toBeFound:
print('Success')
else:
print('fail')
答案 3 :(得分:0)
这个怎么样:(我在python3.6中测试过)
import collections
letterList= ['F','I', 'I', 'X', 'O', 'R', 'E']
toBeFound = ['F', 'I', 'X']
collections.Counter(letterList)
a=collections.Counter(letterList) # print(a) does not show order
# but a.keys() has order preserved
final = [i for i in a.keys() if i in toBeFound]
if final == toBeFound:
print("Correct")
else:
print("Incorrect")
答案 4 :(得分:0)
如果您正在查看letterList是否按指定的顺序包含toBeFound的字母并忽略重复的字母,这将是旧的&#34;文件匹配的简单变体&#34;算法。您可以在非破坏性函数中实现它,如下所示:
def letterMatch(letterList,toBeFound):
i= 0
for letter in letterList:
if letter == toBeFound[i] : i += 1
elif i > 0 and letter != toBeFound[i-1] : break
if i == len(toBeFound) : return True
return False
letterMatch(['F','I', 'I', 'X', 'O', 'R', 'E'],['F', 'I', 'X'])
# returns True
另一方面,如果您正在寻找的是测试letterList是否具有形成toBeFound所需的所有字母(以任何顺序),那么逻辑就会简单得多,因为您只需要&#34;退房&#34; toBeFound的字母使用letterList:
中的字母def lettermatch(letterList,toBeFound):
missing = toBeFound.copy()
for letter in letterList:
if letter in missing : missing.remove(letter)
return not missing
答案 5 :(得分:-1)
按要求。
d = {'key1':1, 'key2':2, 'key3':3}
included_keys = ['key1', 'key2', 'key99']
{k:v for k,v in d.items() if k in included_keys}
这打印正确。我把letterList设为一套!希望它有所帮助。
答案 6 :(得分:-2)
一种简单的方法是遍历toBeFound
,并在letterList
中查找每个元素。
letterList= ['F','I', 'I', 'X', 'O', 'R', 'E']
toBeFound = ['F', 'I', 'X']
found = False
for x in letterList:
if x not in toBeFound:
found = False
break
if found:
print("Correct.")
else:
print("Incorrect.")