2015-04-02 80 views
0

有一個錯誤,當我試圖分裂AttributeError錯誤:對象有沒有屬性「分裂」

l =[u'this is friday', u'holiday begin'] 
split_l =l.split() 
print(split_l) 

的錯誤是:

Traceback (most recent call last): 
    File "C:\Users\spotify_track2.py", line 19, in <module> 
    split_l =l.split() 
AttributeError: 'list' object has no attribute 'split' 

所以我沒有想法處理這種錯誤。

+1

提到什麼是你想用'分裂()'做什麼? – Shashank 2015-04-02 16:56:47

+0

顯然,「處理這種錯誤」的最好方法是避免嘗試調用不存在xD的方法。現在你想要做什麼?你期望'list.split'返回什麼? – 2015-04-02 16:57:40

+0

能以任何方式獲得這樣的打印嗎? a = ['this','is',friday','holiday','begin'] @Shashank – user3849475 2015-04-02 17:00:01

回答

3

首先,list

其次list不具備的功能split這是str它有它不命名您的變量。

檢查文檔str.split

Return a list of the words in the string, using sep as the delimiter string

(重點煤礦)

所以,你需要做的

l =[u'this is friday', u'holiday begin'] 
split_list =[i.split() for i in l] 
print(split_list) 

這將打印

[[u'this', u'is', u'friday'], [u'holiday', u'begin']] 

發表評論編輯

爲了得到你的預期,你可以嘗試

>>> l =[u'this is friday', u'holiday begin'] 
>>> " ".join(l).split(" ") 
[u'this', u'is', u'friday', u'holiday', u'begin'] 

或低於

>>> [j for i in split_list for j in i] 
[u'this', u'is', u'friday', u'holiday', u'begin'] 
+0

回覆非常快:) – itzMEonTV 2015-04-02 16:59:11

+0

@itzmeontv謝謝! – 2015-04-02 16:59:46

+0

Bhargav Rao。所以,治療? :p – itzMEonTV 2015-04-02 17:03:18

相關問題