gpt4 book ai didi

python - 从末尾开始有效地列出元组中的项目

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:01 25 4
gpt4 key购买 nike

我想在 Python 中列出一个元组中的项目,从后面开始,然后到前面。类似于:

foo_t = tuple(int(f) for f in foo)
print foo, foo_t[len(foo_t)-1] ...

我相信这应该可以在没有 Try ...-4 的情况下实现,除了 ...-3。想法?有什么建议吗?

最佳答案

你可以打印tuple(reversed(foo_t)),或者使用list代替tuple,或者

print ' '.join(str(x) for x in reversed(foo_t))

和许多变体。您也可以使用 foo_t[::-1],但我认为 reversed 内置函数更具可读性。

关于python - 从末尾开始有效地列出元组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2659556/

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