>> -6ren">
gpt4 book ai didi

Python遍历 float 宽度列表: TypeError: sequence expected,生成器发现

转载 作者:行者123 更新时间:2023-11-30 23:48:14 25 4
gpt4 key购买 nike

我正在尝试迭代变化的 float 宽度列表。

[10.5, 15.5, 3.7]  <- Randomly generated 

我正在使用这个 float 列表在我尝试打印的字符串列表之间生成空格。我正在通过

print ''.join('%*s' %i for i in zip(WIDTHS, LIST_OF_STRINGS))

我收到错误

TypeError: sequence expected, generator found

谁能解释一下为什么我会收到此错误?

编辑:Python 版本 2.4

最佳答案

您实际上应该收到错误消息

TypeError: * wants int

首先将 float 转换为整数:

widths = map(int, widths)

示例:

>>> widths = [10.5, 15.5, 3.7]
>>> s = ["a", "b", "c"]
>>> widths = map(int, widths)
>>> ''.join('%*s' %i for i in zip(widths, s))
' a b c'

关于Python遍历 float 宽度列表: TypeError: sequence expected,生成器发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8037888/

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