Hey,

Kinda new to this, and trying to assemble some simple code, but I'm having a couple of issues that I hoped someone would be able to help me out with.

1) I would like to be able to remove duplicates from a submitted sentence, leaving only the first occurance of each letter present, treating capital and lowercase letters differently. So 'Apple' would become 'Aple' etc.

2) The other thing I would like to be able to do is make a list appear as a single sentence.
So when submitted would appear as "My name is frank".

Thank you in advance!

Have you tried coding anything yourself?
Things like printing the contents of a list are fairly basic and you should be able to find out how in the python documentation or some basic python tutorial

for x in y:
   print y

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

That's brilliant thanks, not sure why I didn't think of that myself, so simple.

Would anyone have a fix for the duplicates issue?

Thanks again!

word = "Apple"
modWord = ""

for i in range(len(word)):
    found = False
    for j in range(len(modWord)):
        if word[i] == modWord[j]:
            found = True
    if not found:
        modWord = modWord + word[i]

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

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

Hey,

Thanks for that, it works, I created something like it (thank god for learning), but the issue I'm having now is that both remove all duplicated characters, including spaces. Is there any way I could have it for alphanumeric characters only, or just to excude spaces from the removal?

Thanks!

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'
>>>

Thanks for that, but is there any way to remove the dupes from later on as well, not just the adjacent letters?

E.G:
'The quick brown fox jumps over the lazy dog'
Turns into:
'The quick brown fx jmps v t lazy dg'

Thanks for your help!

Yeah python code reads nice my code was probably more of a c++ style. I guess old habits die hard

The easiest thing would probably be to split the string

word = "Once upon a time"
wordList = word.split()

for word in wordList:
   #code like before

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.