""" Strings ==> Dicts """ #problem1 #Input: s = "{'muffin' : 'lolz', 'foo' : 'kitty'}" --> a string #output: {'muffin' : 'lolz', 'foo' : 'kitty'} --> dictionary >>> s "{'muffin' : 'lolz', 'foo' : 'kitty'}" >>> from ast import literal_eval >>> literal_eval(s) {'muffin': 'lolz', 'foo': 'kitty'} >>> s "{'muffin' : 'lolz', 'foo' : 'kitty'}" >>> import json >>> json_acceptable_string = s.replace("'", "\"") >>> d = json.loads(json_acceptable_string) >>> d {'muffin': 'lolz', 'foo': 'kitty'} """ NOTE that if you have single quotes as a part of your keys or values this will fail due to improper character replacement """ #problem:2 #Input: mystring="a=0 b=1 c=3" #output: {'a': 0, 'b': 1, 'c': 3} """ It's easy to convert list into a dict. """ In [1]: mystring = "a=0 b=1 c=3" In [2]: mylist1=mystring.split() #using split for a string generates a list In [3]: mylist1 Out[3]: ['a=0', 'b=1', 'c=3'] In [4]: mylist2=[] In [5]: for i in mylist1: #for every element in list1 i'm caling split at '=' ...: mylist2.append(i.split('=')) ...: In [6]: mylist2 Out[6]: [['a', '0'], ['b', '1'], ['c', '3']] In [7]: dict(mylist2) Out[7]: {'a': '0', 'b': '1', 'c': '3'} #it worked but values in dictonary are strings not ints In [8]: mylist2 Out[8]: [['a', '0'], ['b', '1'], ['c', '3']] #convert the value item into an int i.e '0'->0, '1'->1,'3'->3 ; mylist2 has 3 lists #So for every list in mylist2 i want to change the first element into a int #mylist2[0][1] is '0' #mylist2[1][1] is '1' In [9]: for lists in mylist2: ...: lists[1]=int(lists[1]) ...: In [10]: mylist2 Out[10]: [['a', 0], ['b', 1], ['c', 3]] #we can use a single line answer for this In [1]: mystring = "a=0 b=1 c=3" In [2]: dict( (n,int(v)) for n,v in (i.split('=') for i in mystring.split() ) ) Out[2]: {'a': 0, 'b': 1, 'c': 3} #using eval to solve #try to avoid using eval. #eval() interprets a string as code. >>> a='2*3' >>> eval(a) 6 mystring = "a=0 b=1 c=3" In [3]: mydict=eval('dict(%s)'%mystring.replace(' ',',')) In [4]: mydict Out[4]: {'a': 0, 'b': 1, 'c': 3} """ This one took me a while to understand. try this """ In [27]: dict(a=0,b=2,c=3) Out[27]: {'a': 0, 'b': 2, 'c': 3} #After trying this i came to understand In [25]: 'dict(%s)' % mystring.replace(' ',',') Out[25]: 'dict(a=0,b=1,c=3)' # Invoking eval on the above line gives us desired dictionary #problem 3 #Input:list_with_strings=["name","Ajay Kumar","age",25,"place","India"] #Output:{'age': 25, 'name': 'Ajay Kumar', 'place': 'India'} In [1]: list_with_strings=["name","Ajay Kumar","age",25,"place","India"] In [2]: dict(zip(*[iter(list_with_strings)]*2)) Out[2]: {'age': 25, 'name': 'Ajay Kumar', 'place': 'India'} """ func(*a) is the same as func(a[0], a[1], a[2], a[3] ... a[n]) if ahad n arguments * is an argument unpacking More @ http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean/287582#287582 """ In [37]: list_with_strings=["name","Ajay Kumar","age",25,"place","India"] In [38]: l = [iter(list_with_strings)]*2 In [39]: l Out[39]: [Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps., ] In [40]: dict(zip(l[0], l[1])) Out[40]: {'age': 25, 'name': 'Ajay Kumar', 'place': 'India'} In [41]: def foo(a,b,c,d): ....: print a,b,c,d ....: In [42]: l=[0,1] In [43]: d={"d":3,"c":2} In [44]: foo(*l,**d) #for arguments we use * and keyword arguments we use ** 0 1 2 3 #Easy way to understand this n [3]: my_iterable=iter(list_with_strings) #iter keyword makes it iterable In [4]: dict(zip(my_iterable,my_iterable)) Out[4]: {'age': 25, 'name': 'Ajay Kumar', 'place': 'India'}
No comments:
Post a Comment