How to make a dictionary from raw data string

  • A+
Category:Languages

Essentially I need to create a dictionary from a string of data

Given :

data = "electron1, gamma5, proton43, boson98, ..." 

d(data) would result in:

{'electron':1, 'gamma':5, 'proton':43, 'boson':98, ...} 

My current code is showing an error message of "base 10..."

def d(n):         pair = dict()         for i in range(0,n):                 word = input().split()                 key = word[0]                 value = word[1]                 pair[key]=value         print(pair)  n = int(input())           d ={}                      for i in range(n):             text = input().split()     d[text[0]] = text[1] print(d) 

 


You can do that with a regular expression and a generator expression like:

Code:

END_NUMS_RE = re.compile(r'^(/D+)(/d+)$') dict(END_NUMS_RE.match(x.strip()).groups() for x in data.split(',')) 

Test Code:

import re END_NUMS_RE = re.compile(r'^(/D+)(/d+)$') data = "electron1, gamma5, proton43, boson98" print(dict(END_NUMS_RE.match(x.strip()).groups() for x in data.split(','))) 

Results:

{'electron': '1', 'gamma': '5', 'proton': '43', 'boson': '98'} 

But! A Regex:

So for those that are concerned about using a regex:

Let's benchmark a regex answer vs the second most voted answer here:

import re END_NUMS_RE = re.compile(r'^(/D+)(/d+)$') data = "electron1, gamma5, proton43, boson98"  def method1():     return dict(END_NUMS_RE.match(x.strip()).groups()                 for x in data.split(','))  def method2():     l = data.split(',')     return {         ''.join(             [x for x in item if not x.isdigit()]):             int(''.join([x for x in item if x.isdigit()])) for item in l     }   from timeit import timeit print(timeit(method1, number=10000)) print(timeit(method2, number=10000)) 

Timing Results:

0.05789754982012146 0.10536237238963242 

The regex answer is twice as fast.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: