Python - 从循环构建字符串

标签 python string

我有一组具有纬度和经度值的点,我正在尝试创建以下格式的字符串以便使用 Gmaps API:

 LatitudeX,LongitudeX|LatitudeY,LongitudeY

最终的字符串将使用未知数量的经纬对构建。

我以前的经验是使用 PHP,虽然我已经设法让一些东西工作,但是它看起来有点笨拙,我想知道是否有更“pythonic”的方式来实现结果。

这是我目前所拥有的:

 waypoints = ''

 for point in points:
     waypoints = waypoints+"%s,%s" % (point.latitude, point.longitude)+"|"

 waypoints = waypoints[:-1]

感谢任何建议。

谢谢

最佳答案

使用str.join :

waypoints = '|'.join("{0},{1}".format(p.latitude, p.longitude) for p in points)

关于Python - 从循环构建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8099584/

相关文章:

python - AWS Glue 限制输入大小

python - 基于边缘宽度的 matplotlib 图例

javascript - 将输入字符串分解为单个单词

c# - 字符串在 Visual Studio 调试器中包含引号,但 string.contains 返回 false

javascript排除 "00"和 "01"字符串

string - 多重 split

python - 索引错误 : too many indices for array while plotting ROC curve with scikit-learn?

python - numpy.where() 函数仅用于精确匹配?

python - 如何将传感器数据存储到 Apache Hadoop HDFS、Hive、HBase 或其他

c# - String 或 StringBuilder 返回值?