3 回答

TA贡献1966条经验 获得超4个赞
根据您最清楚的解释,这是我的建议。我希望它符合您的期望:
L = [ 10, 2, 56, 33, 23, 1, 564, 32, 122, 42, 3, 4, 2, 1, 3, 2, 1, 54, 5, 9, 1, 65, 254 ]
# Ask for user input
myInput = int(input("Enter a number: ")) # ex: I entered 5
# Replace the numbers in L that are greater than 100 with the input number
L = [myInput if i > 100 else i for i in L]
print(L) # ex: [10, 2, 56, 33, 23, 1, 5, 32, 5, 42, 3, 4, 2, 1, 3, 2, 1, 54, 5, 9, 1, 65, 5]
# Take every 5th element of L, multiply it by 2 and place the results into a brand new list K
K = [value*2 for i,value in enumerate(L,1) if i % 5 == 0]
print(K) # ex: [46, 84, 6, 18]
# Merge L and K into LK
LK = L + K
print(LK) # ex: [10, 2, 56, 33, 23, 1, 5, 32, 5, 42, 3, 4, 2, 1, 3, 2, 1, 54, 5, 9, 1, 65, 5, 46, 84, 6, 18]

TA贡献1785条经验 获得超8个赞
根据以上评论,这是您的问题的解决方案。如果这不是您想要的,请告诉我,我会相应地进行更新。我在这里使用列表推导。我曾经(i+1)%5访问过第5个索引,因为该索引从0python开始。
L = [ 10, 2, 56, 33, 23, 1, 564, 32, 122, 42, 3, 4, 2, 1, 3, 2, 1, 54, 5, 9, 1, 65, 254 ]
x = int(input("Insert number here: "))
L1 = [x if i > 100 else i for i in L]
L2 = [2*j if (i+1)%5==0 else j for i, j in enumerate(L1)]
L_output = L1 + L2
print (L1)
print (L2)
print (L_output)
输出
Insert number here: 6
[10, 2, 56, 33, 23, 1, 6, 32, 6, 42, 3, 4, 2, 1, 3, 2, 1, 54, 5, 9, 1, 65, 6]
[10, 2, 56, 33, 46, 1, 4, 32, 4, 84, 3, 4, 2, 1, 6, 2, 1, 54, 5, 18, 1, 65, 4]
[10, 2, 56, 33, 23, 1, 6, 32, 6, 42, 3, 4, 2, 1, 3, 2, 1, 54, 5, 9, 1, 65, 6, 10, 2, 56, 33, 46, 1, 4, 32, 4, 84, 3, 4, 2, 1, 6, 2, 1, 54, 5, 18, 1, 65, 4]

TA贡献1809条经验 获得超8个赞
也许这样可以帮助:
l1 = range(100)
l2 = [l1[x]*2 if x%5==4 else l1[x] for x in range(len(l1)) ]
print(l2)
它仅修改每五个元素:因此,这些元素在第4、9、14等位置。(因此,x模5等于4)其他元素保持原样。
添加回答
举报