I am newbie to Python.
I have 2 dictionaries which have the same keys but some keys have different values.
I would like to Iterate over the first dictionary and find the equal key In the second dictionary, check to see If the values equal and If not then print both values.
Can someone please help me with a template I can use?
Thanks
Iterate over one and compare the values.
dict2 = {"key1":"value1",
"key2":"value3"}
dict1 = {"key1":"value1",
"key2":"value2"}
for key in dict1:
if dict1[key] != dict2[key]:
print(key,dict1[key],dict2[key])
dict1 = {"key1": "value1", "key2": "value2", "key3": "value4"}
dict2 = {"key1": "value1", "key2": "value3"}
for key, value1 in dict1.items():
try:
value2 = dict2[key]
if value1 != value2:
print(key, value1, value2)
except KeyError:
print(key, value1, None)
Related
I want to check and filter only if the table has value1 = 005 and value1 = 009. But it seems below query is not helping me. I dont know where I am making mistakes. Kindly help to solve this. Note - I cannot use where not as it may have many different value stored in value1 field
DEFINE TEMP-TABLE test NO-UNDO
FIELD value1 AS CHARACTER
.
EMPTY TEMP-TABLE test.
CREATE test.
ASSIGN
value1 = "005".
CREATE test.
ASSIGN
value1 = "009".
CREATE test.
ASSIGN
value1 = "001".
FOR EACH test NO-LOCK
WHERE value1 <> ""
AND (value1 = "005" AND value1 = "009")
:
MESSAGE YES.
END.
You can use can-find
if can-find(first test WHERE value1 = "005")
AND can-find(first test WHERE value1 = "009")
then message yes.
It is safest to always use can-find(first if you're looking for a non-unique value
It looks like you're looking for an OR ooperation, rather than AND.
If you want to check if both records are present you could do :
DEFINE VARIABLE isPresent005 AS LOGICAL NO-UNDO.
DEFINE VARIABLE isPresent009 AS LOGICAL NO-UNDO.
DEFINE VARIABLE bothPresents AS LOGICAL NO-UNDO.
FIND FIRST test WHERE test.value1 = "005" NO-LOCK NO-ERROR.
isPresent005 = AVAIL test.
FIND FIRST test WHERE test.value1 = "009" NO-LOCK NO-ERROR.
isPresent009 = AVAIL test.
bothPresents = isPresent005 AND isPresent009.
But, if you only want to get these 2 records, you should use OR :
FOR EACH test WHERE test.value1 = "005" OR test.value1 = "009" NO-LOCK :
/*do stuff*/
END.
Another option if you are, maybe, looking for some additional fields might look something like this:
define buffer test005 for test.
define buffer test009 for test.
for each test005 no-lock where test005.customer = 1 and test005.value1 = "005",
each test009 no-lock where test009.customer = 1 and test009.value1 = "009":
display test005.customer.
end.
Use OR instead of AND to search the records...
This will return records if value1 = 005 OR value1 = 009.
FOR EACH test NO-LOCK
WHERE value1 <> ""
AND (value1 = "005" OR value1 = "009")
:
MESSAGE YES.
END.
Is not possible to search using your way, because value1 cannot be two values at once, it's always one OR another.
If I have a table like below, how do I create a dictionary of dynamic type from the 2 columns? E.g. {"a":"1", "b":"2", etc}
let test = datatable (
keys: string,
vals: string
) [
"a,b,c,d", "1,2,3,4"
];
There is the split() and zip() function but they create array of arrays and that doesn't work with todynamic()
Alternate variation
let test = datatable (keys: string, vals: string) ["a,b,c,d", "1,2,3,4"];
test
| mv-apply k = split(keys, ",") to typeof(string)
,v = split(vals, ",") to typeof(string)
on (summarize make_bag(bag_pack(k, v)))
keys
vals
bag_
a,b,c,d
1,2,3,4
{"a":"1","b":"2","c":"3","d":"4"}
Fiddle
You could try something like this, assuming both input arrays have the same length:
test
| extend keys = split(keys, ","),
vals = split(vals, ",")
| mv-apply with_itemindex = i k = keys to typeof(string) on (
summarize bag = make_bag(pack(k, vals[i]))
)
| project bag
I have performed union in my query which gives me the following output
{
key1 = value1,
key2 = value2
},
{
key3 = value3
}
I need output as follows,
{
key1 = value1,
key2 = value2,
key3 = value3
}
You have to deconstruct and reconstruct the maps. This operation is described in some detail in Gremlin Recipes if you want to read more.
The following code gets you to the position of your union():
gremlin> x = [[key1:"value1",key2:"value2"],[key3:"value3"]]
==>[key1:value1,key2:value2]
==>[key3:value3]
gremlin> g.inject(x).unfold()
==>[key1:value1,key2:value2]
==>[key3:value3]
Then you just unfold() those maps to map entries (i.e. key/value pairs) and group() them back together:
gremlin> g.inject(x).unfold().unfold().group().by(keys).by(values)
==>[key1:[value1],key2:[value2],key3:[value3]]
gremlin> g.inject(x).unfold().unfold().group().by(keys).by(select(values))
==>[key1:value1,key2:value2,key3:value3]
I'm attempting to reorder the values of a particular key in a python dictionary.
For instance, if a I have dictionary {'a':"hello","hey","hi"}
When i print the values of 'a', how could I change the order of "hello", "hey", or "hi"?
To rephrase this: the value of your key 'a' in your dictionary is a list containing 3 string-objects.
dict = { 'a': [ "hello", "hey", "hi" ] }
this being the case, you can sort the list like this
Example of my Code:
dictionary={ 'key1' : 'value1' , 'key2' : 'value2' , 'key1' : 'value3' }
x=dictionary['key1']
print(x)
This prints ONLY value3. I want it to print both value3 and value1, since they
both correlate to key1. How can I do this?
You cannot. The keys are added to the dictionary in the order you specified and the second occurence of 'key1' overwrites the previous one.
This is just as though you had written:
x = 1
x = 2
and then asked how you could print both values of x.
To record multiple values the easiest way is to use a defaultdict and build lists of values:
>>> import collections
>>> dictionary = collections.defaultdict(list)
>>> dictionary['key1'].append('value1')
>>> dictionary['key2'].append('value2')
>>> dictionary['key1'].append('value3')
>>> print(dictionary['key1'])
['value1', 'value3']