# put all characters of first list into a dictionary
# object. This is easier to use for looking up characters
# later
list_chars = {}
for string in list1:
for char in string:
list_chars[char] = True
# run through second list, for each character
# in the other list, check if it exists in
# the dictionary of characters from the first
# list. If it does not, then set missing_character to True.
missing_character = False
for string in list2:
for char in string:
if not list_chars.get(char,False):
# means that the first list does
# not contain the character char
missing_character = True
# print whether one list was actually missing a character.
if missing_character:
print('Missing some character!')
else
print('Contained all characters!')
我认为内置的
join
函数可以提供一个有效的解决方案:这样做怎么样:
list2
中所有唯一字符的列表,将其称为charlist
list1
,如果list2
中的单词中有任何字符不在charlist
中,请将它们分开。在对于第1部分:
^{} 是不允许重复的特殊类型;每个项都必须是唯一的。在
第2部分:
^{pr2}$使用一个list comprehension,然后计算结果的长度,我们可以找出}的单词。在
charlist
中有多少个字母不是来自{我不认为有一个内置函数可以处理这个问题。你能做的就是
如果上面的某些部分没有意义,请随时提出后续问题。另外,如果使用上面的break语句,可以使上面的代码更快一些。(如果您已经知道列表中缺少一个字符,请尽早退出for循环。)我将留给您进行分析并确定您是否感兴趣。在
相关问题 更多 >
编程相关推荐