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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saad Bin Ahmed
    New Member
    • Jul 2011
    • 25

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

    I have encountering a problem while concatenating string with a list. What I did, shows below,
    Code:
    w=''
    wordTargetStrings=[]
    target_s=['000','i','t','sp','ga','i','s','ga','sp','s','t','r','i','n','g','sp'] 
    
    for t in range(len(target_s)):
         if target_s != "sp" and target_s!= "ga" and target_s !="pt":
               w=w+target_s[t]
         else: 
               wordTargetStrings.append(w)
               print "wts", wordTargetStrings
    When I executing this program, it shows me the following error.
    TypeError: can only concatenate list (not "str") to list

    Any suggestion?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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?
    Code:
    w=''
    wordTargetStrings=[]
    target_s=['000','i','t','sp','ga','i','s','ga','sp','s','t','r','i','n','g','sp'] 
     
    for word in target_s:
        if word in ["sp", "ga", "pt"]:
            w += word
        else: 
            wordTargetStrings.append(w)
            print "wts", wordTargetStrings

    Comment

    • Saad Bin Ahmed
      New Member
      • Jul 2011
      • 25

      #3
      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...
      Code:
      target_s=[]
      wordTargetStrings=[]
      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"],["."]]
      for f in range(len(temp)):
      	if  "." not in temp[f]:
      		target_s.append(temp[f])
      		
      	else:
      		break					
      print target_s
      for t in range(len(target_s)):
      				
      	if "sp" not in target_s[t] and "ga" not in target_s[t] and "pt" not in target_s[t]: 
      			
      		w=w+target_s[t]  ## read list element until find sp
      	else: 
      		wordTargetStrings.append(w)  ## whenever find sp append wordTargetStrings by w
      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.

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Note that "temp" is a list of lists. To solve the problem, add a print statement
        Code:
        target_s=[]
        wordTargetStrings=[]
        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"],["."]]
        for f in range(len(temp)):
            if  "." not in temp[f]:
                target_s.append(temp[f])
         
            else:
                break                    
        print target_s
        
        w= ""
        for t in range(len(target_s)):
         
            if "sp" not in target_s[t] and "ga" not in target_s[t] and "pt" not in target_s[t]: 
                print type(target_s[t]), target_s[t]
                w=w+target_s[t]  ## read list element until find sp
            else: 
                wordTargetStrings.append(w)  ## whenever find sp append wordTargetStrings by w

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          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.
          Code:
          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"],["."]]
          temp_chrs = [x[0] for x in temp]
          
          words_list = []
          this_word = []
          for el in temp_chrs:
              if el not in ("sp", "ga", "pt"):
                  this_word.append(el)
              else:     ## end of word 
                  words_list.append("".join(this_word))
                  this_word = []
          if len(this_word):
              words_list.append("".join(this_word))
          
          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.

          Comment

          Working...