Solved

shortening firstname.lastname to flastname

Posted on 2004-09-01
4
324 Views
Last Modified: 2010-08-05
Hi all,

I have a list of names. one per line, that I need to shorten. Each name is in the form of firstname.lastname, and I need to have them in flastname form. So john.doe would be jdoe. I'm sure this is pretty basic but I can't figure it out :P


Also, I've never asked a question before. Since I don't know python and can't figure it out, does that mean it's difficult (250 points)? I'm sure it's a snap for someone, hehe. I'll assign something now and know better for next time :)

Thanks!

M
0
Comment
Question by:metrion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
rjkimble earned 250 total points
ID: 11958186
Suppose you have your list of names in a file, names.txt. You can read them into a list using something like this:

file( 'names.txt', 'r' ).readlines( )

Assuming that there are no exceptions to your rule in the file, you can create a list in "flastname" format using something like this:

flastnames = [ first[0] + last for first, last in [ name.split( '.' ) for name in file( 'names.txt', 'r' ).readlines( ) ] ]

You can also break that up a bit:

names = file( 'names.txt', 'r' ).readlines( )
flastnames = list( )
for name in names:
    first, last = name.split( '.' )
    flastnames.append( first[0] + last )

0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11961666
slightly compressed version of the first command would be:

flastnames = [ name[0] + name.split(')[1] for name in file( 'names.txt', 'r' ).readlines( ) ] ]

the broken up would become:

names = file( 'names.txt', 'r' ).readlines( )
flastnames = []
for name in names:
    flastnames.append( name[0] + name.split('.')[1] )


(basically, removed the explit naming of the results of the split) - definitely give rjkimble the points though, just posted this for interest sakes!
0
 

Author Comment

by:metrion
ID: 11965562
Wow, that's excellent! Worked like a charm, thanks! (yours too cjjclifford!)

One thing, it leaves an empty line between each result when I print it out like so:

#!/usr/bin/python

names = file( 'aubnames.txt', 'r' ).readlines( )
flastnames = []
for name in names:
        flastnames.append( name[0] + name.split('.')[1] )
        print name

Is that not the correct way to dump results to stdout?
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 11966930
readlines() leaves the line endings. You can try this instead:

#!/usr/bin/python

names = file( 'aubnames.txt', 'r' ).read( ).splitlines( )
flastnames = []
for name in names:
        flastnames.append( name[0] + name.split('.')[1] )
        print name

This approach reads the entire file into a string and then uses the splitlines() method to break it into a list of lines. The splitlines() method defaults to removing line endings.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trying to run a Python File 11 83
Python:Remove duplicates without sorting? 5 116
Python variable _ manually assigned 9 106
Live coding in Python 2.13.0 eclipse plugin security 1 73
Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question