itertools.product() 的独特共现对
Unique co-occurrence pairs with itertools.product()
我有一个单词列表,例如:
[man, walk, ball]
并且我希望产生它们的共现;即:
[('man', 'walk'), ('man', 'ball'), ('walk', 'ball')]
我使用以下代码:
from itertools import product
my_list = [man, walk, ball]
list(product(my_list, my_list))
这给了我:
[('man', 'man'), ('man', 'walk'), ('man', 'ball'), ('walk', 'man'), ('walk', 'walk'), ('walk', 'ball'), ('ball', 'man'), ('ball', 'walk'), ('ball', 'ball')]
我想知道如何省略重复的对?
尝试 itertools.combinations(iterable, r)
:
>>> import itertools
>>> list(itertools.combinations(['man', 'walk', 'ball'], 2))
[('man', 'walk'), ('man', 'ball'), ('walk', 'ball')]
我有一个单词列表,例如:
[man, walk, ball]
并且我希望产生它们的共现;即:
[('man', 'walk'), ('man', 'ball'), ('walk', 'ball')]
我使用以下代码:
from itertools import product
my_list = [man, walk, ball]
list(product(my_list, my_list))
这给了我:
[('man', 'man'), ('man', 'walk'), ('man', 'ball'), ('walk', 'man'), ('walk', 'walk'), ('walk', 'ball'), ('ball', 'man'), ('ball', 'walk'), ('ball', 'ball')]
我想知道如何省略重复的对?
尝试 itertools.combinations(iterable, r)
:
>>> import itertools
>>> list(itertools.combinations(['man', 'walk', 'ball'], 2))
[('man', 'walk'), ('man', 'ball'), ('walk', 'ball')]