[英]Loop inside an f-string as a value for an embed
I have a tuple with some values and I want to send them in an embed.我有一个包含一些值的元组,我想将它们发送到嵌入中。 They're inside a dictionary like this
dict = {key: [(1, 2, 3), other values here], other key: [(1, 2, 3, 4, 5), other values here]}
它们在这样的字典中
dict = {key: [(1, 2, 3), other values here], other key: [(1, 2, 3, 4, 5), other values here]}
Now some of the tuples here are of different lengths and it triggers me if I used a loop to add an embed field since discord doesn't allow the name parameter to be false or null yet.现在这里的一些元组长度不同,如果我使用循环添加嵌入字段,它会触发我,因为 discord 不允许 name 参数为 false 或 null。 If I use a 0 width whitespace character, there's a big space that I'd rather not have.
如果我使用宽度为 0 的空白字符,那么我宁愿没有很大的空间。 Tried using ternary operators but it didn't work.
尝试使用三元运算符,但没有奏效。 I also can't have this
for i in range(0, len(dict) - 1): pass
since the loop would've already came to an end before I could use it to index the tuple.我也不能让这个
for i in range(0, len(dict) - 1): pass
因为在我可以用它来索引元组之前循环已经结束了。 I also tried doing我也试过做
value = f'{tuple[i] for i in range(0, len(tuple) - 1)}'
but the bot return <generator object stats.<locals>.<genexpr> at 0x0000012E94AB3200>
instead of the values inside the tuple.但机器人返回
<generator object stats.<locals>.<genexpr> at 0x0000012E94AB3200>
而不是元组内的值。
Edit:编辑:
Thanks to the people who answered, It now works, thanks感谢回答的人,现在可以了,谢谢
tuple[i] for i in range(0, len(tuple) - 1)
Is a generator expression, it doesn't produce any values unless consumed by something like a loop or list()
是一个生成器表达式,它不会产生任何值,除非被循环或
list()
之类的东西消耗
You can use the equivalent list-comprehension instead:您可以改用等效的列表理解:
f'{[tuple[i] for i in range(0, len(tuple) - 1)]}'
Or put the generator inside a list()
或者将生成器放在
list()
f'{list(tuple[i] for i in range(0, len(tuple) - 1))}'
Because your comprehension is not wrapped in []
it is technically a generator expression ()
(I think this would have worked in python 2.7 though), try this:因为你的理解没有包含在
[]
中,所以它在技术上是一个生成器表达式()
(我认为这在 python 2.7 中可以工作),试试这个:
my_tuple = (1, 2, 3, 4)
f'{[my_tuple[i] for i in range(0, len(my_tuple) - 1)]}'
Output: Output:
[1, 2, 3]
Also, there are no tuple comprehensions in python because tuples are immutable.此外,在 python 中没有元组推导,因为元组是不可变的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.