so i have 3 list of data,
person=['bobby','tim','sarah','tim','sarah','bobby,'tim','sarah','bobby,]
places=["loc1","loc1","loc2","loc1","loc2","loc2","loc1","loc2",'loc1"]
i have to use this data to show how time a person visited a certain place.
how would i be able to use the above list to get something like that
person loc1 loc2
bobby 2 1
tim 3 0
sarah 0 3
if the list the list was like bobby=['loc1','loc1','loc2']
i could use bobby.count(loc1). to find the data need but it is different here, also I have no idea how to make the table
i am not entire codes , i just need to know how i should start .
Use a temporary dict to store the values:
d = {}
for name, loc in zip(person, places):
d.setdefault(name, []).append(loc)
And to print it:
>>> for k, v in d.items():
print(k, end='\t')
print(v.count('loc1'), end='\t')
print(v.count('loc2'))
tim 3 0
bobby 2 1
sarah 0 3
Hope this helps!
If you have to have two lists to store persons and theirs places, you can have the following code to construct a dict that indicates which person has visited which places and the times.
persons = ['bobby', 'tim', 'sarah', 'tim', 'sarah', 'bobby', 'tim', 'sarah', 'bobby',]
places = ['loc1', 'loc1', 'loc2', 'loc1', 'loc2', 'loc2', 'loc1', 'loc2', 'loc1']
person_to_places = {}
total = len(persons)
for i in xrange(total):
if not person_to_places.has_key(persons[i]):
person_to_places[persons[i]] = []
person_to_places[person[i]].append(places[i])
for name, place in person_to_places.items():
person_to_places[name] = {}
for p in places:
if not person_to_places[name].has_key(p):
person_to_places[name][p] = 0
person_to_places[name][p] += 1
then, you have a dict *person_to_places* like this:
{'bobby': {'loc1': 2, 'loc2': 1}, 'sarah': {'loc2': 3}, 'tim': {'loc1': 3}}
After that, output the table as you like.
I would use dictionaries. Start by creating a dictionary of dictionaries for each name like so (see how to create it below):
data = { 'bobby' : {'loc1':0, 'loc2':0},
'sarah' : {'loc1':0, 'loc2':0},
'tim' : {'loc1':0, 'loc2':0} }
If you don't know your places and person list, you can use sets to make the data dictionary in the above form (there are ways, but here's my way):
from sets import Set
data = dict( (name, dict( (place, 0) for place in Set(places) ) ) for name in Set(person) )
And then simply loop through your lists, incrementing relevant places:
for i in range(len(person)):
data[ person[i] ][ places[i] ] += 1
When your data dictionary is ready, you can print it however you want.
Here's how the data dictionary looks like after running the above code on the lists you provided.
>>> for key in sorted(data): print key, data[key]
...
bobby {'loc2': 1, 'loc1': 2}
sarah {'loc2': 3, 'loc1': 0}
tim {'loc2': 0, 'loc1': 3}
Related
I'm trying to make a dictionary from items in a file. What I have now works but I was wondering if there is a way to only have a list if the key is a duplicate that has different value.so, if I have this
micheal math 2
jim chem 3
jim math 3
pam cs 4
expected output:
{micheal:[math,2],jim: [[chem,3], [math,3]], pam: [cs,4]}
actual output:
{micheal:[[math,2]],jim: [[chem,3], [math,3]], pam: [[cs,4]]}
current code:
example_dict = {}
for line in dictionary:
line = (line.strip()).split(' ')
key = line[0]
if key not in example_dict
example_dict[key] = []
example_dict[key].append(line[1:])
return example_dict
With your current solution, go over your example_dict afterward and flatten values that only have one element, e.x.:
...
example_dict = {k: (v if len(v) > 1 else v[0]) for k, v in example_dict.items()}
return example_dict
It seems like it would make a lot of sense to use dictionaries instead of tuple lists as values.
example_dict = {}
for line in dictionary:
name, subject, grade = line.strip().split() # optional, but cleaner
if name not in example_dict:
example_dict[name] = {}
example_dict[name][subject] = grade
Result:
{'micheal': {'math': '2'},
'jim': {'chem': '3', 'math': '3'},
'pam': {'cs': '4'}}
thing = [{item, abc}, {item, def}, {item, ghi}, {item2, jkl}, {item2, mno}, {item2, pqr}, ...]
to output: item abc def ghi ... item2 jkl mno ...
I've seen similar questions but none that would help me. I appreciate any help I can get!
A solution like this could work:
thing = [{"item", "abc"}, {"item", "def"}, {"item", "ghi"}, {"item2", "jkl"}, {"item2", "mno"}, {"item2", "pqr"}]
newDict = {}
count = 0
key = ""
for s in thing:
count = 0
for e in s:
if count == 0:
if not(e in newDict):
newDict[e] = []
key = e
else:
newDict[key].append(e)
count += 1
print(newDict)
Output:
{'item': ['abc', 'def', 'ghi'], 'item2': ['jkl', 'mno', 'pqr']}
Since you are using a list of sets, the solution is a bit more difficult than if you used a list of lists, since sets are unindexed.
However, we can still access the elements we order them in by iterating through them.
First, we create our newDict, a dictionary will store our output. For every set, s, in thing, we will iterate through every element, e, in s.
If we are currently iterating the first element, where count = 0, if e (which can be only either "item" or "item2"), is not a key in newDict, we will create a new key with an empty list as its value. Then, we will set our variable key equal to e, so that in the next iteration, we can append the value to that dictionary.
Solution if thing is a list of lists:
thing = [["item", "abc"], ["item", "def"], ["item", "ghi"],["item2", "jkl"], ["item2", "mno"], ["item2", "pqr"]]
newDict = {}
for s in thing:
if not(s[0] in newDict):
newDict[s[0]] = []
newDict[s[0]].append(s[1])
print(newDict)
I hope this made sense! Please let me know if you have any further questions or clarifications :)
I have a pandas data frame with 100 columns and I want to create a dictionary for each column with the first column as key and I've been doing it manually.
Let me explain with a sample dataframe
ID a b c
123 jon foo bar
789 pan bam fan
278 car bike boat
Let's consider the above table with column names ID, a, b, c. Now I am trying to create dictionaries for every column with ID being the key of the dictionary.
something like below
dicta = {ID: a}
dictb = {ID: b}
dictc = {ID: c}
What I am doing currently is:
dicta_ = dict(zip(df['ID'], df['a']))
dicta = {k:v for k,v in dicta_.items()}
dictb_ = dict(zip(df['ID'], df['b']))
dictb = {k:v for k,v in dictb_.items()}
dictc_ = dict(zip(df['ID'], df['c']))
dictc = {k:v for k,v in dictc_.items()}
and the above code is fetching me the desired result but I have to do it manually for all the 100 columns which is not the most efficient way to do it.
I would really appreciate if I could get some help or suggestion to automate the process by writing a loop or function. Thanks in advance!
set_index then use df.to_dict():
d = df.set_index('ID').to_dict()
then call the column with the value:
d['a']
# {123: 'jon', 278: 'car', 789: 'pan'}
d['b']
# {123: 'foo', 278: 'bike', 789: 'bam'}
Hello I have a dictionary that looks like this:
dictionary = {'John': {'car':12, 'house':10, 'boat':3},
'Mike': {'car':5, 'house':4, 'boat':6}}
I want to gain access and extract the keys within the sub-dictionary and assign them to variables like this:
cars_total = dictionary['car']
house_total = dictionary['house']
boat_total = dictionary['boat']
Now, when I run the variables above I get a 'Key Error'. It is understandable because I need to first access the outer dictionary. I would appreciate if someone gave a helping hand on how to access keys and the values within the sub-dictionary as those are the values I want to use.
Also i would like to do create a new key, this may not be right but something on these lines:
car = dictionary['car']
house = dictionary['house']
boat = dictionary['boat']
dictionary['total_assets'] = car + house + boat
I want to be able to access all those keys in the dictionary and create a new key. The outer keys such as "John' and 'Mike' should both contain the newly made key at the end.
I know this throws an error but it will give you an idea on what I want to achieve. Thanks for the help
I would just use a Counter object to get the totals:
>>> from collections import Counter
>>> totals = Counter()
>>> for v in dictionary.values():
... totals.update(v)
...
>>> totals
Counter({'car': 17, 'house': 14, 'boat': 9})
>>> totals['car']
17
>>> totals['house']
14
>>>
This has the added benefit of working nicely even if the keys aren't always present.
If you want the total assets, you can then simply sum the values:
>>> totals['total_assets'] = sum(totals.values())
>>> totals
Counter({'total_assets': 40, 'car': 17, 'house': 14, 'boat': 9})
>>>
To sum the total assets for each person and add it as a new key:
for person in dictionary:
dictionary[person]['total_assets'] = sum(dictionary[person].values())
which will result in:
dictionary = {'John': {'car':12, 'house':10, 'boat':3, 'total_assets':25},
'Mike': {'car':5, 'house':4, 'boat':6, 'total_assets':15}}
dictionary doens't have a key car, as you've seen. But dictionary['John'] does.
$ >>> dictionary['John']
{'car': 12, 'boat': 3, 'house': 10}
>>> dictionary['John']['car']
12
>>>
The value associated with each key in dictionary is, itself, another dictionary, which you index separately. There is no single object that contains, e.g., the car value for each subdictionary; you have to iterate
over each value.
# Iterate over the dictionary once per aggregate
cars_total = sum(d['car'] for d in dictionary.values())
house_total = sum(d['house'] for d in dictionary.values())
boat_total = sum(d['boat'] for d in dictionary.values())
or
# Iterate over the dictionary once total
cars_total = 0
house_total = 0
boat_total = 0
for d in dictionary.values():
cars_total += d['car']
house_total += d['house']
boat_total += d['boat']
dictionary = {'John': {'car':12, 'house':10, 'boat':3},'Mike': {'car':5, 'house':4, 'boat':6}}
total_cars=sum([dictionary[x]['car'] for x in dictionary ])
total_house=sum([dictionary[x]['house'] for x in dictionary ])
total_boats=sum([dictionary[x]['boat'] for x in dictionary ])
print(total_cars)
print(total_house)
print(total_boats)
Sample iteration method:
from collections import defaultdict
totals = defaultdict(int)
for person in dictionary:
for element in dictionary[person]:
totals[element] += dictionary[person][element]
print(totals)
Output:
defaultdict(<type 'int'>, {'car': 17, 'boat': 9, 'house': 14})
I have a list of names called names. I also have 2 dictionaries that contain a list of nested dictionaries that both have the key names and other data associated with the name. What I want to do is check that the name from the list is in one of the 2 dictionaries, if so, print the data associated only with that name. I can't find any of this stuff in the python docs
names = ['barry','john','george','sarah','lisa','james']
dict1 = {'results':[{'name':'barry','gpa':'2.9','major':'biology'},
{'name':'sarah','gpa':'3.2','major':'economics'},
{'name':'george','gpa':'2.5','major':'english'}]}
dict2 = {'staff':[{'name':'john','position':'Lecturer','department':'economics'},
{'name':'lisa','position':'researcher','department':'physics'},
{'name':'james','position':'tutor','department':'english'}]}
for x in names:
if x in dict1:
print gpa associated with the name
elif x in dict2:
print position associated with the name
The structure you're using for the two dicts isn't very optimal - each contains only a single element, which is a list of the relevant data for each person. If you can restructure those with a separate element per person using the name as a key, this becomes a trivial problem.
dict1 = {'barry': {'gpa':'2.9','major':'biology'},
'sarah': {'gpa':'3.2','major':'economics'},
'george': {'gpa':'2.5','major':'english'}}
dict2 = {'john': {'position':'Lecturer','department':'economics'},
'lisa': {'position':'researcher','department':'physics'},
'james': {'position':'tutor','department':'english'}}
Since it appears you can't get the data in this format, you'll have to convert it:
dict_results = dict((d['name'], d) for d in dict1[results])
if name in dict_results:
print dict_results[name]['gpa']
for _name in names:
if _name in [person['name'] for person in dict1['results']]: pass
elif _name in [person['name'] for person in dict2['staff']]:pass
something like that at least
This should get you an idea:
for name in names:
print name, ":"
print "\t", [x for x in dict2["staff"] if x["name"] == name]
print "\t", [x for x in dict1["results"] if x["name"] == name]
prints
barry :
[]
[{'major': 'biology', 'name': 'barry', 'gpa': '2.9'}]
john :
[{'department': 'economics', 'position': 'Lecturer', 'name': 'john'}]
[]
george :
[]
[{'major': 'english', 'name': 'george', 'gpa': '2.5'}]
sarah :
[]
[{'major': 'economics', 'name': 'sarah', 'gpa': '3.2'}]
lisa :
[{'department': 'physics', 'position': 'researcher', 'name': 'lisa'}]
[]
james :
[{'department': 'english', 'position': 'tutor', 'name': 'james'}]
[]
If you get this data from a database, you should probably rather work on the SQL frontier of the problem. A database is made for operations like that.
names = ['barry','john','george','sarah','lisa','james']
dict1 = {'results':[{'name':'barry','gpa':'2.9','major':'biology'},
{'name':'sarah','gpa':'3.2','major':'economics'},
{'name':'george','gpa':'2.5','major':'english'}]}
dict2 = {'staff':[{'name':'john','position':'Lecturer','department':'economics'},
{'name':'lisa','position':'researcher','department':'physics'},
{'name':'james','position':'tutor','department':'english'}]}
import itertools
for x in itertools.chain(dict1['results'], dict2['staff']):
for n in names:
if n in x.values():
print x
or the 2nd part could be:
ns = set(names)
for x in itertools.chain(dict1['results'], dict2['staff']):
if x['name'] in ns:
print x