[英]find n elements on the left of an element in list python
I have a list我有一个清单
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I am looking to write a function get_n_preceeding(a, list_of_numbers, n) , which takes an argument a and returns n numbers which preceded that number in list_of_numbers.我正在寻找编写一个函数get_n_preceeding(a, list_of_numbers, n) ,它接受一个参数a并返回在 list_of_numbers 中该数字之前的n 个数字。
For example:例如:
get_n_preceeding(4, my_list, 2):
This should return 2 numbers preceding 4 in the list.这应该返回列表中 4 之前的 2 个数字。 ie ans = [2,3]
即 ans = [2,3]
Similarly, if I want 2 numbers preceding 1, it should give the result as [9,10] # This I think, is the tricky part.同样,如果我想要 1 之前的 2 个数字,它应该给出结果 [9,10] # 我认为这是棘手的部分。
Similarly, I am looking to write another function get_n_succeeding(a, list_of_numbers, b)同样,我正在寻找另一个函数get_n_succeeding(a, list_of_numbers, b)
get_n_succeeding(7, my_list, 2) # This should return [8,9]
If I use get_n_succeeding(9, my_list, 2)
, it should return [10,1].如果我使用
get_n_succeeding(9, my_list, 2)
,它应该返回 [10,1]。
I tried using zip operator but couldn't do it.我尝试使用zip运算符,但无法做到。
Is there any better way of doing this?有没有更好的方法来做到这一点?
I used list comprehension for get_n_preceeding and just a for loop for the succeeding, subtracting the length of the array if the succeeding index goes out of bounds. 我对get_n_preceeding使用了列表推导,对后继使用了for循环,如果后继索引超出范围,则减去数组的长度。
def get_n_preceeding(a: int, numbers: list, n: int = 2) -> list:
start = numbers.index(a)
return [numbers[start - n + x] for x in range(n)]
def get_n_succeeding(a: int, numbers: list, n: int = 2) -> list:
start = numbers.index(a) + 1
length = len(numbers)
output = []
for x in range(n):
try:
output.append(numbers[start + x])
except IndexError:
output.append(numbers[start + x - length])
return output
my_list = list(range(1, 11))
print(get_n_preceeding(1, my_list, 2)) # -> [9, 10]
print(get_n_succeeding(9, my_list, 2)) # -> [10, 1]
Use array.index()
to find the position, then slice the array accordingly: 使用
array.index()
查找位置,然后相应地切片数组 :
def get_n_preceding(xs, x, n):
end = xs.index(x)
start = end - n
ys = []
for i in range(start, end):
ys.append(xs[i % len(xs)])
return ys
def get_n_succeeding(xs, x, n):
start = xs.index(x) + 1
end = start + n
ys = []
for i in range(start, end):
ys.append(xs[i % len(xs)])
return ys
get_n_preceding(list(range(10)), 1, 2) #=> [10, 0]
get_n_preceding(list(range(10)), 5, 2) #=> [3, 4]
get_n_succeeding(list(range(10)), 8, 2) #=> [9, 0]
get_n_succeeding(list(range(10)), 5, 2) #=> [6, 7]
As noted in the docs, array.index()
will find the first matching element, and ignore any later duplicates. 如文档中所述,
array.index()
将找到第一个匹配的元素,并忽略以后的任何重复项。
You can use the .index()
method to find a
's index. 您可以使用
.index()
方法来找到a
“s指数。
aIndex = L.index(a)
preceding = []
for x in range(aIndex - n, aIndex): # Counting n numbers before a
if (aIndex - n) >= 0:
preceding.append(L[x])
else:
aIndex = len(L) # "Turns around" the list if a - n < 0
preceding.append(L[x])
print(preceding)
You can use a generator and enumerate(iterable) : 您可以使用一个生成器并枚举(可迭代) :
def get_n_preceeding(val,data,n=2, findall=False):
found_at = []
for pos,e in enumerate(data):
if e == val:
found_at.append(pos)
if not findall:
break
ld = len(data)
for p in found_at:
circle = data + data + data
yield circle[p-n+ld:p+ld]
Test: 测试:
L = [1, 2, 3, 4, 5, 4, 6, 7, 8, 4, 9, 10]
print("Get first")
for part in get_n_preceeding(1,L,2):
print(part)
print("Get all")
for part in get_n_preceeding(4,L,2,True):
print(part)
print("Get circle")
for part in get_n_preceeding(1,L,4):
print(part)
Output: 输出:
Get first
[9, 10]
Get all
[2, 3]
[4, 5]
[7, 8]
Get circle
[8, 4, 9, 10]
The modulus operator ( %
) is often a good choice when you need an expression to wrap or repeat regularly. 当您需要定期包装或重复表达式时,模数运算符(
%
)通常是一个不错的选择。 For example, this solution is simple and robust against edge cases (eg, n > len(lst)
): 例如,此解决方案对于边缘情况(例如
n > len(lst)
)简单且健壮:
def get_n_preceding(val, lst, n):
start = lst.index(val) - n
return [lst[(start + x) % len(lst)] for x in range(n)]
def get_n_succeeding(val, lst, n):
start = lst.index(val) + 1
return [lst[(start + x) % len(lst)] for x in range(n)]
Results: 结果:
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
get_n_preceding(4, L, n=2) # [2, 3]
get_n_preceding(1, L, n=2) # [9, 10]
get_n_preceding(3, [1, 2, 3], n=5) # [1, 2, 3, 1, 2]
get_n_succeeding(7, L, n=2) # [8, 9]
get_n_succeeding(9, L, n=2) # [10, 1]
get_n_succeeding(3, [1, 2, 3], n=5) # [1, 2, 3, 1, 2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.