以下是使用Python编写的解决方法:
numbers = [1, 2, 3, 4, 5]
prices = [10, 20, 30, 40, 50]
result = []
for i in range(len(numbers)):
if numbers[i] in numbers[:i]:
continue
total_price = prices[i]
for j in range(i+1, len(numbers)):
if numbers[j] == numbers[i]:
total_price += prices[j]
result.append((numbers[i], total_price))
print(result)
输出结果为:
[(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]
解释:首先,我们定义了两个列表numbers
和prices
,分别代表数字和对应的价格。然后,我们创建了一个空列表result
用于存储最终结果。接下来,我们使用两层循环遍历numbers
列表,首先找到第一个没有重复的数字(即列表中第一次出现的数字),然后从这个位置开始遍历后面的数字,如果遇到相同的数字,就将对应的价格相加,并将结果存入total_price
变量中。最后,我们将结果以元组的形式(数字,总价格)添加到result
列表中。最后,我们打印出result
列表作为最终结果。
上一篇:编写两个函数,提示用户输入数字并验证输入是否为数字。
下一篇:编写灵活的正则表达式