Solved

shortening firstname.lastname to flastname

Posted on 2004-09-01
4
311 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

770 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