0

I have following code below.

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        splitName = name.split(' ')
        surname = splitName.pop()
        for i in range(len(splitName)):
            print('Name: %s' % splitName[i])

        return('Surname: %s' % surname)


np = NameParser()

print(np.getName("ali opcode goren"))

# output: name: ali, name: opcode, surname: goren

How do i return two values? Like following code:

for i in range(len(splitName)):
    return('Name: %s' % splitName[i])

return('Surname: %s' % surname)

# output: name ali: (error) i want all values name, name, surname

I want all values but just one output. How can I solve this problem?

Ali
  • 1,358
  • 3
  • 21
  • 32

5 Answers5

3
  1. Split: Split name by space and then do list comprehension again to remove the empty string from the list.
  2. POP: get last item from the list by pop() method and assign it to surname variable.
  3. Exception Handling: Do exception handling during pop process. If the input is empty then this will raise an IndexError exception.
  4. string concatenate: Iterate every Item from the list by for loop and assign the value to user_name variable.
  5. Concatenate surname in string again.
  6. Display result.

Demo:

class NameParser:
    def __init__(self):
        pass

    def getName(self, name):
        #- Spit name and again check for empty strings.
        splitName = [i.strip() for i in name.split(' ') if i.strip()]
        #- Get Surname. 
        try:
            surname = splitName.pop()
        except IndexError:
            print "Exception Name for processing in empty."
            return ""
        user_name = ""
        for i in splitName:
            user_name = "%s Name: %s,"%(user_name, i)
        user_name = user_name.strip()

        user_name = "%s Surname: %s"%(user_name, surname)
        return user_name


np = NameParser()
user_name = np.getName("ali      opcode       goren      abc")
print "user_name:", user_name

Output:

user_name: Name: ali, Name: opcode, Name: goren, Surname: abc
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
1

Try this:

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        listy = [] # where the needed output is put in
        splitName = name.split(' ')

        for i in range(len(splitName)):
            if i==(len(splitName)-1):#when the last word is reach
                listy.append('Surname: '+ splitName[i])
            else:
              listy.append('Name: '+ splitName[i])


        return listy


nr = NameParser()

print(nr.getName("ali opcode goren"))

# output: name: ali, name: opcode, surname: goren

whithout loop:

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        listy = [] # where the needed output is put in
        splitName = name.split(" ")
        listy ="Name",splitName[0],"Name",splitName[1],"Surname",splitName[2]



        return listy


nr = NameParser()

print(nr.getName("ali opcode goren"))

# output: name: ali, name: opcode, surname: goren
Martijn van Wezel
  • 1,120
  • 14
  • 25
1

Try to use yield

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        splitName = name.split(' ')
        surname = splitName.pop()
        for i in range(len(splitName)):
            yield ('Name: %s' % splitName[i])

        yield ('Surname: %s' % surname)


np = NameParser()

for i in (np.getName("ali opcode goren")):
    print i
ahmed
  • 5,430
  • 1
  • 20
  • 36
  • Thanks it's work! :) How to use this code without your for loop? – Ali Apr 23 '15 at 18:33
  • Read [this question and its answers](http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python). Ali, to learn about `yield`. – Celeo Apr 23 '15 at 18:36
1

you can just do this:

def getName(self, name):
    return name.split(' ')

It will return a tuple

def get_name(name):
   return name.split(' ')

>>> get_name("First Middle Last")
['First', 'Middle', 'Last']
joel goldstick
  • 4,393
  • 6
  • 30
  • 46
0

or you can try

class test():
    map = {}
    for i in range(10):
        map[f'{i}'] = i
    return map
samuel161
  • 221
  • 3
  • 2