Python - 连续元素功率的总和
给定一个列表,任务是编写一个Python程序来计算连续元素出现频率的幂的总和。
例子:
Input : test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
Output : 110
Explanation : 2^3 + 3^4 + 4^2 + 5 = 110
Input : test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4]
Output : 105
Explanation : 2^3 + 3^4 + 4^2 = 105
方法#1:使用循环
在此,我们对每个元素进行迭代并测试下一个元素,如果发现不同,则对连续元素的幂进行求和,否则添加计数器以获取频率以增加数量。
Python3
# Python3 code to demonstrate working of
# Summation of consecutive elements power
# Using loop
# initializing list
test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
# printing original lists
print("The original list is : " + str(test_list))
freq = 1
res = 0
for idx in range(0, len(test_list) - 1):
# adding powers
if test_list[idx] != test_list[idx + 1]:
res = res + test_list[idx] ** freq
freq = 1
else:
freq += 1
# catering for last element
res = res + test_list[len(test_list) - 1] ** freq
# printing result
print("Computed summation of powers : " + str(res))
Python3
# Python3 code to demonstrate working of
# Summation of consecutive elements power
# Using defaultdict() + loop + sum()
from collections import defaultdict
# initializing list
test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
# printing original lists
print("The original list is : " + str(test_list))
# getting frequency
temp = defaultdict(int)
for ele in test_list:
temp[ele] += 1
temp = dict(temp)
# computing summation
res = sum([key ** temp[key] for key in temp])
# printing result
print("Computed summation of powers : " + str(res))
输出
The original list is : [2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
Computed summation of powers : 110
方法 #2:使用defaultdict() + loop + sum()
在这里,我们使用键值对使用 defautldict 捕获频率,并使用循环来遍历每个元素。接下来,使用 sum() 执行求和。
蟒蛇3
# Python3 code to demonstrate working of
# Summation of consecutive elements power
# Using defaultdict() + loop + sum()
from collections import defaultdict
# initializing list
test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
# printing original lists
print("The original list is : " + str(test_list))
# getting frequency
temp = defaultdict(int)
for ele in test_list:
temp[ele] += 1
temp = dict(temp)
# computing summation
res = sum([key ** temp[key] for key in temp])
# printing result
print("Computed summation of powers : " + str(res))
输出
The original list is : [2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
Computed summation of powers : 110