我写了一个简单的Python程序:
#!/usr/bin/env python3.5
import sys, itertools
sCharacters = '123'
for iCombinationLength in range(0, len(sCharacters)+1):
for aCombination in itertools.combinations_with_replacement(sCharacters, iCombinationLength):
print(''.join(aCombination))
Run Code Online (Sandbox Code Playgroud)
输出如下:
1
2
3
11
12
13
22
23
33
111
112
113
122
123
133
222
223
233
333
Run Code Online (Sandbox Code Playgroud)
然而,如果它是数字 1、2 和 3 的所有组合,则需要包括:
311
312
313
321
322
323
331
332
333
Run Code Online (Sandbox Code Playgroud)
正如您在上面看到的,事实并非如此。我看过其他帖子给出的 Combinations_with_replacement 函数作为解决方案来获取传入的字符的所有可能组合。但这似乎并没有发生。我在这里做错了什么,如何获得字符变量中字符的所有可能组合?
谢谢你的时间 ;-)
编辑:user2357112 正确地将我的问题标记为重复。链接的答案适用于我的问题。
我将保留这个问题,因为我认为它的措辞与链接的问题不同,并且可能会帮助其他人到达正确的位置。
我想对列表中所有索引之间进行排列。
例如,对于列表 ['a', 'b', 'c'],我想迭代索引i,j以便可以将 'a' 与 'a'、'b'、'c' 等进行比较。
这基本上只是两个嵌套的 for 循环,因此itertools 的产品很好地实现了这一点。
但它所做的工作基本上是我需要的工作的两倍。我只需要上三角 (i, j) 对(因此,无序对 -例如,如果迭代 (0,1),则不需要 (1,0))。
我认为这肯定是一个已经回答的问题,但我找不到它。您能帮忙回答吗?或者,如果这是重复的,请为我指出正确的方向?谢谢!
我拥有的:
from itertools import product
exList = ['a', 'b', 'c']
for i,j in product(range(len(exList)), range(len(exList))):
print([i,j])
---
Out:
[0, 0]
[0, 1]
[0, 2]
[1, 0]
[1, 1]
[1, 2]
[2, 0]
[2, 1]
[2, 2]
Run Code Online (Sandbox Code Playgroud)
但这是我需要的计算量的两倍,[0, 1]并且[1, 0]是多余的。
所以 itertools 的输出是一个有序对。我想要一个无序的对——就像一个三角矩阵。
我想要什么:
from …Run Code Online (Sandbox Code Playgroud) 我正在使用以下函数pairwise来获取有序对的迭代。例如,如果可迭代对象是一个列表[1, 2, 3, 4, 5, 6],那么我想获取(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1). 如果我使用以下函数
from itertools import tee, zip_longest
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip_longest(a, b)
Run Code Online (Sandbox Code Playgroud)
然后它返回(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, None)。
我在代码中使用dataloader可迭代,因此我只想iterable作为函数的输入传递pairwise,并且不想传递额外的输入。
如何获取第一个元素作为上面提到的最后一项中的最后一个元素?
我有以下代码来创建一个包含多个带有整数对的元组的元组:
iterable = (
tuple(zip([0, 1, 2], _))
for _ in product(range(9), repeat=3)
)
next(iterable) # First element is not needed
print(list(iterable))
# This code produces: [((0, 0), (1, 0), (2, 1)), ... , ((0, 8), (1, 8), (2, 8))]
Run Code Online (Sandbox Code Playgroud)
但我需要,如果元组的最后一个元素是“0”(例如(0, 0)或(2, 0)),我必须删除该元组。所以新的列表应该是这样的:
[((2, 1),), ... , ((1, 2), (2, 7)), ((1, 2), (2, 8)), ... , ((0, 8), (1, 8), (2, 8))]
Run Code Online (Sandbox Code Playgroud)
我实际上通过以下代码实现了这个目标,但这不是我认为的正确方法,我不知道:
x = ()
for i in iterable:
y = ()
for j in i:
if j[-1] …Run Code Online (Sandbox Code Playgroud) 我需要在Python中生成大量的列表.每个列表的长度为13,我有4个可能的值可以进入每个元素.这些是[1,-1,i,-i],但它可以是任何东西.
因此,考虑到主题中的信息,我应该得到4*4*4 ...*4 = 4 ^ 13 = 67,108,864个列表,或者更一般地,m ^ n.
我在Python的itertools中尝试了combination_with_replacement方法,但是使用以下代码我只得到560个结果.
c = it.combinations_with_replacement([1,-1,np.complex(0,1), np.complex(0,-1)], 13)
print list(c)
Run Code Online (Sandbox Code Playgroud)
我知道组合不关心顺序,所以这个结果可能是正确的.但是,当我使用排列方法时,我只能选择第二个参数<=第一个参数中的元素个数.
知道怎么做到这一点?
谢谢!
我想在Python中迭代+枚举两个列表.以下代码看起来很丑陋.有没有更好的解决方案?
for id, elements in enumerate(itertools.izip(as, bs)):
a = elements[0]
b = elements[1]
# do something with id, a and b
Run Code Online (Sandbox Code Playgroud)
谢谢.
Python有很好的模块用于处理名为itertools的迭代器吗?Perl中是否有任何模拟?
我知道Object- Iterate但它只有imap和igrep.
如何将字符串字典转换为列表到将字符串映射到这些列表中的值的字典列表?例如,以下字典
{'a': [1,2],
'b': ['x', 'y', 'z']}
Run Code Online (Sandbox Code Playgroud)
将被转换为以下列表
[{'a': 1, 'b': 'x'}, {'a': 1, 'b': 'y'}, {'a': 1, 'b': 'z'},
{'a': 2, 'b': 'x'}, {'a': 2, 'b': 'y'}, {'a': 2, 'b': 'z'},]
Run Code Online (Sandbox Code Playgroud) 我有这样的字典 -
{'A': 0, 'B': 0, 'C': 0, 'D': 4}
Run Code Online (Sandbox Code Playgroud)
我想生成一个这样的列表 -
[{'A': 1, 'B': 0, 'C': 0, 'D': 4},
{'A': 0, 'B': 1, 'C': 0, 'D': 4},
{'A': 0, 'B': 0, 'C': 1, 'D': 4},
{'A': 0, 'B': 0, 'C': 0, 'D': 5}]
Run Code Online (Sandbox Code Playgroud)
什么是最pythonic的方式来做到这一点?
我使用itertools到group by一个dictionary key使用如下:
host_data = []
for k,v in itertools.groupby(temp_data, key=lambda x:x['device_id'])
d = {}
for dct in v:
d.update(dct)
host_data.append(d)
Run Code Online (Sandbox Code Playgroud)
但是,如果可能的话,我想按“ device_id”和“ port_id”进行分组,如何为该分组添加其他密钥?
python ×10
python-itertools ×10
combinations ×2
permutation ×2
enumerate ×1
iterator ×1
perl ×1
python-3.x ×1