Coolkid
Coolkid

Reputation: 33

How to delete lower case from a list and reverse it

The aim is trying to reverse the message, delete all the strings that starts with lower case letter and at the end make everything lower case.

This is my code:

msg = input('code: ')
msg = msg.split()
msg = list(reversed(msg))
for i in msg:
  if i[0] == i[0].lower:
    del msg[i]
msg = ' '.join(msg)
msg = msg.lower()
print(msg)

Here is one example

BaSe fOO ThE AttAcK

Then it will turn into

attack the base

Thank you guys so much!!!!!!!!! so i went back and edited my code:

new_msg = []
msg = input('code: ')
msg = msg.split()
msg = list(reversed(msg))
for word in msg:
  if not word[0].islower():
    new_msg.append(word.lower())
new_msg = ' '.join(new_msg)
print("says:",new_msg)

and i went back to mark it but it said you can't have punctuation! yeah, i think i need help again, how do i get rid of them?

Upvotes: 0

Views: 1119

Answers (6)

jithinodattu
jithinodattu

Reputation: 90

" ".join([word.lower() for word in reversed(msg.split()) if not word[0].islower()])

Upvotes: -1

user1796519
user1796519

Reputation: 1

I suggest you don't delete any element in list/Dict iteration in any case. If you do, it will be wrong.

Like that:

sMsg="BaSe fOO The AttAck"
lstMsg=sMsg.split(" ")
for i in xrange(len(lstMsg)):
    if lstMsg[i][0].islower():
        del lstMsg[i]

Upvotes: -1

Selva Mathan
Selva Mathan

Reputation: 31

string = raw_input().split()                                                     
string2 = []                                                                     
for i in range(len(string)):                                                     
    if not string[i].[0].islower():                  
        string2.insert(0,string[i].lower())                                              
print ' '.join(string2)                                                          

Upvotes: -1

blasko
blasko

Reputation: 690

This looks a little intimidating, but I'll explain it.

s = "BaSe fOO ThE AttAcK"
print ' '.join(reversed([word.lower() for word in s.split(' ') if not word[0].islower()]))

What I'm doing here is splitting s at a single space ' ', which then creates a list of anything not a single ' '. Then I iterate through the list of "words" and convert each word to lower using the built-in lower method only if the first character of the word (which we get by calling the 0th index) is not lower. Lastly we join the list we created using the join method.

Edit: I forgot to reverse the list.

Upvotes: 1

Stiffo
Stiffo

Reputation: 818

new_msg = []
msg = msg.split()
for word in msg: #For every word in the string
    if not word[0].islower(): #if it starts with anything other than lowercase
        new_msg.append(word.lower()) #add to new list
new_msg = ' '.join(new_msg[::-1]) #Reverse order of list and make into string

Upvotes: 2

lynn
lynn

Reputation: 10794

Here's why your code doesn't work, at least:

>>> i = 'fOO'

>>> i[0] == i[0].lower     # huh?
False

>>> i[0].lower             # oh!
<built-in method lower of str object at 0x00249F40>

>>> i[0].lower()           # we forgot to *call* it
'f'

>>> i[0] == i[0].lower()   # perfect!
True

You're comparing a string to a method object -- that's why the condition is never hit.

If you fix this error, it still won't work; you're deleting msg['fOO'] which doesn't make any sense! Try using enumerate() to get the index along with the strings in the list.

(And then it still won't work. Can you find out why?)

Upvotes: 2

Related Questions