问一下关于python list的问题 请问如何比较两个LIST里数的大小?

我想做一个function比较两个list里数的大小defHistogram(A,B)A=[2,3,5,7,42]B=[3,8,45]这里小于等于3的在listA里有2个... 我想做一个function 比较两个list里数的大小 def Histogram(A,B) A = [2,3,5,7,42] B = [3,8,45] 这里小于等于3的在listA里有2个 大于等于3小于8的数有3个 大于等于8小于45的数有1个 最后的output listC = [2,3,1] 请问应该如何入手? 完全没有头绪...
原题:Histogram( [a1, ..., an], [b1, b2, ..., bk] ) returns a new list [c1, c2, ..., ck+1], where c1 is the number (count) of values in sample that are smaller than or equal to b1, ck+1 is the number of values in sample that are greater than bk, ci (for 1 < i < k) is the number of values in sample that are greater than bi and smaller than or equal to bi+1. That means that the list of values binBoundaries defines a set of bins; you can assume that the list binBoundaries for the is sorted in ascending order. The function Histogram counts (and returns) how many samples fall into each bin.
For your example
>>> t = [2, 3, 4, 5, 6, 25, 31]
>>> bins = [3, 5, 10, 30]
the output should be: [2, 2, 1, 1, 1]
2 numbers (2 and 3) are smaller than or equal to 3
2 numbers (4 and 5) are greater than 3 and smaller than or equal to 5
1 number (6) is greater than 5 and smaller than or equal to 10
1 number (25) is greater than 10 and smaller than or equal to 30
1 number (31) is greater than 30
展开
 我来答
galaas
2011-03-14 · TA获得超过370个赞
知道小有建树答主
回答量:191
采纳率:0%
帮助的人:194万
展开全部
为什么b[0]可以小于等于而其他都是小于,还有A,B是否都是升序的?

以下是最一般的处理:

c=[]
prev=0
for i in B:
c.append(0)
for j in A:
if j>=prev and i>j:
c[-1]+=1
prev=i
追问
不好意思啊 说错题了 b[0]是小于等于其他都是大于和小于等于的 是升序的 要求用.sort 的命令
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式