python - 不同的整数组

标签 python algorithm arraylist

给定一个整数数组,我想确定值递增的不同整数组的数量。

给定数组 myList = [1, 2, 3, 4, 3, 2, 2, 3, 1, 2, 1, 4, 2]

有 4 组不同的整数,其中值递增。即

[1, 2, 3, 4], [2, 2, 3], [1, 2][2]

有经验的人可以指导我如何在 python 中实现这一点吗?

最佳答案

另一个可能的答案(也假设 [1, 4] 应该在那里而不是 [2]):


In [14]: def find_ascending_groups(my_list): 
    ...:     groups = [] 
    ...:     current_group = [my_list[0]]
    ...:     for i in range(1, len(my_list)):         
    ...:         if current_group[-1] <= my_list[i]: 
    ...:             current_group.append(my_list[i]) 
    ...:         else: 
    ...:             if len(current_group) > 1: 
    ...:                 groups.append(current_group) 
    ...:             current_group = [my_list[i]] 
    ...:     if len(current_group) > 1:
    ...:         groups.append(current_group) 
    ...:     print(groups) 
    ...:     

In [15]: find_ascending_groups(myList)
[[1, 2, 3, 4], [2, 2, 3], [1, 2], [1, 4]]

关于python - 不同的整数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236885/

相关文章:

java - 从 ArrayList 中删除重复项的算法

python - Pandas 使用 NaN 旋转或 reshape 数据框

python - 将数组插值到恒定密度

python - 在两个用户之间共享临时数据的正确方法

java - 比较两个 ArrayList 索引时出现 IndexOutOfBoundsException

java - 通用类型 x 通用参数 : Building a "very generic" structure

Python:在一行中优雅地打印列表中的所有*剩余*元素

python - Numpy:为什么 (2,1) 数组和垂直矩阵切片的差异不是 (2,1) 数组

java - 两个表的组合来填充距离D算法

算法 : Rearrange 2D Matrix (through element 'flipping' )