I have encountering a problem while concatenating string with a list. What I did, shows below, -
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?
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? - 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
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... -
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.
Note that "temp" is a list of lists. To solve the problem, add a print statement - 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
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. - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: zheetee |
last post by:
<script type="text/vbscript">
sub setTextBoxValue(a1,a2)
thedelete.deleteTextBox.value = a1
end sub
</script>
<td>
<a href="#"...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
| |