Solved

shortening firstname.lastname to flastname

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python  -  List Iteration output problem 2 58
Python - How does this 2D array not work? 3 63
looping 11 27
regex to extract ip:john 17 57
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now