我有一个整数列表,我想生成一个包含所有连续整数列表的列表.
#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之间.
如何有效地将已排序的numpy数组中的相同元素移动到子数组中?
从这里:
import numpy as np
a=np.array([0,0,1,1,1,3,5,5,5])
Run Code Online (Sandbox Code Playgroud)
到这里:
a2=array([[0, 0], [1, 1, 1], [3], [5, 5, 5]], dtype=object)
Run Code Online (Sandbox Code Playgroud) 我正试图在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怎么办?
这段代码:
from itertools import groupby, count
L = [38, 98, 110, 111, 112, 120, 121, 898]
groups = groupby(L, key=lambda item, c=count():item-next(c))
tmp = [list(g) for k, g in groups]
Run Code Online (Sandbox Code Playgroud)
采用[38, 98, 110, 111, 112, 120, 121, 898],按连续数字对其进行分组,并将它们与最终输出合并:
['38', '98', '110,112', '120,121', '898']
Run Code Online (Sandbox Code Playgroud)
如何对具有多列的列表进行同样的操作,就像下面的列表一样,您可以按名称及其第二列值的连续性对它们进行分组,然后合并。
换句话说,这个数据:
L= [
['Italy','1','3']
['Italy','2','1'],
['Spain','4','2'],
['Spain','5','8'],
['Italy','3','10'],
['Spain','6','4'],
['France','5','3'],
['Spain','20','2']]
Run Code Online (Sandbox Code Playgroud)
应给出以下输出:
[['Italy','1-2-3','3-1-10'],
['France','5','3'],
['Spain','4-5-6','2-8-4'],
['Spain','20','2']]
Run Code Online (Sandbox Code Playgroud)
more-itertools应该更适合这项任务吗?
在 Python 中使用 itertools/more-itertools 对多列列表的项目进行分组和组合
我有以下代码可以使用 itertools 生成指定范围内的所有可能组合,但我无法通过使用 cython 代码来提高速度。原始代码是这样的:
from itertools import *
def x(e,f,g):
a=[]
for c in combinations(range(e, f),g):
d = list((c))
a.append(d)
Run Code Online (Sandbox Code Playgroud)
声明 cython 的类型后:
from itertools import *
cpdef x(int e,int f,int g):
cpdef tuple c
cpdef list a
cpdef list d
a=[]
for c in combinations(range(e, f),g):
d = list((c))
a.append(d)
Run Code Online (Sandbox Code Playgroud)
我将后者保存为test_cy.pyx并使用编译cythonize -a -i test_cy.pyx
编译后,我使用以下代码创建了一个新脚本并运行它:
import test_cy
test_cy.x(1,45,6)
Run Code Online (Sandbox Code Playgroud)
我没有得到任何显着的速度提升,仍然花费了与原始脚本相同的时间,大约 10.8 秒。
我是否做错了什么,或者 itertools 已经如此优化以至于无法对其速度进行任何更大的改进?
Python中有没有计算重复排列数的函数?我知道使用 itertools 我可以生成它们,但我只想要一种更快的方法来进行计算并避免计算阶乘,我对生成所有可能的排列不感兴趣。
例:计算4个A、3个B、5个C可能组成的字符串。
分辨率= 12!/(4!3!5!)
或者在代码中:
from math import factorial
from functools import reduce
rep=[4,3,5]
result= factorial(sum(rep))//reduce(lambda x,y: x*y, map(factorial, rep))
Run Code Online (Sandbox Code Playgroud) 我们可以从Python 3中的itertools.product结果中获取特定索引中的元素吗?像下面这样:
xlist = itertools.product('abc', repeat=3)
element = xlist[10]
Run Code Online (Sandbox Code Playgroud)
可能是重复的,但我找不到任何东西。
\n\n我有一个很长的迭代器(10000 个项目),我需要一次迭代约 500 个项目。所以如果我的迭代器是range(10000),它看起来像这样:
Iteration #1: 0, 1, 2, ... 497, 498, 499\nIteration #2: 1, 2, 3, ... 498, 499, 500\nIteration #3: 2, 3, 4, ... 499, 500, 501\nIteration #4: 3, 4, 5, ... 500, 501, 502\n...\nIteration #9500: 9499, 9500, 9501 ... 9996, 9997, 9998\nIteration #9501: 9500, 9501, 9502 ... 9997, 9998, 9999\nRun Code Online (Sandbox Code Playgroud)\n\n等等。有这样一个方法:
\n\ndef nwise_slice(lst, n):\n for i in range(len(lst) - n + 1):\n yield lst[i:i + n]\nRun Code Online (Sandbox Code Playgroud)\n\n但是,这不适用于惰性迭代器。我尝试使用迭代器创建一个解决方案,并根据itertools pairwise …
我想从一个可迭代对象生成一个其前缀的可迭代对象(包括原始可迭代对象本身)。
for prefix in prefixes(range(5)):
print(tuple(prefix))
Run Code Online (Sandbox Code Playgroud)
应该导致
(0,)
(0, 1)
(0, 1, 2)
(0, 1, 2, 3)
(0, 1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)
或在
()
(0,)
(0, 1)
(0, 1, 2)
(0, 1, 2, 3)
(0, 1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)
和
for prefix in prefixes('Hello'):
print(''.join(prefix))
Run Code Online (Sandbox Code Playgroud)
应该导致
H
He
Hel
Hell
Hello
Run Code Online (Sandbox Code Playgroud)
或在
H
He
Hel
Hell
Hello
Run Code Online (Sandbox Code Playgroud)
(空前缀是否是结果的一部分对我来说并不重要,内部或外部结果迭代的确切类型也不重要。)
我能够设计出几种方法来实现这一点,但都感觉至少有点笨拙:
(如果可迭代是一个序列,则有效)
def prefixes(seq):
for i in range(len(seq)):
yield seq[:i + 1]
Run Code Online (Sandbox Code Playgroud)
或使用列表理解:
def prefixes(seq):
return [seq[:i + 1] for i …Run Code Online (Sandbox Code Playgroud) python ×10
python-itertools ×10
python-3.x ×2
continuous ×1
cython ×1
group-by ×1
iterator ×1
lambda ×1
list ×1
numpy ×1
python-2.7 ×1
rust ×1