I have a nested dictionary with list of values, I want to have
- the maximum index wise value between two lists
- the 'id' for each max value (by id I mean from which list is the value coming and what index it is).
I already have the index wise max value between the two lists, what I need with is the 'id'.
#create dictionary:
test = {}
test['A'] = {}
test['A']['number'] = [2,2,3]
test['A']['id'] = ['x','y','z']
test['B'] = {}
test['B']['number'] = [1,3,2]
test['B']['id'] = ['a','b','c']
#this the maximum index-wise value between the two lists
max_list = [max(*l) for l in zip(test['A']['number'], test['B']['number'])]
print(max_list)
What I would like is another list with the following:
['x','b','z']
make an inner zip of id and number so we know which id belongs to which number,
then use max with a custom key function (by number), then split them:
test = {}
test['A'] = {}
test['A']['number'] = [2,2,3]
test['A']['id'] = ['x','y','z']
test['B'] = {}
test['B']['number'] = [1,3,2]
test['B']['id'] = ['a','b','c']
tuple_list = [max(*l, key=lambda t: t[1]) for l in zip(zip(test['A']['id'],test['A']['number']), zip(test['B']['id'],test['B']['number']))]
max_num_list = [t[1] for t in tuple_list]
max_id_list = [t[0] for t in tuple_list]
print(max_num_list)
print(max_id_list)
Output:
[2, 3, 3]
['x', 'b', 'z']
Related
i have 2d list implementation as follows. It shows no. of times every student topped in exams:-
list = main_record
['student1',1]
['student2',1]
['student2',2]
['student1',5]
['student3',3]
i have another list of unique students as follows:-
list = students_enrolled
['student1','student2','student3']
which i want to display student ranking based on their distinctions as follows:-
list = student_ranking
['student1','student3','student2']
What built in functions can be useful. I could not pose proper query on net. In other words i need python equivalent of following queries:-
select max(main_record[1]) where name = student1 >>> result = 5
select max(main_record[1]) where name = student2 >>> result = 2
select max(main_record[1]) where name = student3 >>> result = 3
You define a dict base key of studentX and save the max value for each student key then sort the students_enrolled base max value of each key.
from collections import defaultdict
main_record = [['student1',1], ['student2',1], ['student2',2], ['student1',5], ['student3',3]]
students_enrolled = ['student1','student2','student3']
# defind dict with negative infinity and update with max in each iteration
tmp_dct = defaultdict(lambda: float('-inf'))
for lst in main_record:
k, v = lst
tmp_dct[k] = max(tmp_dct[k], v)
print(tmp_dct)
students_enrolled.sort(key = lambda x: tmp_dct[x], reverse=True)
print(students_enrolled)
Output:
# tmp_dct =>
defaultdict(<function <lambda> at 0x7fd81044b1f0>,
{'student1': 5, 'student2': 2, 'student3': 3})
# students_enrolled after sorting
['student1', 'student3', 'student2']
If it is a 2D list it should look like this: l = [["student1", 2], ["student2", 3], ["student3", 4]]. To get the highest numeric value from the 2nd column you can use a loop like this:
numbers = []
for student in list:
numbers.append(student[1])
for num in numbers:
n = numbers.copy()
n.sort()
n.reverse()
student_index = numbers.index(n[0])
print(list[student_index], n[0])
numbers.remove(n[0])
I wanna achieve this without any libraries or special functions just loops. I wanna have a main program that takes in 2 inputs which are the 2 lists and returns the dictionary like shown below.
Please enter the item names: Cans, bottles, boxes, jugs
please enter quantities : 20,34,10
output : {'Cans':'20','bottles':'34','boxes':'10','jugs':'0'}
If the list of items is longer than the quantities then the quantity becomes automatically 0 as it did with the jugs above.
If the List of Quantity is longer than the items list then the item should automatically become 'unknown object_1' with the number changing accordingly.
Split with comma as delimiter. Fill values with zero for a number of iterations equal to the difference in length between keys and values.
Then use dict comprehension to build your dict. This with the zip built-in function.
keys = 'a,b,c,d'
values = '1,2,3'
keys = keys.split(',')
values = values.split(',')
for i in range(len(keys) - len(values)):
values.append('0')
dct = {}
for i in range(len(keys)):
dct[keys[i]] = values[i]
print(dct)
Output:
{'a': '1', 'b': '2', 'c': '3', 'd': '0'}
This uses only built-in calls so it fits your requirements at best. At the OP requirements it is not using the zip function.
item_names = ['Cans', 'Bottles', 'boxes', 'jugs']
quantities = [20, 34, 10]
output_dict = {}
for i, item in enumerate(item_names):
if i > len(quantities) - 1:
output_dict.update({item : 0})
else:
output_dict.update({item : quantities[i]})
a = list(input().split(','))
b = list(map(int, input().split(',')))
res = {}
for i in range(len(a)):
res[a[i]] = b[i] if i < len(b) else 0
print(res)
list1 = ['cans','Bottles','Boxes','Jugs']
list2 = [1,2,3]
res = {}
for i, element in enumerate(list1):
try:
res[element] = list2[i]
except IndexError:
res[element] = 0
print(res)
Edited code without enumerate or zip:
list1 = ['cans','Bottles','Boxes','Jugs']
list2 = [1,2,3]
res = {}
i=0
for element in list1:
try:
res[element] = list2[i]
except IndexError:
res[element] = 0
i+=1
print(res)
```
Imagine I have this list:
list = ['a','a','b','a']
I would like to do something like this:
print(unique(list))
to retrieve the unique item(s), so python will output b. How could I do this?
Count the items, keep only those which have a count of 1:
>>> data = ['a','a','b','a']
>>> from collections import Counter
>>> [k for k,v in Counter(data).items() if v == 1]
['b']
Using set() property of Python, we can easily check for the unique values. Insert the values of the list in a set. Set only stores a value once even if it is inserted more then once. After inserting all the values in the set by list_set=set(list1), convert this set to a list to print it.
for more ways check : https://www.geeksforgeeks.org/python-get-unique-values-list/#:~:text=Using%20set()%20property%20of,a%20list%20to%20print%20it.
Example to make a unique function :
# Python program to check if two
# to get unique values from list
# using set
# function to get unique values
def unique(list1):
# insert the list to the set
list_set = set(list1)
# convert the set to the list
unique_list = (list(list_set))
for x in unique_list:
print x,
# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)
list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)
output should be:
the unique values from 1st list is
40 10 20 30
the unique values from 2nd list is
1 2 3 4 5
You can also use numpy.unique example:
`
#Ppython program to check if two
# to get unique values from list
# using numpy.unique
import numpy as np
# function to get unique values
def unique(list1):
x = np.array(list1)
print(np.unique(x))
# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)
list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)
`
output should be :
the unique values from 1st list is
[10 20 30 40]
the unique values from 2nd list is
[1 2 3 4 5]
Fore more please check "https://www.geeksforgeeks.org/python-get-unique-values-list/#:~:text=Using%20set()%20property%20of,a%20list%20to%20print%20it."
You could use collections.Counter() for this, e.g.:
from collections import Counter
my_list = ['a','a','b','a']
counts = Counter(my_list)
for element, count in counts.items():
if count == 1:
print(element)
would print b and nothing else in this case.
Or, if you'd like to store the result in a list:
from collections import Counter
my_list = ['a','a','b','a']
counts = Counter(my_list)
unique_elements = [element for element, count in counts.items() if count == 1]
unique_elements is ['b'] in this case.
Count the appearance of an element in the list, and store it in a dictionary
From the dictionary, check which elements has only one appearance
Store the unique (one appearance) elements in a list
Print the list
You can try this:
my_list = ['a','a','b','a']
my_dict = {}
only_one = []
#Step 1
for element in my_list:
if element not in my_dict:
my_dict[element] = 1
else:
my_dict[element] += 1
#Step 2 and Step 3
for key, value in my_dict.items():
if value == 1:
only_one.append(key)
#Step 4
for unique in only_one:
print(unique)
Output:
b
In case you are wondering what other variables contain:
my_dict = {'a': 3, 'b': 1}
only_one = ['b']
Most straight forward way, use a dict of counters.
a = ['a','a','b','a']
counts = {}
for element in a:
counts[element] = counts.get(element, 0) + 1
for element in a:
if counts[element] == 1:
print(element)
out:
b
I created a list, a set and a dict and now I want to remove certain items from them
N = [10**i for i in range(0,3)] #range(3,7) for 1000 to 1M
for i in N:
con_list = []
con_set = set()
con_dict = {}
for x in range (i): #this is the list
con_list.append(x)
print(con_list)
for x in range(i): #this is the set
con_set.add(x)
print(con_set)
for x in range(i): #this is the dict
con_dict = dict(zip(range(x), range(x)))
print(con_dict)
items to remove
n = min(10000, int(0.1 * len(con_list)))
indeces_to_delete = sorted(random.sample(range(i),n), reverse=True)
now if I add this:
for a in indeces_to_delete:
del con_list[a]
print(con_list)
it doesn't work
Need to do the same for a set and a dict
Thanks!
You can use pop
On a dict:
d = {'a': 'test', 'b': 'test2'}
calling d.pop('b') will remove the key/value pair for key b
on list:
l = ['a', 'b', 'c']
calling l.pop(2) will remove the third element (as list index start at 0)
Beware on set:
s = {'a', 'b', 'c'}
calling s.pop() will remove a random element as discussed here: In python, is set.pop() deterministic?
you should use s.discard('a') to remove element 'a'
More infos here: https://docs.python.org/2/tutorial/datastructures.html
I'm trying to get the matching IDs and store the data into one list. I have a list of dictionaries:
list = [
{'id':'123','name':'Jason','location': 'McHale'},
{'id':'432','name':'Tom','location': 'Sydney'},
{'id':'123','name':'Jason','location':'Tompson Hall'}
]
Expected output would be something like
# {'id':'123','name':'Jason','location': ['McHale', 'Tompson Hall']},
# {'id':'432','name':'Tom','location': 'Sydney'},
How can I get matching data based on dict ID value? I've tried:
for item in mylist:
list2 = []
row = any(list['id'] == list.id for id in list)
list2.append(row)
This doesn't work (it throws: TypeError: tuple indices must be integers or slices, not str). How can I get all items with the same ID and store into one dict?
First, you're iterating through the list of dictionaries in your for loop, but never referencing the dictionaries, which you're storing in item. I think when you wrote list[id] you mean item[id].
Second, any() returns a boolean (true or false), which isn't what you want. Instead, maybe try row = [dic for dic in list if dic['id'] == item['id']]
Third, if you define list2 within your for loop, it will go away every iteration. Move list2 = [] before the for loop.
That should give you a good start. Remember that row is just a list of all dictionaries that have the same id.
I would use kdopen's approach along with a merging method after converting the dictionary entries I expect to become lists into lists. Of course if you want to avoid redundancy then make them sets.
mylist = [
{'id':'123','name':['Jason'],'location': ['McHale']},
{'id':'432','name':['Tom'],'location': ['Sydney']},
{'id':'123','name':['Jason'],'location':['Tompson Hall']}
]
def merge(mylist,ID):
matches = [d for d in mylist if d['id']== ID]
shell = {'id':ID,'name':[],'location':[]}
for m in matches:
shell['name']+=m['name']
shell['location']+=m['location']
mylist.remove(m)
mylist.append(shell)
return mylist
updated_list = merge(mylist,'123')
Given this input
mylist = [
{'id':'123','name':'Jason','location': 'McHale'},
{'id':'432','name':'Tom','location': 'Sydney'},
{'id':'123','name':'Jason','location':'Tompson Hall'}
]
You can just extract it with a comprehension
matched = [d for d in mylist if d['id'] == '123']
Then you want to merge the locations. Assuming matched is not empty
final = matched[0]
final['location'] = [d['location'] for d in matched]
Here it is in the interpreter
In [1]: mylist = [
...: {'id':'123','name':'Jason','location': 'McHale'},
...: {'id':'432','name':'Tom','location': 'Sydney'},
...: {'id':'123','name':'Jason','location':'Tompson Hall'}
...: ]
In [2]: matched = [d for d in mylist if d['id'] == '123']
In [3]: final=matched[0]
In [4]: final['location'] = [d['location'] for d in matched]
In [5]: final
Out[5]: {'id': '123', 'location': ['McHale', 'Tompson Hall'], 'name': 'Jason'}
Obviously, you'd want to replace '123' with a variable holding the desired id value.
Wrapping it all up in a function:
def merge_all(df):
ids = {d['id'] for d in df}
result = []
for id in ids:
matches = [d for d in df if d['id'] == id]
combined = matches[0]
combined['location'] = [d['location'] for d in matches]
result.append(combined)
return result
Also, please don't use list as a variable name. It shadows the builtin list class.