假设,我有一个NumPy整数数组,如:
[34,2,3,22,22,22,22,22,22,18,90,5,-55,-19,22,6,6,6,6,6,6,6,6,23,53,1,5,-42,82]
Run Code Online (Sandbox Code Playgroud)
我想找到数组的开始和结束索引,其中一个值超过x次(比如说5次)重复.所以在上面的例子中,它是值22和6.重复22的起始索引是3,结束索引是8.相同的重复6. Python中是否有一个特殊工具有用?否则,我会遍历索引的数组索引,并将实际值与之前的值进行比较.
问候.
我有一个有序的 Python 表单列表:
[1, 2, 3, 4, 5, 12, 13, 14, 15, 20, 21, 22, 23, 30, 35, 36, 37, 38, 39, 40]
Run Code Online (Sandbox Code Playgroud)
如何将列表中的连续数字分组在一起。像这样的一个团体:
[[1, 2, 3, 4, 5], [12, 13, 14, 15], [20, 21, 22, 23,], [30], [35, 36, 37, 38, 39, 40]]
Run Code Online (Sandbox Code Playgroud)
我尝试从这里使用 groupby但无法根据我的需要定制它。谢谢,
这是一个重复的问题,这除了对R,而不是Python的.
我想在列表中标识连续的组(有些人称它们是连续的)整数,其中重复的条目被视为在同一范围内存在.因此:
myfunc(c(2, 3, 4, 4, 5, 12, 13, 14, 15, 16, 17, 17, 20))
Run Code Online (Sandbox Code Playgroud)
收益:
min max
2 5
12 17
20 20
Run Code Online (Sandbox Code Playgroud)
虽然任何输出格式都可以.我目前的蛮力,for-loop方法非常慢.
(如果我能轻易地重新解释Python的答案并且我是愚蠢的,那道歉!)
我正在尝试从范围集合中删除重叠值.
范围由如下字符串表示:
499-505 100-115 80-119 113-140 500-550
我希望将上述内容减少到两个范围:80-140 499-550.这涵盖了所有值,没有重叠.
目前我有以下代码.
cr = "100-115 115-119 113-125 80-114 180-185 500-550 109-120 95-114 200-250".split(" ")
ar = []
br = []
for i in cr:
(left,right) = i.split("-")
ar.append(left);
br.append(right);
inc = 0
for f in br:
i = int(f)
vac = []
jnc = 0
for g in ar:
j = int(g)
if(i >= j):
vac.append(j)
del br[jnc]
jnc += jnc
print vac
inc += inc
Run Code Online (Sandbox Code Playgroud)
我将数组拆分-并将范围限制存储在ar …
我有这样一句话:
s = " foo hello hello hello I am a big mushroom a big mushroom hello hello bye bye bye bye foo"
Run Code Online (Sandbox Code Playgroud)
我想找到所有连续重复的单词序列和每个序列重复的次数.对于上面的例子:
[('hello', 3), ('a big mushroom', 2), ('hello', 2), ('bye', 4)]
Run Code Online (Sandbox Code Playgroud)
我有一个解决方案几乎适用于基于正则表达式的只有一个字符的单词,但我无法将其扩展到真实单词的情况:
def count_repetitions(sentence):
return [(list(t[0]),''.join(t).count(t[0])) for t in re.findall(r'(\w+)(\1+)', ''.join(sentence))]
l=['x', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g', 'h', 'i', 'i', 'i', 'i', 'a', 'b', 'c', 'd']
count_repetitions(sentence)
>>> [(['a', 'b', 'c'], 3), (['g', 'h'], 2), (['i', 'i'], …Run Code Online (Sandbox Code Playgroud) python ×4
list ×3
range ×2
algorithm ×1
arrays ×1
contiguous ×1
numpy ×1
python-3.x ×1
r ×1
string ×1