0

I'm trying to get a list of lists in this format and also separate the name and phone no

[['Jackson, Janet', '313-1352'], ['James, Lebron', '457-6221'], ['Manfredi, Ralph', '872-2221'], ['Prakash, Varun', '312-5398']]

I'm currently getting in this format

['Jackson, Janet 313-1352', 'James, Lebron 457-6221', 'Manfredi, Ralph 872-2221', 'Prakash, Varun 312-5398']

I have been trying these logics

from myDatabasefile import *

b = []
contactlist = select()

print (contactlist)


for row in contactlist:
    b.append(row)
    print (row)
print(b)
James Z
  • 12,209
  • 10
  • 24
  • 44
Varun
  • 117
  • 1
  • 4
  • 14

5 Answers5

3

If all phone numbers contain only digits and dashes, you can use:

import re

rgx = re.compile(r'\s+(?=[\d-]+$)')

result = [rgx.split(x,1) for x in input]

Where input thus is your list of input strings. It generates the following list:

>>> [rgx.split(x,1) for x in input]
[['Jackson, Janet', '313-1352'], ['James, Lebron', '457-6221'], ['Manfredi, Ralph', '872-2221'], ['Prakash, Varun', '312-5398']]

But as said before, it only works properly given the above stated assumption. Best thing is, if the phone number contains spaces, then it can still work:

import re

rgx = re.compile(r'\s+(?=[\d\s-]+$)')

result = [rgx.split(x,1) for x in input]

For input with spaces in the phone number, it gives:

>>> input = ['Jackson, Janet 313-13 52', 'James, Lebron 457-6 22 1', 'Manfredi, Ralph 872-222 1', 'Prakash, Varun 312-5 3 9 8']
>>> [rgx.split(x,1) for x in input]
[['Jackson, Janet', '313-13 52'], ['James, Lebron', '457-6 22 1'], ['Manfredi, Ralph', '872-222 1'], ['Prakash, Varun', '312-5 3 9 8']]
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

Can you guarantee a phone number will always be 7 digits? if so,

contactList = []
for item in listOfContacts:
     contact = []
     phoneNumber = item[-8:] #get the phone number
     contactInfo = item.split() #split on the space
     contactInfo.pop() #pop the phone number
     contactName = " ".join(contactInfo)
     contact.append(contactName)
     contact.append(phoneNumber)
     contactList.append(contact)

There's probably more efficient ways to do it, but off the top of my head this is pretty succinct.

ynot269
  • 305
  • 2
  • 14
1

If phone numbers are always 8-digits, you can try:

contact_list = [[x[:-9], x[-8:]] for x in initial_list]
user257297
  • 46
  • 2
0

A one line solution without regex:

s = ['Jackson, Janet 313-1352', 'James, Lebron 457-6221', 'Manfredi, Ralph 872-2221', 'Prakash, Varun 312-5398']

new_s = [[' '.join(i.split()[:2])]+[i.split()[-1]] for i in s]

Output:

[['Jackson, Janet', '313-1352'], ['James, Lebron', '457-6221'], ['Manfredi, Ralph', '872-2221'], ['Prakash, Varun', '312-5398']]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0
import re
l = ['Jackson, Janet 313-1352', 'James, Lebron 457-6221', 'Manfredi, Ralph 872-2221', 'Prakash, Varun 312-5398']
[ re.search(r'(.*) (\d+\-\d+)', s).groups() for s in l ]

Output:

[('Jackson, Janet', '313-1352'),
 ('James, Lebron', '457-6221'),
 ('Manfredi, Ralph', '872-2221'),
 ('Prakash, Varun', '312-5398')]
Transhuman
  • 3,527
  • 1
  • 9
  • 15