python如何将数组分成几个区间,取每个区间的最大值存到另一个数组里
2015-04-29 16:13
369 查看
python如何将数组分成几个区间,取每个区间的最大值存到另一个数组里
贴吧上有人问python 问题:
有一段数组,把它分成几个区间,取每个区间的最大值存到另一个数组里
比如数组a[1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1,0]
看看python培训黄哥所写的2个方法。
第一种方法:
# coding:utf-8
"""
黄哥python远程视频培训班
https://github.com/pythonpeixun/article/blob/master/index.md
黄哥python培训试看视频播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
咨询qq:1465376564
"""
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
def group(lst, n):
num = len(lst) % n
zipped = zip(*[iter(lst)] * n)
return zipped if not num else zipped + [lst[-num:], ]
tmp = group(lst, 3)
max_lst = [max(item) for item in tmp]
print max_lst
#[3, 6, 9, 8, 5, 2]
第二种方法
# coding:utf-8
"""
黄哥python远程视频培训班
https://github.com/pythonpeixun/article/blob/master/index.md
黄哥python培训试看视频播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
咨询qq:1465376564
"""
def group(seq, size):
def take(seq, n):
for i in xrange(n):
yield seq.next()
if not hasattr(seq, 'next'):
seq = iter(seq)
while True:
x = list(take(seq, size))
if x:
yield x
else:
break
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
lst_group = group(lst, 3)
max_lst = [max(item) for item in list(lst_group)]
print max_lst
贴吧上有人问python 问题:
有一段数组,把它分成几个区间,取每个区间的最大值存到另一个数组里
比如数组a[1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1,0]
看看python培训黄哥所写的2个方法。
第一种方法:
# coding:utf-8
"""
黄哥python远程视频培训班
https://github.com/pythonpeixun/article/blob/master/index.md
黄哥python培训试看视频播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
咨询qq:1465376564
"""
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
def group(lst, n):
num = len(lst) % n
zipped = zip(*[iter(lst)] * n)
return zipped if not num else zipped + [lst[-num:], ]
tmp = group(lst, 3)
max_lst = [max(item) for item in tmp]
print max_lst
#[3, 6, 9, 8, 5, 2]
第二种方法
# coding:utf-8
"""
黄哥python远程视频培训班
https://github.com/pythonpeixun/article/blob/master/index.md
黄哥python培训试看视频播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
咨询qq:1465376564
"""
def group(seq, size):
def take(seq, n):
for i in xrange(n):
yield seq.next()
if not hasattr(seq, 'next'):
seq = iter(seq)
while True:
x = list(take(seq, size))
if x:
yield x
else:
break
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
lst_group = group(lst, 3)
max_lst = [max(item) for item in list(lst_group)]
print max_lst
相关文章推荐
- python如何将数组分成几个区间,取每个区间的最大值存到另一个数组里
- Python版求数组的最大连续区间
- 如何找到区间数组的最大和?最大积?
- 2个二叉树A、B,树的每个节点上都有一个整数值。如何判断A与B是否其中一个是另一个的子树?
- 求助,如何使用批处理命令查询本机有几个盘,然后每个盘总大小空间和可用大小空间,然后将这些信息生成一个文档,求高手指教,谢谢。
- java获取一维数组中连续数字分成几个连续的数字数组
- 【Python】求数组(list)中最大的X个数,不递归,尽可能减少代码量
- 题目:输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个 子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为 O(n)。
- 【BZOJ1012】【树状数组求区间最值】最大数maxnumber
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- 动态规划--求最大连续子数组的和(Python实现)&求解最大连续乘积字串(Python实现)
- 整数数组中,每个元素均出现两次,除了一个元素例外,如何找出这个元素?能否设计一个线性时间的算法,且不需要额外的存储空间?
- python 8-4 如何使用线程本地数据,threading.local函数可以创建线程本地数据空间,属性对每个线程独立存在
- 20个排序好的数组,每个数组500个数,按照降序排序好的,让找出500个最大的数。
- 如何寻找数组中的最小值与最大值
- 如何用C语言求一维数组中的最大数
- 窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆
- 对于1个正整数N,将其拆分成几个正整数的和,如何拆分可使得其乘积最大?
- 在Python中,如何将一个字符串数组转换成整型数组
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3