简体   繁体   English

查找组合的列表项对

[英]Finding combinations pairs of list items forward

I have a input list, 我有一个输入列表,

n = [0, 6, 12, 18, 24, 30, 36, 42, 48] 
# Here, the list is multiples of 6 
# but need not always be, could be of different numbers or unrelated.

Now I would like to generate pair of numbers from the list so the output is, 现在我想从列表中生成一对数字,所以输出是,

output = [(1, 6), (7, 12), (13, 18), (19, 24), (25, 30), (31, 36), (37, 42), (43, 48)]

I have below snippet to get it done. 我有以下片段来完成它。

zip([(i+1) for i in n[:-1]], n[1:])

Just out of curious, I would like to know the other approaches than what I have! 出于好奇,我想知道其他方法而不是我的方法!

What you have right now is pretty good (in my books). 你现在拥有的是非常好的(在我的书中)。 Although, you could substitute the n[:-1] with n , because zip performs a "shortest possible zipping" - zips upto the shorter of the two lists - 虽然,你可以代替n[:-1]n ,因为zip执行“最短的压缩和解” -拉链高达两个列表中较短的-

>>> list(zip([1, 2, 3], [4, 5]))
[(1, 4), (2, 5)]

So, you could rewrite your expression as - 所以,您可以将表达式重写为 -

list(zip([(i+1) for i in n], n[1:]))

For conciseness. 为了简洁。 Drop the list(..) for python 2. 删除python 2的list(..)

An alternative ( suggested by RoadRunner ), would be to bring the list comprehension out, and the zip in - 一个替代方案( 由RoadRunner建议 ),将列表理解,以及zip -

>>> [(x + 1, y) for x, y in zip(n, n[1:])]
[(1, 6), (7, 12), (13, 18), (19, 24), (25, 30), (31, 36), (37, 42), (43, 48)]

Or, you can get rid of zip completely (as suggested by splash58 ), by using positional based indexing - 或者,您可以通过使用基于位置的索引完全摆脱zip (如splash58所示 ) -

>>> [(n[i] + 1, n[i + 1]) for i in range(len(n) - 1)]
[(1, 6), (7, 12), (13, 18), (19, 24), (25, 30), (31, 36), (37, 42), (43, 48)]

Another way to do this is using the functional programming paradigm, with map - 另一种方法是使用函数式编程范例,使用map -

>>> list(zip(map(lambda x: x + 1, n), n[1:]))
[(1, 6), (7, 12), (13, 18), (19, 24), (25, 30), (31, 36), (37, 42), (43, 48)]

Which does the same thing your list comp does, but probably slower! 你的列表组件做了同样的事情,但可能更慢!


And finally, if you use pandas (my favourite library), then you can leverage the IntervalIndex API - 最后,如果您使用pandas (我最喜欢的库),那么您可以利用IntervalIndex API -

>>> pd.IntervalIndex.from_breaks(n, closed='right')
IntervalIndex([(0, 6], (6, 12], (12, 18], (18, 24], (24, 30], (30, 36], (36, 42], (42, 48]]
              closed='right',
              dtype='interval[int64]')

Another one as numpy accepts integer addition: numpy的另一个接受整数加法:

import numpy as np

n = [0, 6, 12, 18, 24, 30, 36, 42, 48] 
a = np.array(n)
output = list(zip(a+1,a[1:]))

print(output)

Returns: 返回:

[(1, 6), (7, 12), (13, 18), (19, 24), (25, 30), (31, 36), (37, 42), (43, 48)]

Adding to @cᴏʟᴅsᴘᴇᴇᴅ's recommendation of the functional programming paradigm, you could also use map() without zip() to do this: 添加@cᴏʟᴅsᴘᴇᴇᴅ对函数式编程范例的推荐,你也可以使用不带zip() map() zip()来做到这一点:

n = [0, 6, 12, 18, 24, 30, 36, 42, 48] 

result = list(map(lambda x, y: (x + 1, y), n, n[1:]))

print(result)
# [(1, 6), (7, 12), (13, 18), (19, 24), (25, 30), (31, 36), (37, 42), (43, 48)
n1=range(0,49)
n=n1[1::6]
x1=range(1,44)
x=x1[::6]
print (zip(x,n))

Are you looking for something like this ? 你在找这样的东西吗?

final_list=[]
n = [0, 6, 12, 18, 24, 30, 36, 42, 48]
for i in range(0,len(n),1):
    chuck=n[i:i+2]
    if len(chuck)<2:
        pass
    else:
        final_list.append((chuck[0]+1,chuck[1]))
print(final_list)

output: 输出:

[(1, 6), (7, 12), (13, 18), (19, 24), (25, 30), (31, 36), (37, 42), (43, 48)]

You can also do in one line: 你也可以在一行中做:

n = [0, 6, 12, 18, 24, 30, 36, 42, 48]
print([(n[i:i+2][0]+1,n[i:i+2][1]) for i in range(0,len(n),1) if len(n[i:i+2])==2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM