我有一个列表列表(使用简单的列表推导生成):
>>> base_lists = [[a, b] for a in range(1, 3) for b in range(1, 6)]
>>> base_lists
[[1,1],[1,2],[1,3],[1,4],[1,5],[2,1],[2,2],[2,3],[2,4],[2,5]]
Run Code Online (Sandbox Code Playgroud)
我想将整个列表转换为包含列表中所有值的元组,即:
resulting_tuple = (1,1,1,2,1,3,1,4,1,5,2,1,2,2,2,3,2,4,2,5)
Run Code Online (Sandbox Code Playgroud)
最有效的方法是什么?(使用列表理解生成这个相同元组的方法也是可以接受的答案.)我在这里和Python文档中查看了答案,但是我找不到合适的答案.
编辑:
非常感谢所有回答的人!
我有这个初始字符串.
'bananaappleorangestrawberryapplepear'
Run Code Online (Sandbox Code Playgroud)
并且还有一个带字符串的元组:
('apple', 'plepe', 'leoran', 'lemon')
Run Code Online (Sandbox Code Playgroud)
我想要一个函数,以便从最初的字符串和带有字符串的元组我得到这个:
'bananaxxxxxxxxxgestrawberryxxxxxxxar'
Run Code Online (Sandbox Code Playgroud)
我知道如何通过在每个单词的初始字符串中找到单词然后在所有带有替换单词的初始字符串中逐字符循环来强制执行.
但它不是非常有效和丑陋.我怀疑应该有一些方法可以通过itertools或其他东西以功能的方式更优雅地完成这项工作.如果您知道可以高效执行此操作的Python库,请告诉我们.
更新:贾斯汀皮尔指出我在最初的问题中没有描述的案例.如果一个单词是'aaa'并且'aaaaaa'在初始字符串中,则输出应该看起来像'xxxxxx'.
python string optimization functional-programming python-itertools
我有一个整数列表,我想生成一个包含所有连续整数列表的列表.
#I have:
full_list = [0,1,2,3,10,11,12,59]
#I want:
continuous_integers = [[0,1,2,3], [10,11,12], [59]]
Run Code Online (Sandbox Code Playgroud)
我有以下工作,但似乎是一个糟糕的方式:
sub_list = []
continuous_list = []
for x in full_list:
if sub_list == []:
sub_list.append(x)
elif x-1 in sub_list:
sub_list.append(x)
else:
continuous_list.append(sub_list)
sub_list = [x]
continuous_list.append(sub_list)
Run Code Online (Sandbox Code Playgroud)
我已经看到其他问题表明itertools.groupby是一种有效的方法,但是我不熟悉这个函数,我似乎在编写lambda函数来描述连续性时遇到了麻烦.
问题:有没有更好的方法(可能使用itertools.groupby?)
注意事项:full_list将包含1到59个整数,将始终排序,整数将介于0到59之间.
当我使用itertools.product时,如何跳过迭代中具有重复元素的元组?或者说,无论如何不要在迭代中查看它们?因为如果列表的数量太多,跳过可能是耗时的.
Example,
lis1 = [1,2]
lis2 = [2,4]
lis3 = [5,6]
[i for i in product(lis1,lis2,lis3)] should be [(1,2,5), (1,2,6), (1,4,5), (1,4,6), (2,4,5), (2,4,6)]
Run Code Online (Sandbox Code Playgroud)
它不会有(2,2,5)和(2,2,6),因为2在这里重复.我怎样才能做到这一点?
我有一个生成器函数列表,如:
def myGen(x):
for i in range(x):
yield i
g5 = myGen(5); g10 = myGen(10); g15 = myGen(15)
cycleList = [g5, g10, g15]
Run Code Online (Sandbox Code Playgroud)
在这些生成器之间循环并从列表中删除耗尽的生成器的最佳方法是什么?
输出应该是:
0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13 14
Run Code Online (Sandbox Code Playgroud) 所以我有一个字符串列表列表
[['a','b'],['c','d'],['e','f']]
Run Code Online (Sandbox Code Playgroud)
我想得到所有可能的组合,结果是
[['a','b'],['c','d'],['e','f'],
['a','b','c','d'],['a','b','e','f'],['c','d','e','f'],
['a','b','c','d','e','f']]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经提出了这段代码
input = [['a','b'],['c','d'],['e','f']]
combs = []
for i in xrange(1, len(input)+1):
els = [x for x in itertools.combinations(input, i)]
combs.extend(els)
print combs
Run Code Online (Sandbox Code Playgroud)
很大程度上遵循这篇文章的答案.
但结果是
[(['a','b'],),(['c','d'],),(['e','f'],),
(['a','b'],['c','d']),(['a','b'],['e','f']),(['c','d'],['e','f']),
(['a','b'],['c', 'd'],['e', 'f'])]
Run Code Online (Sandbox Code Playgroud)
我现在很难过,试图找到一种优雅,pythonic的方式解开那些元组.
说我有以下三个列表:
aList = [1,2,3,4,5,6]
bList = ['a','b','c','d']
cList = [1,2]
Run Code Online (Sandbox Code Playgroud)
我想使用来遍历它们zip。
通过如下方式使用循环zip:
from itertools import cycle
for a,b,c in zip(aList, cycle(bList), cycle(cList)):
print a,b,c
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:
1 a 1
2 b 2
3 c 1
4 d 2
5 a 1
6 b 2
Run Code Online (Sandbox Code Playgroud)
虽然我希望我的结果像:
1 a 1
2 b 1
3 c 1
4 d 1
5 a 2
6 b 2
Run Code Online (Sandbox Code Playgroud) 给出一个上限列表:B1,B2,... BN;
依赖函数:f1,...,fN-1,
我想知道是否有一个使用itertools或python中的其他类的配方:
for i1 in range(0, B1):
for i2 in range(f1(i1), B2): ...
for iN in range(fN-1(iN-1), BN)
dostuff(i1, i2, ... iN)
Run Code Online (Sandbox Code Playgroud)
哪里有N级筑巢?
我想使用这样的辅助函数:
dependentProducts(Bs,fs,dostuff),
它返回一个列表或者可迭代的
理想情况下,实现将是迭代而不是递归.
我正试图在hackerrank比赛中找到一个有趣的问题,并且出现了这个问题.我使用itertools,这是代码:
import itertools
l = []
for _ in range(int(input())):
l.append(int(input()))
max = l[0] * l[len(l)-1]
for a,b in itertools.combinations(l,2):
if max < (a*b):
max = (a*b)
print(max)
Run Code Online (Sandbox Code Playgroud)
他们还有其他任何有效的方法吗?因为我在一些我无法访问的测试用例中得到时间错误(因为它是一个小小的竞赛).
在Python中,我可以执行以下操作:
from itertools import product
k = 3
for kmer in product("AGTC", repeat=k):
print(kmer)
Run Code Online (Sandbox Code Playgroud)
在Rust中,我可以k=3通过以下方式强制行为:
#[macro_use] extern crate itertools;
for kmer in iproduct!("AGTC".chars(), "AGTC".chars(), "AGTC".chars()){
println!("{:?}", kmer);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要k=4或k=5怎么办?
python ×10
python-itertools ×10
python-2.7 ×2
python-3.x ×2
combinations ×1
continuous ×1
cycle ×1
functools ×1
group-by ×1
lambda ×1
list ×1
optimization ×1
rust ×1
string ×1
tuples ×1
zip ×1