from itertools import permutations
print(list(permutations('000000000111111111', r=18)))
Run Code Online (Sandbox Code Playgroud)
我假设上面的代码有效,但每个0and1都被视为一个单独的符号,因此对于每个排序,我都会得到大量重复:
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', …Run Code Online (Sandbox Code Playgroud) python combinations combinatorics python-itertools python-3.x
我似乎无法找到一种优雅的方式从t开始并导致s.
>>>t = ['a',2,'b',3,'c',4]
#magic
>>>print s
{'a': 2, 'c': 4, 'b': 3}
Run Code Online (Sandbox Code Playgroud)
我提出的解决方案看起来不那么优雅:
s = dict()
for i in xrange(0, len(t),2): s[t[i]]=t[i+1]
# or something fancy with slices that I haven't figured out yet
Run Code Online (Sandbox Code Playgroud)
它显然很容易解决,但是,似乎还有更好的方法.在那儿?
我列出了4个清单:
list1, list2, list3, list4 = range(0,4), range(4,9), range(8,15), range(2,16)
# just using range() generate sample data
Run Code Online (Sandbox Code Playgroud)
我可以list1_item, list2_item, list3_item, list4_item通过以下代码按顺序列出所有项目组合:
for item1 in list1:
for item2 in list2:
for item3 in list3:
for item4 in list4:
print [item1, item2, item3, item4]
Run Code Online (Sandbox Code Playgroud)
我可以知道我是否可以通过itertools模块完成这项工作?
谢谢
我有一个列表,我想将此列表转换为地图
mylist = ["a",1,"b",2,"c",3]
Run Code Online (Sandbox Code Playgroud)
mylist相当于
mylist = [Key,Value,Key,Value,Key,Value]
Run Code Online (Sandbox Code Playgroud)
所以输入:
mylist = ["a",1,"b",2,"c",3]
Run Code Online (Sandbox Code Playgroud)
输出:
mymap = {"a":1,"b":2,"c":3}
Run Code Online (Sandbox Code Playgroud)
PS:我已经编写了以下函数来完成同样的工作,但我想使用python的迭代器工具:
def fun():
mylist = ["a",1,"b",2,"c",3]
mymap={}
count = 0
for value in mylist:
if not count%2:
mymap[value] = mylist[count+1]
count = count+1
return mymap
Run Code Online (Sandbox Code Playgroud) 我有这种格式的项目列表:
['2', '', '9', '24', '1', '7', '12', '4', '', '18', '13', '34', '3', '10']
Run Code Online (Sandbox Code Playgroud)
我需要把它变成由空项分隔的列表列表:
[['2'], ['9', '24', '1', '7', '12', '4'], ['18', '13', '34', '3', '10']]
Run Code Online (Sandbox Code Playgroud)
我只是不确定该怎么做.我确实尝试过做一个嵌套循环但是没有按计划工作.
我发现有一种方法可以使用itertools.groupby来完成它,但我无法弄清楚它的语法.
我感谢所有的帮助!谢谢!
我有一些这样的字典
d = {'a' : ['1', '+', '='], 'b' : ['2'], c : ['3']}
Run Code Online (Sandbox Code Playgroud)
我的目标是通过替换其中的元素,从初始单词创建所有可能衍生物的列表:
word = 'abcde'
rez = ['abcde',
'1bcde', '+bcde', '=bcde',
'a2cde', '12cde', '+2cde', '=2cde',
'ab3de', '1b3de', '+b3de', '=b3de', 'a23de', '123de', '+23de', '=23de']
Run Code Online (Sandbox Code Playgroud)
单词顺序rez并不重要.
我觉得应该有一些简单的方法itertools,但我看不到它.任何漂亮和美丽的解决方案都将非常受欢迎.
我正在调试编写代码的一部分,因此我使用大量调用print来检查变量的值.我遇到过一种情况,当打印变量导致Python跳过for循环时.
import numpy as np
import itertools as itr
(...)
class myclass():
def a_function_inside_a_class():
print('-------------')
current_size = self.get_current_size()
current_size[dimension] += 1
new_layer = [range(i) for i in current_size]
new_layer[dimension] = [current_size[dimension]]
print(new_layer)
new_layer = itr.product(*new_layer)
print(list(new_layer)) # THIS LINE CAUSES PROBLEMS
for c in new_layer:
print('for')
print(list(c))
(...)
a_function_that_causes_some_error(c)
Run Code Online (Sandbox Code Playgroud)
我创建一个列表,然后用于itertools创建此列表的组合,然后迭代它们.如果我打电话给a_function_inside_a_class()上面,我没有for打印.没有错误发生.解释器不进入循环.
(...)
-------------
[[2], range(0, 1), range(0, 1)]
[(2, 0, 0)]
-------------
[range(0, 1), [2], range(0, 1)]
[(0, 2, …Run Code Online (Sandbox Code Playgroud) 用python计算笛卡儿积非常简单.只需要使用 itertools.product
>>> source = [['a', 'b', 'c'], [1, 2, 3]]
>>> list(itertools.product(*source))
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]
Run Code Online (Sandbox Code Playgroud)
但我找不到相反的操作.如何[['a', 'b', 'c'], [1, 2, 3]]从产品的结果中找到来源.有谁知道通用解决方案?
我很感激任何建议.
list_a = []
for color in [True,False]:
for piece in range(1,7):
list_a = list_a + function(piece,color)
Run Code Online (Sandbox Code Playgroud)
这里function(piece,color)返回一个列表,我想加入并最终返回长列表,可以itertools.chain在这里使用吗?因为我觉得它可能会更快.我只显示一个示例,但在我的实际代码中,循环运行大约100,000次,这就是为什么我在寻找更快的方法.
我有两个清单[1,2,3]和[4,5,6].我想使用以下生成所有组合的列表itertools
ResultingList = [[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]
到目前为止,我只调查了itertools.combinations函数,它似乎只能处理这样的事情:
list(itertools.combinations([1,2,3,4,5,6],2))
Run Code Online (Sandbox Code Playgroud)
哪个输出不正确的结果.我怎样才能生成ResultingList以上内容?
谢谢
python ×10
python-itertools ×10
list ×3
combinations ×2
dictionary ×2
python-3.x ×2
for-loop ×1
loops ×1
string ×1