The second one is a simple funciton
#our list
l = ['Hello','world','from','python']
#use the join() function to join the words together with spaces
string_list = ' '.join(l)
print string_list
#prints
#Hello world from python
Im pretty sure that changed for python 30. Im not how you do it there
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
Python code can be written so it almost reads like pseudo code:
word = "Apple"
# create an empty string
new_word = ""
# iterate through word one character at a time
for c in word:
# check if c is already in new_word
if c not in new_word:
new_word += c
print(new_word) # Aple
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
Here's a way to only check against the last character (as a function):
>>> def remove_dups( word ):
... """ Remove all adjacent duplicate letters """
... new_word = word[0]
... for c in word:
... if c != new_word[-1]:
... new_word += c
... return new_word
...
>>> remove_dups ( "This phhrasse hhhhhas mmuullttiippllee repeaters" )
'This phrase has multiple repeaters'
>>> remove_dups( "Apple" )
'Aple'
>>>
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
A simple addition to the if statement will help you to treat spaces different:
#!/usr/bin/python
# remove duplicates from text (leave spaces)
text = 'The quick brown fox jumps over the lazy dog'
# create an empty string
new_text = ""
# iterate over all characters of the text
for c in text:
# check if c is a space or not in new_text
if c.isspace() or c not in new_text:
# concatinate c to new_text
new_text += c
print(new_text)
"""
my result -->
The quick brown fx jmps v t lazy dg
"""
The first line is for my Ubuntu/Linux machine, it shows it where the Python interpreter is located. Windows will ignore this line.
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
One of you should have just written the entire program in the beginning, since the OP didn't have to produce even one line of original code, and saved everyone the trouble of having to read through all of the posts.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714