【问题标题】:tuple operation in python 3.5python 3.5中的元组操作
【发布时间】:2016-06-03 07:43:58
【问题描述】:

我想通过以下方式从给定元组创建一个新元组:

1 1

2 3

3 6

4 10

5 15

两个元组应该具有相同的维度(元素数量)。 可能是这样的:

old_tuple[0] 移动到 new_tuple[0]

new_tuple[0] + old_tuple[1] 移动到 new_tuple[1]

new_tuple[1] + old_tuple[2] 移动到 new_tuple[2]

new_tuple[2] + old_tuple[3] 移动到 new_tuple[3]

new_tuple[3] + old_tuple[4] 移动到 new_tuple[4]

new_tuple[4] + old_tuple[5] 移动到 new_tuple[5]

等等……

【问题讨论】:

  • 我现在进步了一点。我发现我不能用元组做到这一点,所以我将元组更改为列表。此链接向我解释了列表理解,但我还没有完成它,因为我需要在此操作期间访问两个列表并且我不知道如何。 treyhunner.com/2015/12/python-list-comprehensions-now-in-color

标签: tuples python-3.5


【解决方案1】:

我终于明白了。将元组更改为列表后,我执行了以下操作:

L2 = [sum(L1[0:n+1]) for n in range(len(L1))]

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 2012-06-30
    • 2010-10-04
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多