Reputation: 2112
I have a list like:
defaultdict(<class 'list'>, {'Web': ['site: www.domain.com'], 'Phone': ['(111) 222-333', '(222) 333-444'], 'VAT': ['987654321'], 'Fax': ['(444) 555-666', '(777) 888-999'], 'E-mail': ['adress: [email protected]', 'address: [email protected]'], 'ID': ['number:1234567890']})
I want to clean words like: site:
, adress:
number:
.
Output should be:
defaultdict(<class 'list'>, {'Web': ['www.domain.com'], 'Phone': ['(111) 222-333', '(222) 333-444'], 'VAT': ['987654321'], 'Fax': ['(444) 555-666', '(777) 888-999'], 'E-mail': ['[email protected]', '[email protected]'], 'ID': ['1234567890']})
I know that I can remove words from specific list item like:
for em in d["E-mail"]:
print(em.replace("address: ","",1))
but I'm looking for something that would clean whole list.
Upvotes: 2
Views: 479
Reputation: 1441
Similar to Padraic Cunningham response but with regex:
In [39]: import re
In [40]: s = re.compile('[a-zA-Z]+:\s?')
In [41]: d={'Web': ['site: www.domain.com'], 'Phone': ['(111) 222-333', '(222) 333-444'], 'VAT': ['987654321'], 'Fax': ['(444) 555-666', '(777) 888-999'], 'E-mail': ['adress: [email protected]', 'address: [email protected]'], 'ID': ['number:1234567890']}
In [42]: def clean(dict_):
....: for k, v in dict_.items():
....: dict_[k] = map(lambda x: s.sub('', x), v)
....:
In [43]: clean(d)
Out[43]:
{'E-mail': ['[email protected]', '[email protected]'],
'Fax': ['(444) 555-666', '(777) 888-999'],
'ID': ['1234567890'],
'Phone': ['(111) 222-333', '(222) 333-444'],
'VAT': ['987654321'],
'Web': ['www.domain.com']}
Upvotes: 0
Reputation: 180391
You just want the substring after the :
, so either splitting will get us the substring or nothing will be removed if there is no :
in the string:
for k,v in d.items():
d[k] = [s.split(":", 1)[-1].lstrip() for s in v ]
print(d)
Output:
{'E-mail': ['[email protected]', '[email protected]'], 'Phone': ['(111) 222-333', '(222) 333-444'], 'ID': ['1234567890'], 'Web': ['www.domain.com'], 'VAT': ['987654321'], 'Fax': ['(444) 555-666', '(777) 888-999']}
Using [-1]
as the index will mean we either get the second of two or the only string if there is nothing split. We also need to lstrip
any leading whitespace from the substring after splitting.
You could also apply the same logic as you add the data to your defaultdict to avoid having to iterate over and mutate the dict values after they have already been assigned.
Upvotes: 4
Reputation: 132
for em in dict:
if ":" in dict[em]
a=dict[em].split(":")
a.remove(a[0])
print(a)
Try this code here
Upvotes: 0