I am trying to edit this function so the values of the dictionary will not be printed in parentheses and will be iterable:
def traverse_appended(key):
reg_dict = {}
#keypath = r"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
for item in traverse_reg(key):
keypath_str = str(keypath+item)
reg_dict[item] = str(get_reg("Displayversion", keypath_str)), str(get_reg("DisplayName", keypath_str))
#reg_dict[item] = get_reg("DisplayName", keypath_str)
return reg_dict
the expected output is :
{'DXM_Runtime': 'None', 'None'}
The function output:
{'DXM_Runtime': ('None', 'None')}
#Consider traverse_appended returns following dict.
#I think, converting func_dict values which are tuple into string, will help you to get expected output.
func_dict = {"DXM_Runtime":('None','None'),
"TMP_KEY":('A','B')
}
derived_dict = {}
for k,v in func_dict.viewitems():
tmp_str = ",".join(v)
derived_dict[k] = tmp_str
print derived_dict
#Output
E:\tmp_python>python tmp.py
{'DXM_Runtime': 'None,None', 'TMP_KEY': 'A,B'}
#If this doesn't help you, then please post the code for get_reg and traverse_reg function also.
Related
I am trying to update the dictionary "menu" values by getting a value from an appropriate dictionary. I try the following code and get the desired result. But I expect some other alternate ways and a more generic solution in a pythonic way.
Code
language = {"english" : {"lbl01":"File" ,"lbl02":"Accounts"},
"tamil" : {"lbl01":"கோப்பு" ,"lbl02":"கணக்கியல்"},
"hindi" : {"lbl01":"Hindi_File","lbl02":"Hindi_accounts"}}
scut = {"user1": {"lbl01":"Alt+F","lbl02":"F5"},
"user2": {"lbl01":"Ctrl+F","lbl02":"Shift+F5"}}
menu = {"lbl01" :{"id":"file" ,"lan1":"","lan2":"","scut":""},
"lbl02" :{"id":"accounts","lan1":"","lan2":"","scut":""}}
user = ["user2"]
selected_lan = ["tamil","hindi"]
menukey_lst,submenukey_lst =[],[]
for menukey,menuvalue in menu.items():
if menukey not in menukey_lst: menukey_lst.append(menukey)
for submenukey,submenuvalue in menuvalue.items():
if submenukey not in submenukey_lst: submenukey_lst.append(submenukey)
for index,mk in enumerate(menu.keys()):
for item in submenukey_lst:
menu[menukey_lst[index]]["lan1"] = language[selected_lan[0]][menukey_lst[index]]
menu[menukey_lst[index]]["lan2"] = language[selected_lan[1]][menukey_lst[index]]
menu[menukey_lst[index]]["scut"] = (scut[user[0]][mk])
print(menu)
Output
{'lbl01': {'id': 'file', 'lan1': 'கோப்பு', 'lan2': 'Hindi_File', 'scut': 'Ctrl+F'}, `'lbl02': {'id': 'accounts', 'lan1': 'கணக்கியல்', 'lan2': 'Hindi_accounts', 'scut': 'Shift+F5'}}`
This should work.
for key, value in menu.items():
for sub_key in value.keys():
if sub_key == 'lan1':
value[sub_key] = language[selected_lan[0]][key]
elif sub_key == 'lan2':
value[sub_key] = language[selected_lan[1]][key]
elif sub_key == 'scut':
value[sub_key] = scut[user[0]][key]
I would like to make a loop to create a dataframe that gathers the lines of an input dataframe, which have common points.
My problem : When I apply the function, the output dataframe is empty...
yet with a print (output) in the loop, we see that the program works .. I do not understand, i tried to change return position but that doesn't work
Thank you in advance for your help !
def group (dataframe, identifiant, output):
for i in range(len(identifiant)):
ident = identifiant.loc[i,"IDCTV"]
# print(ident)
for j in range(len(dataframe)):
if dataframe.loc[j,"IDCONTREVENANT"] == ident:
di = dataframe.loc[j, "DATE_INFRACTION"]
nt = dataframe.loc[j,"NOTRAIN"]
genre = dataframe.loc[j,"CODEETATCIVIL"]
age = dataframe.loc[j,"AGE"]
# print(di, nt, genre, age)
for k in range(len(dataframe)):
if k != j :
if dataframe.loc[k,"DATE_INFRACTION"] == di and dataframe.loc[k,"NOTRAIN"] == nt:
idgroup = dataframe.loc[k,"IDCONTREVENANT"]
genreidgroup = dataframe.loc[k,"CODEETATCIVIL"]
ageidgroup = dataframe.loc[k,"AGE"]
output = output.append({ "IDREF" : ident ,"CODEETATCIVILREF" : genre,"AGEREF" : age ,"IDCTV" : idgroup,"CODEETATCIVILCTV" : genreidgroup,"AGECTV" : ageidgroup}, ignore_index = True)
print(output)
return output
group(df,IDCTV,df_groups)
print(df_groups)
I think you want to change
group(df,IDCTV,df_groups)
to
df_groups = group(df,IDCTV,df_groups)
Right now you're calling the group funciton and doing all that calculation, but you're not saving the output anywhere. So when you run print(df_groups) it prints out whatever it was before you called the function.
I asked a question for statistical functions and got an answer but I am looking at another way to do it:
What I find weird is:
This works:
myData = dataSplit.map(lambda arr: (arr[1]))
myData2 = myData.map(lambda line: line.split(',')).map(lambda fields: ("Column", float(fields[0]))).groupByKey()
stats[1] = myData2.map(lambda (Column, values): (min(values))).collect()
But when I add this function:
stats[4] = myData2.map(lambda (Column, values): (values)).variance()
It fails.
So I put some print:
myData = dataSplit.map(lambda arr: (arr[1]))
print myData.collect()
myData2 = myData.map(lambda line: line.split(',')).map(lambda fields: ("Column", float(fields[0]))).groupByKey()
print myData2.map(lambda (Column, values): (values)).collect()
Printing myData:
[u'18964', u'18951', u'18950', u'18949', u'18960', u'18958', u'18956', u'19056', u'18948', u'18969', u'18961', u'18959', u'18957', u'18968', u'18966', u'18967', u'18971', u'18972', u'18353', u'18114', u'18349', u'18348', u'18347', u'18346', u'19053', u'19052', u'18305', u'18306', u'18318', u’18317']
Printing myData2:
[<pyspark.resultiterable.ResultIterable object at 0x7f3f7d3e0710>]
solved
print myData.map(lambda line: line.split(',')).map(lambda fields: ("Column", float(fields[0]))).map(lambda (column, value) : (value)).stdev()
I am trying to create a nested dictionary from a mysql query but I am getting a key error
result = {}
for i, q in enumerate(query):
result['data'][i]['firstName'] = q.first_name
result['data'][i]['lastName'] = q.last_name
result['data'][i]['email'] = q.email
error
KeyError: 'data'
desired result
result = {
'data': {
0: {'firstName': ''...}
1: {'firstName': ''...}
2: {'firstName': ''...}
}
}
You wanted to create a nested dictionary
result = {} will create an assignment for a flat dictionary, whose items can have any values like "string", "int", "list" or "dict"
For this flat assignment
python knows what to do for result["first"]
If you want "first" also to be another dictionary you need to tell Python by an assingment
result['first'] = {}.
otherwise, Python raises "KeyError"
I think you are looking for this :)
>>> from collections import defaultdict
>>> mydict = lambda: defaultdict(mydict)
>>> result = mydict()
>>> result['Python']['rules']['the world'] = "Yes I Agree"
>>> result['Python']['rules']['the world']
'Yes I Agree'
result = {}
result['data'] = {}
for i, q in enumerate(query):
result['data']['i'] = {}
result['data'][i]['firstName'] = q.first_name
result['data'][i]['lastName'] = q.last_name
result['data'][i]['email'] = q.email
Alternatively, you can use you own class which adds the extra dicts automatically
class AutoDict(dict):
def __missing__(self, k):
self[k] = AutoDict()
return self[k]
result = AutoDict()
for i, q in enumerate(query):
result['data'][i]['firstName'] = q.first_name
result['data'][i]['lastName'] = q.last_name
result['data'][i]['email'] = q.email
result['data'] does exist. So you cannot add data to it.
Try this out at the start:
result = {'data': []};
You have to create the key data first:
result = {}
result['data'] = {}
for i, q in enumerate(query):
result['data'][i] = {}
result['data'][i]['firstName'] = q.first_name
result['data'][i]['lastName'] = q.last_name
result['data'][i]['email'] = q.email
How can I do the following in Python:
I have a command output that outputs this:
Datexxxx
Clientxxx
Timexxx
Datexxxx
Client2xxx
Timexxx
Datexxxx
Client3xxx
Timexxx
And I want to work this in a dict like:
Client:(date,time), Client2:(date,time) ...
After reading the data into a string subject, you could do this:
import re
d = {}
for match in re.finditer(
"""(?mx)
^Date(.*)\r?\n
Client\d*(.*)\r?\n
Time(.*)""",
subject):
d[match.group(2)] = (match.group(1), match.group(2))
How about something like:
rows = {}
thisrow = []
for line in output.split('\n'):
if line[:4].lower() == 'date':
thisrow.append(line)
elif line[:6].lower() == 'client':
thisrow.append(line)
elif line[:4].lower() == 'time':
thisrow.append(line)
elif line.strip() == '':
rows[thisrow[1]] = (thisrow[0], thisrow[2])
thisrow = []
print rows
Assumes a trailing newline, no spaces before lines, etc.
What about using a dict with tuples?
Create a dictionary and add the entries:
dict = {}
dict['Client'] = ('date1','time1')
dict['Client2'] = ('date2','time2')
Accessing the entires:
dict['Client']
>>> ('date1','time1')