列表转换问题 [英] list conversion question

查看:76
本文介绍了列表转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在使用PIL,并希望

列出像素值

in一个图像,按照这个值的数量来计算

像素。


im.histogram()返回一个列表

是每个亮度值的像素数。


所以,我想对直方图进行排序,并且得到

结果列表包含

索引而不是计数。


这似乎是一个相当常见的

任务,但我不知道该怎么做致电

寻找指导。


任何帮助表示赞赏。


谢谢,

Kent

解决方案

" Kent Tenney" <柯** @ springfed.com>在留言中写道

新闻:ma ************************************ ** @ pyth on.org ...

你好,

我正在使用PIL,并希望列出像素值
im.histogram()返回一个列表,其中
是每个亮度值的像素数。 br />
所以,我想对直方图进行排序,并且结果列表包含索引而不是计数。

这似乎是'这是一个相当普遍的任务,但我不知道该怎么称呼它寻找指导。

任何帮助都表示赞赏。
<谢谢,
肯特



假设im.histogram()返回类似[0,1,0,5,43等]的列表如何

关于


hist = [0,1,0,5,43]

values = [ i for i in enumerate(hist)]

values.sort(lambda a,b:cmp(b [1],a [1]))

索引= [a for a,b in values]


- Paul


Paul McGuire写道:

假设im.histogram()返回一个像[0,1,0,5,43等]的列表如何
关于

hist = [0,1,0,5,43]
values = [i for i in enumerate(hist)]
values.sort(lambda a,b:cmp(b [1],a [1 ]))
index = [a for a,b in values]




或稍微调整一下速度(用lambda排序很贵)

为清晰起见,IMO,


pairs.sort()

indices = [成对的(值,偏移)偏移量]


在Python2.4中允许这样做/>

hist = [0,1,0,5,43]
[pair [0] for pair in sorted(enumerate(hist),
.... key = lambda pair:pair [1])]

[0, 2,1,3,4]




还不确定这是件好事。

Andrew
da***@dalkescientific.com

" Andrew Dalke" <广告**** @ mindspring.com>在留言中写道

news:iv ***************** @ newsread1.news.pas.earthl ink.net ...

Paul McGuire写道:

假设im.histogram()返回如[0,1,0,5,43等]的列表如何
关于

hist = [0,1,0,5,43]
values = [i for i in enumerate(hist)]
values.sort(lambda a ,b:cmp(b [1],a [1]))
indices = [a for a,b in values]



或稍微调整一下以获得速度(带有lambda的排序是昂贵的)
为了清楚起见,IMO,
对=枚举(hist)中的(偏移量,值)的[(值,偏移量)]
pairs.sort()
indices = [成对(值,偏移)的偏移量]

在Python2.4中允许这样做

< blockquote class =post_quotes>>>> hist = [0,1,0,5,43]
>>> [pair [0] for sorted in sorted(enumerate(hist),... key = lambda pair:pair [1])]
[0,2,1,3,4]>>>



还不确定这是件好事。

Andrew
da *** @ dalkescientific.com




我假设结果列表应按降序排序

订单。可能最快的方法(因为我们正在调整

速度)只是将对列表comp更改为pairs = [(-value,

) (枚举(hist)]中的(偏移量,值),或者你的Py 2.4键子句

来自key = lambda pair:pair [1] tokey = lambda pair:-pair [1]。


- Paul


Howdy,

I am using PIL, and would like to
make a list of the pixel values
in an image, sorted by quantity of
pixels with that value.

im.histogram() returns a list which
is a pixel count for each brightness value.

So, I want to sort the histogram, and have
the resulting list contain
the indexes instead of the counts.

This seems like it''d be a fairly common
task, but I don''t know what to call
it to look for guidance.

Any help is appreciated.

Thanks,
Kent

解决方案

"Kent Tenney" <ke**@springfed.com> wrote in message
news:ma**************************************@pyth on.org...

Howdy,

I am using PIL, and would like to
make a list of the pixel values
in an image, sorted by quantity of
pixels with that value.

im.histogram() returns a list which
is a pixel count for each brightness value.

So, I want to sort the histogram, and have
the resulting list contain
the indexes instead of the counts.

This seems like it''d be a fairly common
task, but I don''t know what to call
it to look for guidance.

Any help is appreciated.

Thanks,
Kent


Assuming im.histogram() returns a list like [ 0, 1, 0, 5, 43, etc. ] how
about:

hist = [ 0, 1, 0, 5, 43 ]
values = [ i for i in enumerate(hist)]
values.sort(lambda a,b: cmp(b[1],a[1]))
indexes = [ a for a,b in values ]

-- Paul


Paul McGuire wrote:

Assuming im.histogram() returns a list like [ 0, 1, 0, 5, 43, etc. ] how
about:

hist = [ 0, 1, 0, 5, 43 ]
values = [ i for i in enumerate(hist)]
values.sort(lambda a,b: cmp(b[1],a[1]))
indexes = [ a for a,b in values ]



or tweaked a bit for speed (a sort with a lambda is expensive)
and for clarity, IMO,

pairs = [(value, offset) for (offset, value) in enumerate(hist)]
pairs.sort()
indexes = [offset for (value, offset) in pairs]

In Python2.4 this is allowed

hist = [ 0, 1, 0, 5, 43 ]
[pair[0] for pair in sorted(enumerate(hist), .... key=lambda pair: pair[1])]
[0, 2, 1, 3, 4]



Not yet sure that that''s a good thing.

Andrew
da***@dalkescientific.com


"Andrew Dalke" <ad****@mindspring.com> wrote in message
news:iv*****************@newsread1.news.pas.earthl ink.net...

Paul McGuire wrote:

Assuming im.histogram() returns a list like [ 0, 1, 0, 5, 43, etc. ] how
about:

hist = [ 0, 1, 0, 5, 43 ]
values = [ i for i in enumerate(hist)]
values.sort(lambda a,b: cmp(b[1],a[1]))
indexes = [ a for a,b in values ]



or tweaked a bit for speed (a sort with a lambda is expensive)
and for clarity, IMO,

pairs = [(value, offset) for (offset, value) in enumerate(hist)]
pairs.sort()
indexes = [offset for (value, offset) in pairs]

In Python2.4 this is allowed

>>> hist = [ 0, 1, 0, 5, 43 ]
>>> [pair[0] for pair in sorted(enumerate(hist), ... key=lambda pair: pair[1])]
[0, 2, 1, 3, 4] >>>



Not yet sure that that''s a good thing.

Andrew
da***@dalkescientific.com



I assumed that the resulting list should be sorted in descending frequency
order. Probably the fastest way to do this (since we are tweaking for
speed) would be to just change the pairs list comp to "pairs = [(-value,
offset) for (offset, value) in enumerate(hist)]", or your Py 2.4 key clause
from "key=lambda pair: pair[1]" to "key=lambda pair: -pair[1]".

-- Paul


这篇关于列表转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆