Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较元组列表,根据条件确认子集?_Python_String_Python 3.x_Counter - Fatal编程技术网

Python 比较元组列表,根据条件确认子集?

Python 比较元组列表,根据条件确认子集?,python,string,python-3.x,counter,Python,String,Python 3.x,Counter,给定一个单词和一个单词列表,我必须找到可以使用给定单词的字母(字母数量)构建的列表元素/单词。我曾尝试使用集合中的Counter对象和python 2.7的cmp()函数的定义(我使用的是3.6.5) 我后来意识到,对于这样的问题,这种方法似乎是一种不好的做法(早些时候,我试图使用计数器对象字典进行比较)。我的程序不工作的原因是因为compare_fn依赖于“>”,我认为使用计数器是一个不错的选择。不要把它们列成清单。 我特意返回了[True,False,False],而不是[1,-1,-1],

给定一个单词和一个单词列表,我必须找到可以使用给定单词的字母(字母数量)构建的列表元素/单词。我曾尝试使用集合中的
Counter
对象和python 2.7的
cmp()
函数的定义(我使用的是3.6.5)


我后来意识到,对于这样的问题,这种方法似乎是一种不好的做法(早些时候,我试图使用计数器对象字典进行比较)。我的程序不工作的原因是因为compare_fn依赖于“>”,我认为使用计数器是一个不错的选择。不要把它们列成清单。 我特意返回了[True,False,False],而不是[1,-1,-1],但是您可以很容易地更改它

此外:我使用了列表理解而不是映射,因为它在python中更流行,但语义是相同的

from collections import Counter
word = 'ravenous'
candidates = ["raven", "mealwheel", "rasputin"]

def count_fn(mystr):
    return Counter(mystr)

def compare_fn (c1,c2):
    return all(c1[char] >= count for char, count in c2.items())

counter_word =  count_fn(word)
list_candidates = [count_fn(candidate) for candidate in candidates]
cmp_list = [compare_fn(counter_word, i) for i in list_candidates]
print(cmp_list)

我认为使用计数器是一个不错的选择。不要把它们列成清单。 我特意返回了[True,False,False],而不是[1,-1,-1],但是您可以很容易地更改它

此外:我使用了列表理解而不是映射,因为它在python中更流行,但语义是相同的

from collections import Counter
word = 'ravenous'
candidates = ["raven", "mealwheel", "rasputin"]

def count_fn(mystr):
    return Counter(mystr)

def compare_fn (c1,c2):
    return all(c1[char] >= count for char, count in c2.items())

counter_word =  count_fn(word)
list_candidates = [count_fn(candidate) for candidate in candidates]
cmp_list = [compare_fn(counter_word, i) for i in list_candidates]
print(cmp_list)

发生了什么事
('a',1)
列表中,单词
('a',2)
列表中,候选人
?相关问题:@BcK,这将是错误的。示例-我不能从“生锈”中生成“生锈”(需要两个s);但是我可以从“Buu”(一个额外的U)中生成“Ub”
('a',1)
列表单词中,而
('a',2)
列表候选词中。相关问题:@BcK,这将是错误的。示例-我不能从“生锈”中生成“生锈”(需要两个s);但是我可以从'Buu'(一个额外的U)生成'Ub',你也可以将
compare_fn
定义为
c2-c1=={}
@aranfey,我使用了帮助(计数器)并在文档中找到了c2-c1。据我所知,如果计数器元素是精确匹配的,则结果不会显示。“差分剥离所有负计数和零计数”。因此,如果所比较的单词是字谜,那么结果中就不会有。我之前想问的是,如何在c2-c1中返回零计数实例。我哪里出错了?@Aran Fey from help(collections.Counter)->>>Counter('bccd')-Counter('bccd')| Counter()//空计数器是如何变为真的?@Poppinyoshi
Counter
是一个dict子类,因此将空计数器与空dict进行比较会返回
True
。但是您必须使用
=
,而不是
|
。您也可以将
比较fn
定义为
c2-c1=={}
@aranfey,我使用了帮助(计数器)并在文档中遇到了c2-c1。据我所知,如果计数器元素是精确匹配的,则结果不会显示。“差分剥离所有负计数和零计数”。因此,如果所比较的单词是字谜,那么结果中就不会有。我之前想问的是,如何在c2-c1中返回零计数实例。我哪里出错了?@Aran Fey from help(collections.Counter)->>>Counter('bccd')-Counter('bccd')| Counter()//空计数器是如何变为真的?@Poppinyoshi
Counter
是一个dict子类,因此将空计数器与空dict进行比较会返回
True
。但是您必须使用
==
,而不是
from collections import Counter
word = 'ravenous'
candidates = ["raven", "mealwheel", "rasputin"]

def count_fn(mystr):
    return Counter(mystr)

def compare_fn (c1,c2):
    return all(c1[char] >= count for char, count in c2.items())

counter_word =  count_fn(word)
list_candidates = [count_fn(candidate) for candidate in candidates]
cmp_list = [compare_fn(counter_word, i) for i in list_candidates]
print(cmp_list)