Python|列表中的切片产品
访问列表中的元素有许多类型和变体。这些是Python编程的重要组成部分,必须知道如何执行相同的操作。本文讨论了获取初始 K 元素并进行乘法的方法。让我们讨论执行此任务的特定解决方案。
方法#1:使用列表切片+循环
这个问题可以在 1 行中执行,而不是使用Python提供的列表切片功能使用循环,然后使用循环执行产品。
Python3
# Python3 code to demonstrate
# Sliced Product in List
# using list slicing + loop
# getting Product
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 5
# Sliced Product in List
# using list slicing + loop
res = prod(test_list[:K])
# print result
print("The first K elements product of list is : " + str(res))
Python3
# Python3 code to demonstrate
# Sliced Product in List
# using islice() + loop
from itertools import islice
# getting Product
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 5
# Sliced Product in List
# using islice() + loop
res = list(islice(test_list, 0, K))
res = prod(res)
# print result
print("The first K elements product of list is : " + str(res))
输出 :
The original list : [4, 5, 2, 6, 7, 8, 10]
The first K elements product of list is : 1680
方法 #2:使用 islice() + 循环
内置函数也可用于执行此特定任务。 islice函数可用于获取切片列表,然后 prod() 可用于执行产品。
Python3
# Python3 code to demonstrate
# Sliced Product in List
# using islice() + loop
from itertools import islice
# getting Product
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 5
# Sliced Product in List
# using islice() + loop
res = list(islice(test_list, 0, K))
res = prod(res)
# print result
print("The first K elements product of list is : " + str(res))
输出 :
The original list : [4, 5, 2, 6, 7, 8, 10]
The first K elements product of list is : 1680