0

I want to create a dictionary from the string like this below

" Car : 100 Bus: 200 Travel cost = 500 cycle : 10 bike : 50 "

and the output should look like below

{ 'car' : 100 , 'bus' : 200, 'Travel cost' : 500 ,  'cycle' : 10 , 'bike' : 50}

It has ":" and "=" in the string, so I am not understanding how to do, please help me. I tried:

Dict = dict((x.strip(), y.strip()) for x, y in (element.split(':') for element in string.split(' ')))
kutschkem
  • 7,826
  • 3
  • 21
  • 56
Tina
  • 13
  • 2
  • 2
    So what would you do is there wasn't `=` ? We're not a code writing service, please show what you tried and we can help from there! – h4z3 Oct 14 '19 at 11:43
  • 2
    looks like the answer is here https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary – k2a Oct 14 '19 at 11:43
  • What is the purpose of the exercise? Read a string, sanitize the input and output to a dict? – Bruno Vermeulen Oct 14 '19 at 11:45
  • @BrunoVermeulen Yes. I want to organise data and use it further – Tina Oct 14 '19 at 11:48
  • You might first try to split the text with regex: `d = {m[0].strip() : int(m[1]) for m in re.findall("([^:=]+)[:=]\s+(\d+)", s)}` – 001 Oct 14 '19 at 12:04
  • first, tokenize your string `re.findall("[A-Z]{2,}(?![a-z])|[A-Z][a-z]+(?=[A-Z])|[\'\w\-]+",input)`, then, zip the list result into a dict(with `for` and `if` statement) – Hadi Farhadi Oct 14 '19 at 12:15

1 Answers1

1

It is quite hard to parse the string but let us assume the pattern in the string is:

'\D+ : \d+ \D+ : \d+'

where \D is a character and \d is a digit and divided by :, you may proceed as follows.

  • Sanitize the string and replace = with :
  • find all patterns for '\D+ : \d '
  • loop over the found pattern and convert to a dict

for example

import re
input_str = "Car : 100 Bus: 200 Travel cost = 500 cycle : 10 bike : 50 "
input_str = input_str.replace('=', ':')

dict_elements = re.findall(r'\D+ : \d+ ', input_str)

my_dict = {}
for element in dict_elements:
    key, val = element.split(':')
    key = key.strip()
    val = int(val.strip())
    my_dict[key] = val

print(my_dict)

actually a better solution dealing with possible spaces before and after the : is the following

import re
input_str = "Car:100 Bus: 200 Travel cost = 500 cycle: 10       bike      :50    "

dict_elements = re.findall(r'\s*(\D+)\s*[:=]\s*(\d+)\s*', input_str)

my_dict = {}
for element in dict_elements:
    my_dict[element[0].strip()] = int(element[1].strip())

print(my_dict)

Note I dealt with : or = in the search string of findall with [:=], while key is (\D+) and val is (\d+).

Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29