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(' ')))
6
  • 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! Commented Oct 14, 2019 at 11:43
  • 2
    looks like the answer is here stackoverflow.com/questions/988228/… Commented Oct 14, 2019 at 11:43
  • What is the purpose of the exercise? Read a string, sanitize the input and output to a dict? Commented Oct 14, 2019 at 11:45
  • @BrunoVermeulen Yes. I want to organise data and use it further Commented Oct 14, 2019 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)} Commented Oct 14, 2019 at 12:04

1 Answer 1

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+).

Sign up to request clarification or add additional context in comments.

3 Comments

and where is bike : 50?
ah, yes it needs a space after 50 ;). Probably the regex can be made much more clever to deal with the spaces!
Thank you so much fr inputs, it is very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.