FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Python question





davidv
Code:
# takes user input
number = raw_input("Enter the number: ")

numberdict = {
            '1': 'one',
            '2': 'two',
            '3': 'three',
            '4': 'four',
            '5': 'five',
            '6': 'six',
            '7': 'seven',
            '8': 'eight',
            '9': 'nine'
            }

# creates a blank string
new_number = ''

x = 0

# as long as the counter is less than the length of the input
while x < (len(number)):
    for k in numberdict:
        # if the the first character matches a key in the dictionary
        if number[x] == k:

            # add the value from the key to the blank string
            new_number = new_number + numberdict[k] + ' '
           
    # goes to the next character in the string
    x += 1

print new_number[:-1]


It's just a short piece of code I wrote the other day that takes a string of ints and converts that into a string of characters. With this line,

Code:
# add the value from the key to the blank string
new_number = new_number + numberdict[k] + ' '


I want to know if there a better way to concat two strings and have a space delimiter.

Thanks.
Fire Boar
Why not just use a list comprehension?

Code:
# takes user input
number = raw_input("Enter the number: ")

numberdict = {
            '1': 'one',
            '2': 'two',
            '3': 'three',
            '4': 'four',
            '5': 'five',
            '6': 'six',
            '7': 'seven',
            '8': 'eight',
            '9': 'nine'
            }

# From "creates a blank string" onward... all that can be reduced to this one line.
print ' '.join([numberdict[x] for x in number if x in '123456789'])
davidv
Better yet...

Code:
number = raw_input("Enter the number: ")

numberdict = {
            '0': 'zero',
            '1': 'one',
            '2': 'two',
            '3': 'three',
            '4': 'four',
            '5': 'five',
            '6': 'six',
            '7': 'seven',
            '8': 'eight',
            '9': 'nine'
            }

for k in number:
    print numberdict[k],


Yeah, I also forgot the zero Sad
Fire Boar
That works too, though be careful - if anyone enters something that's not a number, it will throw an error. My version mimics the behaviour of the original, in which invalid (i.e. non-numeric) characters are ignored.

Another difference is that your version will end its output with a space. Mine works by joining together an array using the space as a delimiter, so it doesn't append a space to the last item.
Related topics
I Have a question . Is it the server support Chinese?
python
Does frihost still support Python for CGI programming
Python Support ???
php or python question about some processing.
Python, CGI and CSS
Variant of Python on servers?
Question about python
Another Python Question .replace()
A question about Python lambda
A question about dictionary of lists -- Python
Python
Python - Using files
Tutorial needed for python cgi
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.