Make Dictionary from 2D Array Python? - python

I have a 2D array as follows.
[['FE0456143', '218.04'], ['FB1357448', '217.52'], ['FB1482960', '222.70'], ['FB1483107', '223.32'], ['FE0456556', '12429.67'], ['FE0456594', '213.71'], ['FB1483056', '218.86'], ['FE0456061', '12392.33'], ['FB1482479', '223.35']]
The first element is the key while the second is the value. I have tried:
keys = zip(*data)[0]
vals = zip(*data)[1]
dic(zip(keys,vals))
However some elements of the array may have duplicate keys, and the elements are not corresponding to them? I want all of the keys to have 3 values associated with it?

Sounds like you want a 1 to many mapping. You can have this if you make your value a list:
from collections import defaultdict
d = defaultdict(list)
for k, v in data:
d[k].append(v)

Related

match the column value based on previous paired values in python

i have one dictionary with pair of keys and values and list
dict={0.000806:1.341382,0.023886:39.63012,7.525935:63.89669,7.571048:62.47208}
list=[7.525935,7.571048,0.000806,0.023886]
with this following list and dictionaries my expected output is
expected output:
{7.525937:63.89669,
7.571048:62.47208,
0.000806:1.341382,
0.023886:0.023886}
thing_category=dict((t,c) for c,t in category_thing.items())
list=[7.525935,7.571048,0.000806,0.023886]
for stuff in list_of_things:
if stuff in category_thing:
print(stuff)
with the help of list values match the dictionary values how can i do that i tried merge and map also but it didn't worked
with help of this 2 columns i have to match the values with anotherexpecting matched values with help of values in first figure
You can use a comprehension to construct a new dict with keys in the order of the list_ entries:
dict_ = {0.000806:1.341382,0.023886:39.63012,7.525935:63.89669,7.571048:62.47208}
list_ = [7.525935,7.571048,0.000806,0.023886]
{k: dict_[k] for k in list_}
However, you might run into problems with float keys if there are slight numeric differences.

Probems with merging same values for different keys in dictionary

I have a lot of Coordinates, for which i've created a large dictionary for which individual keys might have the same list of values. I want to merge those keys with the same list of values, as to get a count of how many keys have a given list of values. I've managed to merge them, but for some reason some of the values are reversed and therefore not properly merged.
So far I've tried creating a new dictionary with the merged keys as a tuple and kept the values as a list. Furthermore i only keep those list of values which has two or more keys.
My start dictionary
start_dict = {
'Key1': [243928620, 243938319],
'Key2': [243935130, 243935973],
'Key3': [243928620, 243938319],
'Key4': [243928628, 243938315],
'Key5': [243928628, 243938315],
'Key6': [243930418, 243933130, 243933141]
}
For which Key1 & 3 have the same list of values, same for Key4 & 5.
So i've merged them using
from collections import defaultdict
New_dict= defaultdict(list)
for k, v in sorted(start_dict.items()):
New_dict[tuple(v)].append(k)
final_dict = {tuple(v):set(k) for k, v in New_dict.items()}
With the expected results being
{(‘Key1’,’Key2’): {243928620, 243938319}, (‘Key4’,’Key5’): {243928628, 243938315}}
But for some reason it ends up as
{(‘Key1’,’Key2’): {243928620, 243938319}, (‘Key4’,’Key5’): {243938315,243928628}}
Where the values in a single list is switched, which is a problem since the order of the coordinates matter.
Of course the actual dataset is larger, and the merging works for 49/50 of the keys and list of value pairs.
Thanks for your time and suggestions.
The idea of using an intermediate dictionary was correct, but the keys of this dictionary should be the values of the original dictionary:
new_dict = defaultdict(list)
for k, v in sorted(start_dict.items()):
new_dict[tuple(v)].append(k)
final_dict = { tuple(v): list(k) for k, v in new_dict.items() if len(v) > 1 }
No need to use a set as we don't care about the uniqueness of values within the lists themselves.
Output:
{('Key1', 'Key3'): [243928620, 243938319], ('Key4', 'Key5'): [243928628, 243938315]}

Can't work out how to apply multi-keyed dictionary vals back to dataframe column

I have a dictionary with 2 keys for every 1 value like so:
Initial Dict
Key : ('106338', '2006-12-27') , Value : []
Dict after populating
Key : ('106338', '2006-12-27') , Value : [8, 7, 9, 8, 7]
The value for each key pair is an array holding some amount of information which I need the length of. I created this dictionary by first itertupling across a dataframe and generating key pairs and empty arrays for each unique record. I then iterated across it again and populated the arrays with the information I need by appending values to the end of each key pair. Key pairs were generated from row values. The first item in the key is the Identification number for the asset and the second item is the date for the asset. Here is code for dict creation:
perm_dict = {}
for row in df_perm.itertuples():
perm_dict[str(row[1]),str(row[3])] = []
for row in df_perm.itertuples():
if row[6].to_datetime().date() < row[9].to_datetime().date() and row[9].to_datetime().date() < row[5].to_datetime().date():
perm_dict[str(row[1]), str(row[3])].append(row[10])
My problem is that I now need to call those values back via the key pairs by iterating through the original dataframe so I can take my array lengths and make a new column out of them. Screenshot of DataFrame:
I am having trouble working out a solution in my head for applying these counts back to the original dataframe as a new column for only the rows with key matches. I can't iterate back through to add them because then I'd be modifying my original DF and I've read that's a big no-no. Any help that you all may be able to provide would be greatly appreciated! Also please lmk if I need to include more information as I can provide more.
Edit1
Here are the outputs after running the dictionary comprehension code provided.
This might be what you are looking for.
import pandas as pd
# sample data
d = {('106338', '2006-12-27'): [8, 7, 9, 8, 7]}
df = pd.DataFrame([['106338', '2006-12-27']], columns=['Key1', 'Key2'])
# first make dictionary mapping to length of list
d_len = {k: len(v) for k, v in d.items()}
# perform mapping
df['Len'] = list(map(d_len.get, (zip(*(df[col] for col in ('Key1', 'Key2'))))))
# output
# Key1 Key2 Len
# 106338 2006-12-27 5

sort one list of tuple and another list together

I have one object :
x= P(id1,type1,state1), y=P(id1,type2,state2)
and I create pair : (x,y)
and a dict :
dict[(type1,state1),(type2,state2)] = [id1,id2]
I want to sort keys by type and state and sort value in the same way.
To sort keys I do :
keys = sorted(tmpKeys, key=lambda element: (element.type,element.state))
for exemple, after sorted, if I obtain (y,x)(keys=[(type2,state2),(type1,state1)] in keys how can I have value = [id2,id1].
How can I give to my dict values that is sort like keys ?

Adding Multiple Values to Single Key in Python Dictionary Comprehension

I am using a dictionary comprension to be created from an excel spreadsheet. The first column of the excel sheet are the keys and the next 3 columns are the values. I'd like to build a dictionary comprehension that I can use later in my script. I understand a dictionary comprehension to be built as:
d = {key: value for (key, value) in sequence}
and I can do this and get a nice key,value dictionary:
d = {str(row.getValue("Column1")): str(row.getValue("Column2")) for i in arcpy.SearchCursor(xls,"[Column1] = 'Lake_Huron'")}
I'm just not sure how I would go about adding the other 2 columns in the dictionary comprehension as 2nd and 3rd values to the key? Is this possible?
d = {str(row.getValue("Column1")): (str(row.getValue("Column2")), str(row.getValue("Column3")), str(row.getValue("Column4"))) for i in arcpy.SearchCursor(xls,"[Column1] = 'Lake_Huron'")}
OR
d = {str(row.getValue("Column1")): (str(row.getValue("Column{0}".format(i)) for i in [2, 3, 4]) for i in arcpy.SearchCursor(xls,"[Column1] = 'Lake_Huron'")}

Categories