Reputation: 322
I have a list of strings
list = ['2(a)', '2(b)', '3', '3(a)', '1d', '5']
where it is intentional that the 1d, 3, and 5 don't involve parentheses.
I would like to create a dictionary which looks like this:
dict = {'2': 'a', '2': 'b', '3': 'a', '1': 'd'}
or
dict = {'2': ['a', 'b'], '3': ['a'], '1': ['d']}.
Essentially, ignore those strings without a letter a-z. I've used regular expressions to extract from the top list the following:
['a', 'b', 'a', 'd'],
but this hasn't helped me much in forming my dictionary easily.
Any help is much appreciated.
Upvotes: 1
Views: 1761
Reputation: 3525
This is a good time to use setdefault()
for a dictionary to define the structure of your dictionary. The first part involves capturing the numbers from the elements using a regex that captures all numbers. That list
is then concatenated using join()
.
We then extract only alphabet characters using either a list comprehension -> [j for j in i if j.isalpha()]
, or pass as a generator j for j in i if j.isalpha()
(generator in our case, joining the elements as a string
together once again).
Lastly a check to see that both key
and value
exist so that we can set our dictionary to be of this format -> { '' : [] , ...}
import re
def to_dict(l):
d = {}
for i in l:
key = re.findall(r'\d+', i)
value = ''.join(j for j in i if j.isalpha())
if key and value:
d.setdefault(''.join(key), []).append(value)
return d
Sample output:
l = ['2(a)', '2(b)', '3', '3(a)', '1c', '5']
print to_dict(l)
>>> {'1': ['c'], '3': ['a'], '2': ['a', 'b']}
Upvotes: 2
Reputation: 49330
Since a dictionary can't contain duplicate keys, use a defaultdict
:
import collections
l = ['2(a)', '2(b)', '3', '3(a)', '1c', '5']
d = collections.defaultdict(list)
for item in l:
num = ''.join(c for c in item if c.isdigit())
word = ''.join(c for c in item if c.isalpha())
if word and num:
d[num].append(word)
Result:
>>> print(d)
defaultdict(<class 'list'>, {'2': ['a', 'b'], '1': ['c'], '3': ['a']})
Upvotes: 4