我正在尝试在python中生成morris序列.我目前的解决方案如下,但我觉得我只是在python中写了c.任何人都可以提供更多的pythonic解决方案吗?
def morris(x):
a = ['1', '11']
yield a[0]
yield a[1]
while len(a) <= x:
s = ''
count = 1
al = a[-1]
for i in range(0,len(al)):
if i+1 < len(al) and al[i] == al[i+1]:
count += 1
else:
s += '%s%s' % (count, al[i])
count = 1
a.append(s)
yield s
a = [i for i in morris(30)]
Run Code Online (Sandbox Code Playgroud) 我不需要itertools.groupby的懒惰.我只是想把我的列表分成一个列表的字典:
dict([(a, list(b)) for a,b in itertools.groupby(mylist, mykeyfunc)])
Run Code Online (Sandbox Code Playgroud)
是否有标准功能已经这样做了?
我有一个单词列表,我需要生成所有这些可能的排列,但需要注意一点.
我目前使用以下代码:
from itertools import permutations
wordlist = ["word1", "word2", "word3"]
for perm in permutations(wordlist):
print "".join(perm)
Run Code Online (Sandbox Code Playgroud)
它给出了输出:
word1word2word3
word1word3word2
...
word3word2word1
Run Code Online (Sandbox Code Playgroud)
但是我还需要它来打印这些单词的子集,例如:
word1
word1word2
word2word1
...
Run Code Online (Sandbox Code Playgroud)
但我对如何做到这一点没有丝毫想法.我该从哪里开始?我该怎么读?
我可以粗略地编写代码,但有一些直观的方法使用列表理解或itertools等?
而且,如果给出数字是如何做的数字是k数字而不是3?
我想测试一个有序集是否是一个更大的有序集的子集.我使用了元组和itertools.combinations:
def subset_test(a, b):
return a in itertools.combinations(b, len(a))
Run Code Online (Sandbox Code Playgroud)
例如,
>>> subset_test((0, 1, 2), (0, 3, 1, 4, 2))
True
>>> subset_test((0, 1, 2), (0, 3, 2, 4, 1))
False
Run Code Online (Sandbox Code Playgroud)
它有效,但是当我测试大元组时它很慢.
我有一个列表,可以包含Nones和datetime对象.我需要在连续datetime对象的子列表中拆分它,并需要datetime在原始列表中记录该子列表的第一个对象的索引.
例如,我需要能够转向
original = [None, datetime(2013, 6, 4), datetime(2014, 5, 12), None, None, datetime(2012, 5, 18), None]
Run Code Online (Sandbox Code Playgroud)
成:
(1, [datetime.datetime(2013, 6, 4, 0, 0), datetime.datetime(2014, 5, 12, 0, 0)])
(5, [datetime.datetime(2012, 5, 18, 0, 0)])
Run Code Online (Sandbox Code Playgroud)
我尝试了两种方法.一个使用find:
binary = ''.join('1' if d else '0' for d in original)
end = 0
start = binary.find('1', end)
while start > -1:
end = binary.find('0', start)
if end < 0:
end = len(binary)
dates …Run Code Online (Sandbox Code Playgroud) 我试图找到字符串"0000111"的不同排列.所有不同的字符串包含三个1和4个0.这是我的代码:
p = itertools.permutations("0000111")
l = list(p)
print len(l) #5040
print len(set(l)) #35
Run Code Online (Sandbox Code Playgroud)
怎么了?还有更好的方法吗?
这是顺序:
l = [['A', 'G'], 'A', ['A', 'C']]
Run Code Online (Sandbox Code Playgroud)
我需要为每个排列返回三个元素序列
all = ['AAA','GAA','AAC','GAC']
Run Code Online (Sandbox Code Playgroud)
我无法想出这一个!我在保留排列顺序方面遇到了麻烦!
我有一个元组列表,我试图通过合并各个元组元素来获得其结果。
例如:
lists = [
[(1,), (2,), (3,)],
[(4,), (5,), (6,)]
]
p = itertools.product(*lists)
for product in p:
print product
Run Code Online (Sandbox Code Playgroud)
这产生了一堆元组:
((1,), (4,))
((1,), (5,))
((1,), (6,))
((2,), (4,))
((2,), (5,))
((2,), (6,))
((3,), (4,))
((3,), (5,))
((3,), (6,))
Run Code Online (Sandbox Code Playgroud)
我想要的是这样的元组列表:
(1,4)
(1,5)
(1,6)
(2,4)
(2,5)
(2,6)
(3,4)
(3,5)
(3,6)
Run Code Online (Sandbox Code Playgroud)
我还需要使用它来容纳任意数量的元组初始列表。
因此,对于3:
lists = [
[(1,), (2,), (3,)],
[(4,), (5,), (6,)],
[(7,), (8,), (9,)]
]
p = itertools.product(*lists)
for product in p:
print product
Run Code Online (Sandbox Code Playgroud)
我想要:
(1, 4, 7)
(1, 4, …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现我自己的版本itertools.compress,问题是我偶然发现了返回类型。我的意思是这两个函数都返回一个迭代器,但我认为第二个函数不被视为生成器函数,因为里面没有 yield 语句。所以我的问题是,这两个实现是等价的吗?
def compress (seq, selectors):
from operator import itemgetter
fst = itemgetter (0)
snd = itemgetter (1)
yield from map (fst, filter (snd, zip (seq, selectors)))
def compress (seq, selectors):
from operator import itemgetter
fst = itemgetter (0)
snd = itemgetter (1)
return map (fst, filter (snd, zip (seq, selectors)))
Run Code Online (Sandbox Code Playgroud) python ×10
python-itertools ×10
permutation ×3
subset ×2
tuples ×2
list ×1
palindrome ×1
python-2.7 ×1
python-3.x ×1
sequences ×1
yield-from ×1