how to push value to key object jquery - object

For Example:
{a: '1', b: '2', c: '3'}
now:
add 33 to b
{a: '1', b: '2,33', c: '3'}

Related

How to compare particular element in list python3?

l1= [['1', 'apple', '1', '2', '1', '0', '0', '0'], ['1',
'cherry', '1', '1', '1', '0', '0', '0']]
l2 = [['1', 'cherry', '2', '1'],
['1', 'plums', '2', '15'],
['1', 'orange', '2', '15'],
['1', 'cherry', '2', '1'],
['1', 'cherry', '2', '1']]
output = []
for i in l1:
for j in l2:
if i[1] != j[1]:
output.append(j)
break
print(output)
Expected Output:
[['1', 'plums', '2', '15'], ['1', 'orange', '2', '15']]
How to stop iteration and find unique elements and get the sublist?
How to stop iteration and find unique elements and get the sublist?
To find the elements in L2 that are not in L1 based on the fruit name:
l1= [[1,'apple',3],[1,'cherry',4]]
l2 = [[1,'apple',3],[1,'plums',4],[1,'orange',3],[1,'apple',4]]
output = []
for e in l2:
if not e[1] in [f[1] for f in l1]: # search by matching fruit
output.append(e)
print(output)
Output
[[1, 'plums', 4], [1, 'orange', 3]]
You can store all the unique elements from list1 in a new list, then check for list2 if that element exists in the new list. Something like:
newlist = []
for item in l1:
if item[1] not in newlist:
newlist.append(item)
output = []
for item in l2:
if item[1] not in newlist:
output.append(item)
print(output)
This is slightly inefficient but really straightforward to understand.

Is a dictionary within a dictionary with any list values

I have a question regarding dictionaries and how to tell if a dictionary appears in another. In the example below, I wish to check if d1 appears in d2 which as shown below:
d1 = { 'a': '1', 'b': '2', 'c': '3' }
d2 = { 'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5' }
res = all(item in d2.items() for item in d1.items())
However, I have a case where I wish to be able to pass in a list of values and check that the values for a, b and any of the values of c appear in d2.
d1 = { 'a': '1', 'b': '2', 'c': ['3', '33', '333'] }
d2 = { 'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5' }
Any help would be appreciated.
If I understand correctly, in your 2nd example, the result should be True since '3' belongs to list d1['c'] and d2['c'] == 3. If I am right then this might work:
d1 = { 'a': '1', 'b': '2', 'c': ['3', '33', '333'] }
d2 = { 'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5' }
res = all(item in d2.items() if not isinstance(item[1], list)
else any((item[0], x) in d2.items() for x in item[1])
for item in d1.items())
print(res)

Remove some same-index elements from two lists based on one of them

Let's suppose that I have these two lists:
a = [{'id': 3}, {'id': 7}, None, {'id': 1}, {'id': 6}, None]
b = ['5', '5', '3', '5', '3', '5']
I want to filter both at the same-index based though only on a and specifically on filtering out the None elements of a.
So finally I want to have this:
[{'id': 3}, {'id': 7}, {'id': 1}, {'id': 6}]
['5', '5', '5', '3']
I have written this code for this:
a_temp = []
b_temp = []
for index, el in enumerate(a):
if el:
a_temp.append(a[index])
b_temp.append(b[index])
a = a_temp[:]
b = b_temp[:]
I am wondering though if there is any more pythonic way to do this?
This solution
uses zip() to group corresponding elements of a and b together
Makes a list of 2-tuples of corresponding elements, such that the corresponding element of a is not None
Use the zip(*iterable) idiom to flip the dimensions of the list, thus separating the single list of 2-tuples into two lists of singletons, which we assign to new_a and new_b
a = [{'id': 3}, {'id': 7}, None, {'id': 1}, {'id': 6}, None]
b = ['5', '5', '3', '5', '3', '5']
new_a, new_b = zip(*((x, y) for x, y in zip(a, b) if x))
# new_a = ({'id': 3}, {'id': 7}, {'id': 1}, {'id': 6})
# new_b = ('5', '5', '5', '3')
If you just want a simple solution, please try:
a = [{'id': 3}, {'id': 7}, None, {'id': 1}, {'id': 6}, None]
b = ['5', '5', '3', '5', '3', '5']
n = []
for i in range(len(b)):
if a[i] is None:
n.append(i)
for i in sorted(n, reverse=True):
a.pop(i)
b.pop(i)
a
[{'id': 3}, {'id': 7}, {'id': 1}, {'id': 6}]
b
['5', '5', '5', '3']

Remove all keys excepts those mentioned from a dict python3

i have a nested python dictionary where i am trying to remove all keys except those mentioned. I found a solution here where we can remove keys specified, what i am trying to do is reverse of it, i want to remove all keys except those mentioned. So i made a slight change in the function, but i am getting an empty dict.
from collections import MutableMapping
def remove_fields(d, list_of_keys_to_remove):
if not isinstance(d, (dict, list)):
return d
if isinstance(d, list):
return [v for v in (remove_fields(v, list_of_keys_to_remove) for v in d) if v]
return {k: v for k, v in ((k, remove_fields(v, list_of_keys_to_remove)) for k, v in d.items()) if
k in list_of_keys_to_remove}
Adding sample input
EDIT: let suppose i want to keep RENEWAL_TYPE
{
'G_1': [
{
'ACCOUNTING_RULE_ID': '1',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '2',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '3',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'DO_NOT_RENEW'
},
{
'ACCOUNTING_RULE_ID': '4',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
}
]
}
You're looking for the intersection between the set of keys in the dict and the set of values in the list. i.e. all keys that exist in both sets.
Try this:
from collections import MutableMapping
d = { 'A': 1, 'B': 2, 'C': 3, 'D': 4 }
list_of_keys_to_keep = ['B', 'C', 'F']
def remove_fields(d, list_of_keys_to_keep):
return {key: value for key, value in d.items() if key in list_of_keys_to_keep}
print(remove_fields(d, list_of_keys_to_keep)) # -> {'B': 2, 'C': 3}
Edit: Updated name of list_of_keys_to_remove to list_of_keys_to_keep since this seems to be what it actually represents.
Edit2: Updated after Askers update. The following works for the sample provided:
from collections import MutableMapping
d = {
'G_1': [
{
'ACCOUNTING_RULE_ID': '1',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '2',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '3',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'DO_NOT_RENEW'
},
{
'ACCOUNTING_RULE_ID': '4',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
}
]
}
list_of_keys_to_keep = ['RENEWAL_TYPE']
def filter_dict(di, keys):
if not isinstance(di, (dict, list)):
return di
if isinstance(di, list):
return [filter_dict(value, keys) for value in di]
return {key: filter_dict(value, keys) for key, value in di.items() if key in keys or isinstance(value, (dict, list))}
print(filter_dict(d, list_of_keys_to_keep))
Output:
{'G_1': [{'RENEWAL_TYPE': 'RENEW'}, {'RENEWAL_TYPE': 'RENEW'}, {'RENEWAL_TYPE': 'DO_NOT_RENEW'}, {'RENEWAL_TYPE': 'RENEW'}]}
try this one:
dict = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
keys_to_preserve = ["e", "b", "c"]
dict_final={k: v for k,v in dict.items() if k in keys_to_preserve}
def remove_fields(d, keys_to_preserve):
if isinstance(d, list):
return [remove_fields(v, keys_to_preserve) for v in d]
elif isinstance(d, dict):
return dict([(k, remove_fields(v, keys_to_preserve)) for k, v in d.items() if k in keys_to_preserve])
else:
return d

Convert a list of strings to a list of strings and ints

I have a dictionary of lists, each list has 3 elements, i want to convert the 2nd and 3rd element to ints from strings
dict = {1:['string', '2', '3',],
2:['string', '2', '3',],
3:['string', '2', '3',],}
to become:
dict = {1:['string', 2, 3,],
2:['string', 2, 3,],
3:['string', 2, 3,],}
Thank you
Firstly, don't name your dictionaries dict, as it's a reserved keyword and you don't wanna overwrite it.
Coming to your solution.
d = {
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
d2 = {
k: [
int(i) if i.isdigit() else i
for i in v
]
for k, v in d.items()
}
Will give you an output of:
{
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3']
}
{
1: ['string', 23, 3],
2: ['string', 2, 3],
3: ['string', 2, 3]
}
If you dictionary has many elements you might not want to create a second dictionary in memory, but modify the existing one in-place:
data = {
1: ['string', '2', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
for v in data.values():
v.append(int(v.pop(1)))
v.append(int(v.pop(1)))
print(data)
Output:
{1: ['string', 2, 3], 2: ['string', 2, 3], 3: ['string', 2, 3]}

Resources