gpt4 book ai didi

python - 根据输入对大列表进行切片

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:54 26 4
gpt4 key购买 nike

如果我有多个这样的列表

hello = [1,3,5,7,9,11,13]

bye = [2,4,6,8,10,12,14]

并且用户输入3

有没有办法让输出返回列表中的 3 个索引并从那里开始获取:

9 10

11 12

13 14

每个空格之间有制表符 \t

如果用户输入5

预期的输出是

5 6

7 8

9 10

11 12

13 14

我试过了

for i in range(user_input):
print(hello[-i-1], '\t', bye[-i-1])

最佳答案

只需使用从末尾开始减去用户输入 (-user_input) 并移动到末尾 (-1) 的负索引,例如:

for i in range(-user_input, 0):
print(hello[i], bye[i])

关于python - 根据输入对大列表进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54837236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com