python - Inserting data in a Dictionary with raw_input dynamically? -


i writing code school project user enters data student along numbers 3 subject , if needed can update them later. have 2 questions.

1) how insert key value pair dictionary when inputs in 1 line of console?

ex:-

 >>>enter data?  >>>alex 45 26 35  

here key 'alex' , values 45 26 35

expected output

 {'alex': '45 62 35'} 

2) perform update single command line statement?

with syntax >>>'action' 'data'

ex:-

   >>>update alex 45 47 41  

the main problem facing here how split statement in action , data program can identify them individually , further data key value pair?

if want split string @ first space, can use string.split(s, maxsplit=n) s string split , maxsplit=n number of splits stop at. if not give value s call function string.split(maxsplit=n) split whitespaces.

example -

>>> s = "alex 45 26 35" >>> s.split(maxsplit=1) ['alex', '45 26 35'] 

you can use split strings , use first element key , second value in dictionary (if want).

you can use similar logic in actions case well, split input @ first space, , first element of return of split action, , second element data .

to create dictionary list, can use multiple methods, -

 d= dict([s.split(maxsplit=1)])  d  >>> {'alex': '45 26 35'} 

or

d = {} slst = s.split(maxsplit=1) d[slst[0]] = d[slst[1]] 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -