Python 在列表中进行K差索引配对
K是一个特殊的数字,用于设置从第0个索引开始的差值之和。例如,如果k = 3,则表示将第0个索引添加到K,并找到精确配对。在Python中,我们有一些内置函数,例如str(),len()和append(),将用于解决列表中的K差索引配对问题。
让我们举一个列表的例子。
给定的列表, [“A”, “B”, “C”, “D”, “E”, “F”]
然后将K值设置为1
最后的输出将索引配对为 [“AC”, “BD”, “CE”, “DF”]
语法
在示例中使用以下语法-
str()
内置函数str()将值转换为字符串,以便与其他字符串集成。
len()
len()是Python中的一个内置函数,用于返回对象的长度。
append()
内置函数append()将元素添加到列表的末尾。
使用map()函数
在以下示例中,开始程序与提供与运算符相关的各种内置函数的operator库。然后创建原始列表my_list来存储字符串并显示列表。接下来,初始化K的初始值,该值将用于设置列表中的差异索引配对。然后使用内置函数map(),该函数接受一些参数,operator.concat(用于索引配对),和切片标记(my_list[:-1], my_list[K:]),根据K的值与特定索引配对。最后,显示结果。
示例
import operator
# initialize list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
# print the original list
print("The input list : " + str(my_list))
# Initiaze the initial value of K
K = 4
# The K difference index pair using map()
result = list(map(operator.concat, my_list[:-1], my_list[K:]))
# printing result
print("List after K difference concatenation is : " + str(result))
输出
The input list
['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S', 'P', 'O', 'I', 'N', 'T']
List after K difference concatenation is : ['TR', 'UI', 'TA', 'OL', 'RS', 'IP', 'AO', 'LI', 'SN', 'PT']
使用递归
在下面的示例中,程序使用递归函数f_diff_pairs(),接受两个参数l和k,分别从原始列表变量my_list和K值中获取值。然后使用第一个if语句设置条件,即K值小于等于原始列表的长度,并返回一个空列表。然后使用另一个if语句设置条件,即如果K的值等于1,则返回列表推导式来计算从第一个索引开始的K差异,否则将返回从原始列表的第0个索引开始的索引配对列表。
示例
def K_diff_pairs(l, K):
if K >= len(l):
return []
if K == 1:
return [l[i]+_list[i+1] for i in range(len(l)-1)]
return [l[0]+l[K]] + K_diff_pairs(l[1:], K)
# Intialization of original list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
# printing original list
print("The original input list: " + str(my_list))
# initialize the initial value of K
K = 3
# K difference index pairing in list
res = K_diff_pairs(my_list, K)
# print the result
print("The K difference index pairing: " + str(res))
输出
The original input list: ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S', 'P', 'O', 'I', 'N', 'T']
The K difference index pairing: ['TO', 'UR', 'TI', 'OA', 'RL', 'IS', 'AP', 'LO', 'SI', 'PN', 'OT']
使用简单的 while 循环
在下面的例子中,首先将输入列表初始化为变量 my_list,并显示出来。然后将 K 值初始化为 2,这意味着每个索引都与后一个索引的差为 2。在变量 res 中初始化一个空列表,用于存储最终结果。使用 while 循环,变量 i 遍历原始列表的长度 (length(my_list) – k)。然后使用内置函数 append(),通过计算原始列表索引 my_list[i] 与 k 值即 my_list[i+k] 的和来插入元素。然后使用 += 运算符来使变量迭代到原始列表的特定索引。最后,打印结果。
示例
# Initialization of original list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
# print the original list
print("The input list: " + str(my_list))
# Initialize the initial value of K
K = 2
# Using a while loop: K difference index pairing in list
res = []
# Initialize the initial value of i
i = 0
while i < len(my_list) - K:
res.append(my_list[i] + my_list[i+K])
i += 1
# print the final result
print("The K difference index pairing: " + str(res))
输出
The input list: ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S', 'P', 'O', 'I', 'N', 'T']
The K difference index pairing: ['TT', 'UO', 'TR', 'OI', 'RA', 'IL', 'AS', 'LP', 'SO', 'PI', 'ON', 'IT']
结论
K的差异是通过使用某些特定条件和操作来配对两个元素列表的特定值。该程序使用各种内置方法,如列表推导、while循环、map()和list()。这种类型的程序通常用于建立需求数据的连接。