Convert split () to dict () after calling strip ()

I would like to create a dictionary of values ​​from a split () sequence without spaces.

If I had a list of lines that were formatted like this:

lines = ['Item1 = A         Item2 = B         Item3 = C',
         'Item4 = D     Item5 = E']

I know how to get pairs by spaces> 2:

s = [y for x in lines for y in x.split(' ') if y]

This returns another list of rows with pairs:

s = ['Item1 = A', 'Item2 = B', 'Item3 = C', 'Item4 = D', 'Item5 = E']

So far so good. Now from here you need to break the pairs into =, the left side - keyand the right side - value. I can do it:

t = [y.split('=') for x in lines for y in x.split(' ') if y]

This returns another list of strings with broken pairs:

t = ['Item1 ', ' A', 'Item2 ', ' B', 'Item3 ', ' C', 'Item4 ', 'D', 'Item5 ', ' E']

Now each element has either a trailing or leading space. This can be easily fixed by updating the line of the last list so that:

t = [z.strip() for x in lines for y in x.split(' ') for z in y.split('=') if y]

To make this dictionary, I know to call the generator expression:

d = dict(y.split('=') for x in lines for y in x.split(' ') if y)

key value. z.strip(), :

ValueError: dictionary update sequence element #0 has length 5; 2 is required

:

dict() strip() split('=') ? strip() dict()?

+4
4

:

s = ['Item1 = A', 'Item2 = B', 'Item3 = C', 'Item4 = D', 'Item5 = E']

#b = dict([x.split(' = ') for x in s])  # list comprehension: slightly faster.
b = dict(x.split(' = ') for x in s)     # generator expr.   : memory efficient.

print(b)  # {'Item3': 'C', 'Item1': 'A', 'Item4': 'D', 'Item5': 'E', 'Item2': 'B'}
+3

t range 2:

>>> t = [z.strip() for x in lines for y in x.split('  ') for z in y.split('=') if y]
>>> t
['Item1', 'A', 'Item2', 'B', 'Item3', 'C', 'Item4', 'D', 'Item5', 'E']

>>> dict((t[i], t[i + 1]) for i in range(0, len(t), 2))
{'Item2': 'B', 'Item3': 'C', 'Item1': 'A', 'Item4': 'D', 'Item5': 'E'}

, :

>>> d = dict(tuple(k.strip() for k in y.split('=')) for x in lines for y in x.split('  ') if y)

>>> d
{'Item2': 'B', 'Item3': 'C', 'Item1': 'A', 'Item4': 'D', 'Item5': 'E'}
0

, re itertools dict

>>> import itertools
>>> import re
>>> dict(itertools.chain.from_iterable(re.findall('(\w+\d+) = (\w+)', line) for line in lines))
{'Item1': 'A', 'Item2': 'B', 'Item4': 'D', 'Item3': 'C', 'Item5': 'E'}
0

:

lines = ['Item1 = A         Item2 = B         Item3 = C',
         'Item4 = D     Item5 = E']
gen = (piece for line in lines for piece in line.split() if piece != '=')
d = dict(zip(gen, gen))
print(d)
>>> {'Item4': 'D', 'Item2': 'B', 'Item1': 'A', 'Item3': 'C', 'Item5': 'E'}

, :

d = dict(zip(*[(p for l in lines for p in l.split() if p != '=')] * 2))

, , , , . zip, dict.

The disadvantage of this solution is that it should always have spaces around the equal characters.

0
source

Source: https://habr.com/ru/post/1683168/


All Articles