473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
4 14947
bvdet
2,851 Expert Mod 2GB
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
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
dwblas
626 Expert 512MB
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
dwblas
626 Expert 512MB
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

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

Similar topics

4
by: Richard Shea | last post by:
Hi - I've writing a Python script which has a query which looks like this ... select * from T where C1 not in (1,2,3) .... C1 is a numeric column so elements of (1,2,3) must not be quoted...
2
by: Asad Khan | last post by:
Hello, I was using MySQL till a few days ago just fine. Now everytime I try to run it using mysql.exe, I get the following error: "can't connect to mysql server 'localhost' (10061)" What...
3
by: PC Datasheet | last post by:
I have three lines of text I need to concatenate into one "paragraph". The "paragraph" need to look like: Text1 Text2 Text3 where the lines of text are stacked in the "paragraph". When I use...
0
by: phmyhn | last post by:
I have two web pages, one is viewlarger.aspx, another one is shoppingcart.aspx. On the viewlarger.aspx, when clicking "add to cart" image button, the sub appends the id (passed from another page...
2
by: Morten | last post by:
Hi. My problem is that nearly all items in my dropdownlist is quite long and I would like to display the text in a textbox with a mouseover. When I move the mouse down the list I see the bg...
44
by: Tolga | last post by:
As far as I know, Perl is known as "there are many ways to do something" and Python is known as "there is only one way". Could you please explain this? How is this possible and is it *really* a...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
7
by: zheetee | last post by:
<script type="text/vbscript"> sub setTextBoxValue(a1,a2) thedelete.deleteTextBox.value = a1 end sub </script> <td> <a href="#"...
4
by: chaz | last post by:
here is the html : <br> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="<%=LABEL_WIDTH%>" class="formtext"><%= HTEXT("Connection type:")%></td> <td...
1
by: edgarwat | last post by:
I have a text file that I'm trying to parse with ifstream getline function. The problem is that I have to recognize single quotes in the middle of words as well as on the end of words. the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.