在列表中查找插入点 [英] Finding the insertion point in a list
问题描述
我有一个有序列表,例如x = [0,100,200,1000],并给出任何
正整数y,我想确定其在
列表中的适当位置(即我必须插入它以便
保持列表排序。我可以通过一系列if
语句清楚地做到这一点:
>
如果y< x [1]:
n = 0
elif y< x [2]:
n = 1
elif y< x [3]:
n = 2
else:
n = 3 >
或具有生成器理解力
n = sum(y> x [i] for i in range(len(x))) - 1
但必须有一个更清洁的方式,因为第一种方法是笨重的
并且不适应更改列表长度,第二种方法不是
对于代码的随意读者来说很明显。
我的列表通常会有2到5个项目,因此速度不是很大的问题。我'感谢您的指导。
此致
Thomas Philips
I have an ordered list e.g. x = [0, 100, 200, 1000], and given any
positive integer y, I want to determine its appropriate position in
the list (i.e the point at which I would have to insert it in order to
keep the list sorted. I can clearly do this with a series of if
statements:
if y<x[1]:
n = 0
elif y < x[2]:
n = 1
elif y < x[3]:
n = 2
else:
n = 3
Or with a generator comprehension
n = sum ( y>x[i] for i in range(len(x)) ) - 1
But there has to be a cleaner way, as the first approach is unwieldy
and does not adapt to changing list lengths, and the second is not
obvious to a casual reader of the code.
My list will typically have 2 to 5 items, so speed is not a huge
issue. I''d appreciate your guidance.
Sincerely
Thomas Philips
推荐答案
您可以查看bisect模块(标准
发行版的一部分)。
You might look at the bisect module (part of the standard
distribution).
3月16日下午12:59,tkp ... @ hotmail.com写道:
On Mar 16, 12:59 pm, tkp...@hotmail.com wrote:
我有一个有序的清单,例如x = [0,100,200,1000],并给出任何
正整数y,我想确定其在
列表中的适当位置(即我必须插入它以便
保持列表排序。我可以通过一系列if
语句清楚地做到这一点:
>
如果y< x [1]:
n = 0
elif y< x [2]:
n = 1
elif y< x [3]:
n = 2
else:
n = 3 >
或具有生成器理解力
n = sum(y> x [i] for i in range(len(x))) - 1
但必须有一个更清洁的方式,因为第一种方法是笨重的
并且不适应更改列表长度,第二种方法不是
对于代码的随意读者来说很明显。
我的列表通常会有2到5个项目,因此速度不是很大的问题。我'感谢你的指导。
真诚
托玛s飞利浦
I have an ordered list e.g. x = [0, 100, 200, 1000], and given any
positive integer y, I want to determine its appropriate position in
the list (i.e the point at which I would have to insert it in order to
keep the list sorted. I can clearly do this with a series of if
statements:
if y<x[1]:
n = 0
elif y < x[2]:
n = 1
elif y < x[3]:
n = 2
else:
n = 3
Or with a generator comprehension
n = sum ( y>x[i] for i in range(len(x)) ) - 1
But there has to be a cleaner way, as the first approach is unwieldy
and does not adapt to changing list lengths, and the second is not
obvious to a casual reader of the code.
My list will typically have 2 to 5 items, so speed is not a huge
issue. I''d appreciate your guidance.
Sincerely
Thomas Philips
一种方法是使用内置的cmp并循环遍历列表中的
项目。也许是这样的:
x = [0,100,200,1000]
numLst = len(x)
count = 0
for i in range(numLst):
resultOfCmp = cmp(newNum,x [count])
if resultOfCmp == - 1:
打印i
x.insert(count,newNum)
break
count + = 1
#其中newNum是要插入的那个。
这是一个黑客,但它可能会让你的创意果汁流动起来。
Mike
One way to do this would be to use the cmp built-in and loop over the
items in the list. Maybe something like this:
x = [0, 100, 200, 1000]
numLst = len(x)
count = 0
for i in range(numLst):
resultOfCmp = cmp(newNum, x[count])
if resultOfCmp == -1:
print i
x.insert(count, newNum)
break
count += 1
# Where newNum is the one to be inserted.
It''s a hack, but it might get the ol'' creative juices flowing.
Mike
3月16日下午12:59,tkp ... @ hotmail.com写道:
On Mar 16, 12:59 pm, tkp...@hotmail.com wrote:
我有一个有序列表,例如x = [0,100,200,1000],并给出任何
正整数y,我想确定其在
列表中的适当位置(即我必须插入它以便
保持列表排序。我可以通过一系列if
语句清楚地做到这一点:
>
如果y< x [1]:
n = 0
elif y< x [2]:
n = 1
elif y< x [3]:
n = 2
else:
n = 3 >
或具有生成器理解力
n = sum(y> x [i] for i in range(len(x))) - 1
但必须有一个更清洁的方式,因为第一种方法是笨重的
并且不适应更改列表长度,第二种方法不是
对于代码的随意读者来说很明显。
我的列表通常会有2到5个项目,因此速度不是很大的问题。我'感谢你的指导。
真诚
托玛s飞利浦
I have an ordered list e.g. x = [0, 100, 200, 1000], and given any
positive integer y, I want to determine its appropriate position in
the list (i.e the point at which I would have to insert it in order to
keep the list sorted. I can clearly do this with a series of if
statements:
if y<x[1]:
n = 0
elif y < x[2]:
n = 1
elif y < x[3]:
n = 2
else:
n = 3
Or with a generator comprehension
n = sum ( y>x[i] for i in range(len(x)) ) - 1
But there has to be a cleaner way, as the first approach is unwieldy
and does not adapt to changing list lengths, and the second is not
obvious to a casual reader of the code.
My list will typically have 2 to 5 items, so speed is not a huge
issue. I''d appreciate your guidance.
Sincerely
Thomas Philips
列表通常会有2到5个项目?保持简单!
x.append(y)
x.sort()
- 保罗
List "will typically have 2 to 5 items"? Keep it simple!
x.append(y)
x.sort()
-- Paul
这篇关于在列表中查找插入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!