日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python -- join()

發(fā)布時(shí)間:2023/12/2 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python -- join() 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python -- join()

pythonjoinos


月似當(dāng)時(shí),人似當(dāng)時(shí)否?


在 python 中,一共有兩個(gè) join 方法,一個(gè)是 str.join(),另一個(gè)是 os.path.join() ,這里只了解前一種


str.join(iterable)

官方文檔
Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.

簡(jiǎn)單來講,就是將可迭代對(duì)象中的元素以 str 為分隔符拼接返回,但是這些元素必須為 String 類型,不然會(huì)報(bào)錯(cuò)

# 報(bào)錯(cuò) >>> l = [1,2,3] >>> " ".join(l) Traceback (most recent call last):File "<stdin>", line 1, in <module> TypeError: sequence item 0: expected str instance, int found# 對(duì) list 拼接 >>> l = ["hello","pinsily"] >>> " ".join(l) 'hello pinsily'# 對(duì) string 拼接 >>> s = "hello pinsily" >>> ":".join(s) 'h:e:l:l:o: :p:i:n:s:i:l:y'# 對(duì)元組拼接 >>> t = ("hello","pinsily") >>> " ".join(t) 'hello pinsily'# 對(duì)字典拼接 >>> d = {"hello":"1","pinsily":"2","and":"3","world":"4"} >>> " ".join(d) 'hello pinsily and world'

:當(dāng)要使用大量的字符串拼接時(shí),盡量避免 + 操作,這樣會(huì)產(chǎn)生大量的臨時(shí)變量,占據(jù)內(nèi)存,可以先將其拼接到 list 中,然后使用 join 方法


轉(zhuǎn)載于:https://www.cnblogs.com/pinsily/p/7904191.html

總結(jié)

以上是生活随笔為你收集整理的python -- join()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。