当两个列表中都有重复元素时,列表理解

发布于 2025-01-30 01:59:41 字数 655 浏览 3 评论 0 原文

我想找到所有具有一组特定目标字母的单词。当目标中的字母没有重复时,我的代码工作起来,但如果有重复。

这是我的代码:

target = 'bba'
target_words = [
  'aani', 'aaru', 'abac', 'abas', 
  'abba', 'abby', 'abed', 'abel', 'abet'
]
target_list = list(target)


final_list = []
for word in target_words:
    word_list = list(word)
    if (all(x in word_list for x in target_list)):
        final_list.append(word)
        
print('Final list: ', final_list)

输出是最终列表:['abac','abas','abba','abby','abed','abed','abel','abet']

我想要它为最终列表:['abba','abby']

我找不到一种方法来获得我想要的结果。这可能是因为我将单词字母转换为列表,但我看不到该怎么做。

I want to find all words that have a particular set of target letters. My code works when none of the letters in the target is repeated, but not if there are repeats.

Here is my code:

target = 'bba'
target_words = [
  'aani', 'aaru', 'abac', 'abas', 
  'abba', 'abby', 'abed', 'abel', 'abet'
]
target_list = list(target)


final_list = []
for word in target_words:
    word_list = list(word)
    if (all(x in word_list for x in target_list)):
        final_list.append(word)
        
print('Final list: ', final_list)

The output is Final list: ['abac', 'abas', 'abba', 'abby', 'abed', 'abel', 'abet']

I would like it to be Final list: ['abba', 'abby']

I cannot find a way to get the result I want. It may be because I am converting the letters of the words into a list, but I don't see how to do it otherwise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2025-02-06 01:59:41

这是 collections.counter()的好用例。它为我们提供了一种干净的方法来检查 target_words target 的单词之间是否存在所需的重叠,请考虑重复的字母(并提出了 azro ):

from collections import Counter

target_counter = Counter(target)
[word for word in target_words if Counter(word) & target_counter == target_counter]

此输出:

['abba', 'abby']

This is a good use case for collections.Counter(). It gives us a clean way to check whether the desired overlap exists between a word in target_words and the target, accounting for duplicate letters (with a suggested improvement by azro):

from collections import Counter

target_counter = Counter(target)
[word for word in target_words if Counter(word) & target_counter == target_counter]

This outputs:

['abba', 'abby']
怕倦 2025-02-06 01:59:41

首先,让我们在目标词中找到独特的字母。

target_word_letters = set(target)

现在,我们可以使用词典理解来获取每个字母的计数。

counts = {letter: target.count(letter) for letter in target_word_letters}

现在,我们需要一个列表理解,该列表可以通过 target_words 进行迭代,并检查计数中的每个字母中的每个字母,该字母的相同数字(或更多)在目标单词中。

[word for word in target_words if all(word.count(l) >= c for l, c in counts.items())]

结果:

['abba', 'abby']

First let's find the unique letters in your target word.

target_word_letters = set(target)

Now, we can use a dictionary comprehension to get a count of each letter.

counts = {letter: target.count(letter) for letter in target_word_letters}

Now we need a list comprehension that iterates over target_words and checks that for every letter in counts, the same number (or more) of that letter is in the target word.

[word for word in target_words if all(word.count(l) >= c for l, c in counts.items())]

Result:

['abba', 'abby']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文