Python - 列表中的相邻元素
给定一个列表,为每个元素提取下一个和上一个元素。
Input : test_list = [3, 7, 9, 3]
Output : [(None, 7), (3, 9), (7, 3), (9, None)]
Explanation : for 7 left element is 3 and right, 9.
Input : test_list = [3, 7, 3]
Output : [(None, 7), (3, 3), (7, None)]
Explanation : for 7 left element is 3 and right, 3.
方法:使用循环
在这里,我们迭代每个元素并使用元素访问获取下一个和上一个元素。
Python3
# Python3 code to demonstrate working of
# Adjacent elements in List
# Using loop
# Function to find adjacent
# elements in List
def findAdjacentElements(test_list):
res = []
for idx, ele in enumerate(test_list):
# Checking for all cases to append
if idx == 0:
res.append((None, test_list[idx + 1]))
elif idx == len(test_list) - 1:
res.append((test_list[idx - 1], None))
else:
res.append((test_list[idx - 1], test_list[idx + 1]))
return res
# Initializing list
input_list = [3, 7, 8, 2, 1, 5, 8, 9, 3]
# Printing original list
print("The original list is:", input_list)
# printing result
print("The Adjacent elements list:", findAdjacentElements(input_list))
输出:
The original list is: [3, 7, 8, 2, 1, 5, 8, 9, 3]
The Adjacent elements list: [(None, 7), (3, 8), (7, 2), (8, 1), (2, 5), (1, 8), (5, 9), (8, 3), (9, None)]