Issue
The code is working and adding to the list. However it is adding to each list three times instead of once. I want to append the items in the list once and not three times.
I have tried to check the range and it is only one. However, it still adds to list with the append method three times
newlist= [['id', 'name', 'lastContactedTime', 'email', 'phone_phones', 'home_phones', 'mobile_phones', 'work_phones', 'fax_phones', 'other_phones', 'address_1', 'address_2', 'address_3', 'city', 'state', 'postal_code', 'country', 'tags'], ['12-contacts', 'Courtney James', '', '[email protected]', '+1 3455463849', '', '', '', '', '', '654 Rodney Franklin street', '', '', 'Birmingham', 'AL', '45678', 'US', ''], ['4-contacts', 'Joe Malcoun', '2019-08-13 14:41:12', '[email protected]', '', '', '', '', '', '', '212 South Fifth Ave', '', '', 'Ann Arbor', 'MI', '48103', 'US', ''], ['8-contacts', 'Rafael Acosta', '', '[email protected]', '+1 338551534', '', '', '', '', '', '13 Jordan Avenue SW', '', '', 'Birmingham', 'AL', '45302', 'US', '']]
namelist = [] # new, empty list
for i in range(1, len(newlist)):
names = newlist[i][1].split() # this yields [first_name, last_name]
namelist.append([names[1], names[0]]) # [last_name, first_name]
companylist=[]
for i in range(1, len(newlist)):
p = re.compile(r'(.+)@(.+)\.(.+)')
test_str = newlist[i][3]
company= re.findall(p, test_str)
companyname= list(company[0][1])
companynom=''.join(companyname)
companylist.append(companynom) #yields company names
# strip non-numeric characters'
workphone = []
wrkstreetaddress = []
workcityaddress = []
wrkstate = []
wrkzip = []
for i in range(1, len(newlist)):
phone = re.sub(r'\D', '', newlist[i][4])
# remove leading 1 (area codes never start with 1)
phone = phone.lstrip('1')
workingphone= '{}.{}.{}'.format(phone[0:3], phone[3:6], phone[6:])
workphone.append(workingphone) #yields a list of workphone numbers
wrkstraddress= newlist[i][10]
wrkstreetaddress.append(wrkstraddress) #yields a list of work street addresses
wrkcityaddress= newlist[i][13] #yields a list of city addresses
workcityaddress.append(wrkcityaddress)
workstate= newlist[i][14] #yields a list of states
wrkstate.append(workstate)
workzip=newlist[i][15]
wrkzip.append(workzip) #yields a list of zip codes
I expect each list to contain one list with three items:
If I print workstreetaddress list I get:
print(wrskstreetaddress)
['654 Rodney Franklin street', '212 South Fifth Ave', '13 Jordan Avenue SW']
instead of:
['654 Rodney Franklin street']
['654 Rodney Franklin street', '212 South Fifth Ave']
['654 Rodney Franklin street', '212 South Fifth Ave', '13 Jordan Avenue SW']
The same for all the other lists from companylist to wrkzip I get the same results with adding the items three times instead of once
Solution
The result of this print statement at the end of your code:
print(workphone, wrkstreetaddress, workcityaddress, wrkstate, wrkzip)
Yields:
['345.546.3849', '..', '338.551.534'] ['654 Rodney Franklin street', '212 South Fifth Ave', '13 Jordan Avenue SW'] ['Birmingham', 'Ann Arbor', 'Birmingham'] ['AL', 'MI', 'AL'] ['45678', '48103', '45302']
I don't see a problem with your lists.
Answered By - pls256
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.