python - 编辑列表以在不同位置添加字符串以输出格式化字符串

标签 python string-formatting

我有一列数字:

195,191
22,23
18,252
172,221

我想高效地添加以下字符到其上,并将其转换为字符串以输出此内容空闲时:

goto(195+position,(191 * xy) - y)
goto(22+position,(23 * xy) - y)
goto(18+position,(252 * xy) - y)
goto(172+position,(221 * xy) - y)

字符:goto(, +position, (, * xy), -y) 必须位于如上所示的位置。

我尝试使用Python的[:]对数组进行切片,但它不起作用,因为数据中的数据数字在2到3位数字之间变化。

最佳答案

尝试使用splitformat 一起像这样:

lines = ["195,191", "22,23", "18,252", "172,221"]
for s in lines:
    print("goto({0[0]}+position,({0[1]} * xy) - y)".format(s.split(",")))

这将打印

goto(195+position,(191 * xy) - y)
goto(22+position,(23 * xy) - y)
goto(18+position,(252 * xy) - y)
goto(172+position,(221 * xy) - y)

关于python - 编辑列表以在不同位置添加字符串以输出格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36734323/

相关文章:

python - 在单个数据框中合并具有相同索引的多行?

Python Pandas 数据格式化

format - 将 REAL 的数组行转换为逗号分隔的字符串

python - 在类方法文档字符串中使用字符串格式

c# - 字符串格式 : Hexadecimal ("X") Format Specifier and Zero (0)

python - 通过 SSH 在后台执行远程 python 脚本

python - 如何缩短这个 bool 表达式?

python - 重采样后合并 pandas DataFrame

C 字符串格式化等效项

c# - 整数的字符串格式 c#