七度&光 2009-01-29 22:45 采纳率: 22.2%
浏览 1242
已采纳

Python join: 为什么是 string.join (列表)而不是 list.join (string) ?

This has always confused me. It seems like this would be nicer:

my_list = ["Hello", "world"]
print my_list.join("-")
# Produce: "Hello-world"

Than this:

my_list = ["Hello", "world"]
print "-".join(my_list)
# Produce: "Hello-world"

Is there a specific reason it is like this?

转载于:https://stackoverflow.com/questions/493819/python-join-why-is-it-string-joinlist-instead-of-list-joinstring

  • 写回答

9条回答 默认 最新

  • 程序go 2009-01-29 22:51
    关注

    It's because any iterable can be joined, not just lists, but the result and the "joiner" are always strings.

    E.G:

    import urllib2
    print '\n############\n'.join(
        urllib2.urlopen('http://data.stackexchange.com/users/7095'))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波