gpt4 book ai didi

python - 在 f 字符串内循环作为嵌入的值

转载 作者:行者123 更新时间:2023-12-04 08:04:02 26 4
gpt4 key购买 nike

我有一个带有一些值的元组,我想以嵌入方式发送它们。他们在这样的字典里dict = {key: [(1, 2, 3), other values here], other key: [(1, 2, 3, 4, 5), other values here]}现在这里的一些元组长度不同,如果我使用循环添加嵌入字段,它会触发我,因为 discord 还不允许 name 参数为 false 或 null。如果我使用 0 宽度的空白字符,那么我宁愿没有一个很大的空间。尝试使用三元运算符,但没有奏效。我也不能有这个for i in range(0, len(dict) - 1): pass因为循环在我可以使用它来索引元组之前就已经结束了。我也试过做value = f'{tuple[i] for i in range(0, len(tuple) - 1)}'但机器人返回 <generator object stats.<locals>.<genexpr> at 0x0000012E94AB3200>而不是元组内的值。
编辑:
感谢回答的人!现在可以用了,谢谢

最佳答案

tuple[i] for i in range(0, len(tuple) - 1)
是一个生成器表达式,它不会产生任何值,除非被循环或 list() 之类的东西消耗掉。
您可以改用等效的列表理解:
f'{[tuple[i] for i in range(0, len(tuple) - 1)]}'
或者把发电机放在 list() 里面
f'{list(tuple[i] for i in range(0, len(tuple) - 1))}'

关于python - 在 f 字符串内循环作为嵌入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66309971/

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