By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,813 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,813 IT Pros & Developers. It's quick & easy.

"can only concatenate list (not "str") to list"

P: 25
I have encountering a problem while concatenating string with a list. What I did, shows below,
Expand|Select|Wrap|Line Numbers
  1. w=''
  2. wordTargetStrings=[]
  3. target_s=['000','i','t','sp','ga','i','s','ga','sp','s','t','r','i','n','g','sp'] 
  4.  
  5. for t in range(len(target_s)):
  6.      if target_s != "sp" and target_s!= "ga" and target_s !="pt":
  7.            w=w+target_s[t]
  8.      else: 
  9.            wordTargetStrings.append(w)
  10.            print "wts", wordTargetStrings
  11.  
  12.  
When I executing this program, it shows me the following error.
TypeError: can only concatenate list (not "str") to list

Any suggestion?
Aug 21 '11 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
When I tested your code, I did not get the TypeError you did. You code will never reach the else statement. I made some modifications to your code. What are your trying to achieve?
Expand|Select|Wrap|Line Numbers
  1. w=''
  2. wordTargetStrings=[]
  3. target_s=['000','i','t','sp','ga','i','s','ga','sp','s','t','r','i','n','g','sp'] 
  4.  
  5. for word in target_s:
  6.     if word in ["sp", "ga", "pt"]:
  7.         w += word
  8.     else: 
  9.         wordTargetStrings.append(w)
  10.         print "wts", wordTargetStrings
Aug 21 '11 #2

P: 25
Actually I want to read the words. As list elements are the characters and "sp" indicate the end of word. So, read the list until find sp. Whenever find sp the previously appended characters will be a word.
This is not what I desire. My program actually is...
Expand|Select|Wrap|Line Numbers
  1. target_s=[]
  2. wordTargetStrings=[]
  3. temp=[["T"],["h"],["i"],["s"],["sp"],["i"],["s"],["sp"],["s"],["t"],["r"],["i"],["n"],["g"],["sp"],["."],["002"],["N"],["o"],["w"],["sp"],["o"],["r"],["sp"],["N"],["e"],["v"],["e"],["r"],["sp"],["."]]
  4. for f in range(len(temp)):
  5.     if  "." not in temp[f]:
  6.         target_s.append(temp[f])
  7.  
  8.     else:
  9.         break                    
  10. print target_s
  11. for t in range(len(target_s)):
  12.  
  13.     if "sp" not in target_s[t] and "ga" not in target_s[t] and "pt" not in target_s[t]: 
  14.  
  15.         w=w+target_s[t]  ## read list element until find sp
  16.     else: 
  17.         wordTargetStrings.append(w)  ## whenever find sp append wordTargetStrings by w
  18.  
  19.  
I am suppose to read temp which contains list of elements. Whenever find "." its mean end of sentence, whenever finds "sp its mean end of word". I want to read words in a sentence. But it shows me an error in w=w+target_s[t] and not be concatenating list with string. After that I have to read characters in a word.
Aug 21 '11 #3

Expert 100+
P: 624
Note that "temp" is a list of lists. To solve the problem, add a print statement
Expand|Select|Wrap|Line Numbers
  1. target_s=[]
  2. wordTargetStrings=[]
  3. temp=[["T"],["h"],["i"],["s"],["sp"],["i"],["s"],["sp"],["s"],["t"],["r"],["i"],["n"],["g"],["sp"],["."],["002"],["N"],["o"],["w"],["sp"],["o"],["r"],["sp"],["N"],["e"],["v"],["e"],["r"],["sp"],["."]]
  4. for f in range(len(temp)):
  5.     if  "." not in temp[f]:
  6.         target_s.append(temp[f])
  7.  
  8.     else:
  9.         break                    
  10. print target_s
  11.  
  12. w= ""
  13. for t in range(len(target_s)):
  14.  
  15.     if "sp" not in target_s[t] and "ga" not in target_s[t] and "pt" not in target_s[t]: 
  16.         print type(target_s[t]), target_s[t]
  17.         w=w+target_s[t]  ## read list element until find sp
  18.     else: 
  19.         wordTargetStrings.append(w)  ## whenever find sp append wordTargetStrings by w 
Aug 21 '11 #4

Expert 100+
P: 624
Note bvdet's suggestion. Breaking on a given trigger is common in programming. You add the bytes to a list, and then process the list when the break is found.
Expand|Select|Wrap|Line Numbers
  1. temp=[["T"],["h"],["i"],["s"],["sp"],["i"],["s"],["sp"],["s"],["t"],["r"],["i"],["n"],["g"],["sp"],["."],["002"],["N"],["o"],["w"],["sp"],["o"],["r"],["sp"],["N"],["e"],["v"],["e"],["r"],["sp"],["."]]
  2. temp_chrs = [x[0] for x in temp]
  3.  
  4. words_list = []
  5. this_word = []
  6. for el in temp_chrs:
  7.     if el not in ("sp", "ga", "pt"):
  8.         this_word.append(el)
  9.     else:     ## end of word 
  10.         words_list.append("".join(this_word))
  11.         this_word = []
  12. if len(this_word):
  13.     words_list.append("".join(this_word))
  14.  
  15. print words_list 
You are left to deal with the period yourself. If any of this code is confusing, add some print statements and/or break it into it's parts.
Aug 21 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.