Update Values in Nested Dictionary According to Nested Key - nested

I have the following Structures:
MyDict = {'Team1': {'John' : 0, 'Mary' : 0},
'Team2': {'Steven' : 0, 'Gwen' : 0,'Fred' : 0},
'Team3': {'Louise' : 0, 'Alice' : 0, 'Jim' : 0},
'Team4': {'Paul' : 0, 'George' : 0},
'Team5': {'Tracey' : 0}}
MyList = ['John', 'Gwen', 'Jim', 'George', 'John', 'Gwen', 'Alice', 'Frank', 'John', 'Gwen', 'Frank', 'John', 'Gwen', 'Tracey']
The first is a nested structure with members in specific teams.
For each occurrence of the member in MyList, I need that value updated in MyDict.
Once complete, I need the printed total for each team.
So after iterating through MyList, the nested structure will look like this:
MyDict = {'Team1': {'John' : 4, 'Mary' : 0},
'Team2': {'Steven' : 0, 'Gwen' : 4,'Fred' : 0},
'Team3': {'Louise' : 0, 'Alice' : 1, 'Jim' : 1},
'Team4': {'Paul' : 0, 'George' : 1},
'Team5': {'Tracey' : 1}}
And the printed totals will look like this:
Team1: 4
Team2: 4
Team3: 2
Team4: 1
Team5: 1

use code below:
MyDict = {'Team1': {'John' : 0, 'Mary' : 0},
'Team2': {'Steven' : 0, 'Gwen' : 0,'Fred' : 0},
'Team3': {'Louise' : 0, 'Alice' : 0, 'Jim' : 0},
'Team4': {'Paul' : 0, 'George' : 0},
'Team5': {'Tracey' : 0}}
MyList = ['John', 'Gwen', 'Jim', 'George', 'John', 'Gwen', 'Alice', 'Frank', 'John', 'Gwen', 'Frank', 'John', 'Gwen', 'Tracey']
for name in MyList:
for key,val in MyDict.items():
if name in val:
val[name]+=1
for key,val in MyDict.items():
print(key,':',sum(val.values()))
have fun :)

Related

Find number of co-occurring elements between dataframe columns

I have a DataFrame that has a website, categories, and keywords for that website.
Url | categories | keywords
Espn | [sport, nba, nfl] | [half, touchdown, referee, player, goal]
Tmz | [entertainment, sport] | [gossip, celebrity, player]
Goal [ [sport, premier_league, champions_league] | [football, goal, stadium, player, referee]
Which can be created using this code:
data = [{ 'Url': 'ESPN', 'categories': ['sport', 'nba', 'nfl'] ,
'keywords': ["half", "touchdown", "referee", "player", "goal"] },
{ 'Url': 'TMZ', 'categories': ["entertainment", "sport"] ,
'keywords': ["gossip", "celebrity", "player"] },
{ 'Url': 'Goal', 'categories': ["sport", "premier_league", "champions_league"] ,
'keywords': ["football", "goal", "stadium", "player", "referee"]},
]
df =pd.DataFrame(data)
For all the word in the keywords column, I want to get the frequency of categories associated with it. The results might look like this:
{half: {sport: 1, nba: 1, nfl: 1}, touchdown : {sport: 1, nba: 1,
nfl: 1}, referee: {sport: 2, nba: 1, nfl: 1, premier_league: 1,
champions_league:1 }, player: {sport: 3, nba: 1, nfl: 1,
premier_league: 1, champions_league:1 }, gossip: {sport:1,
entertainment:1}, celebrity: {sport:1, entertainment:1}, goal:
{sport:2, premier_league:1, champions_league:1, nba: 1, nfl: 1},
stadium:{sport:1, premier_league:1, champions_league:1} }
Since the columns contain lists, you can explode them to repeat a row once for each element per list:
result = (
df.explode("keywords")
.explode("categories")
.groupby(["keywords", "categories"])
.size()
)

How can I replace value with a series of increasing number in VsVim?

I want to replace this part with a series of increasing number in VsVim.
Like so:
std::map<std::string, int> monthMap = {
{"Jan", 0}, {"Janurary", 0}, {"Feb", 0}, {"February", 0}, {"Mar", 0}, {"March", 0}
};
I want to substitute all 0 for 1, 2, 3...
std::map<std::string, int> monthMap = {
{"Jan", 1}, {"Janurary", 1}, {"Feb", 2}, {"February", 2}, {"Mar", 3}, {"March", 3}
};
How can I do this?
Thanks!

group dictionaries and get count

I have a list of dictionaries like this:
list1 = [{'name': 'maik','is_payed': 1, 'brand': 'HP', 'count': 1, 'items': [{'device': 'mouse', 'count': 110}]},{'name': 'milanie','is_payed': 0, 'brand': 'dell', 'count':10, 'items': [{'device': 'bales', 'count': 200}]}]
list2 = [{'name': 'maik','is_payed': 0, 'brand': 'HP', 'count': 20, 'items': [{'device': 'mouse', 'count': 1}]},{'name': 'nikola','is_payed': 1, 'brand': 'toshiba', 'count':10, 'items': [{'device': 'hard', 'count': 20}]}]
my_list= list1 + list2
count = pd.DataFrame(my_list).groupby(['name', 'is_payed'])
final_list_ = []
for commande, group in count:
print(commande)
records = group.to_dict("records")
final_list_.append({"name": commande[0],
"payed": commande[1],
"occurrence": len(group),
"items": pd.DataFrame(records).groupby('device').agg(
occurrence=('device', 'count')).reset_index().to_dict('records')})
I don't know how can I get it like this:
the 'payed' field is like this payed/total_commands
for example lets take maik he has two commands one is payed and the other one is not, so the final result will be like this:
{'name': 'maik','payed': 1/2, 'brand': 'HP', 'count': 21, 'items': [{'device': 'mouse', 'count': 111}]}
Since you just want to group by "name" and are only interested in the "played" values, let's concentrate on that and ignore the other data.
So for our purposes, your starting data looks like:
my_list = [
{'name': 'maik', 'is_payed': 1},
{'name': 'milanie', 'is_payed': 0},
{'name': 'maik', 'is_payed': 0},
{'name': 'nikola', 'is_payed': 1}
]
Now let's take a first pass over this data and count up the number of times we see a name and the number of times that name corresponds to an "is_payed" flag
results = {}
for item in my_list:
key = item["name"]
results.setdefault(key, {"sum": 0, "count": 0})
results[key]["count"] += 1
results[key]["is_payed"] += item["is_payed"]
At this point we have a dictionary that will look like:
{
'maik': {'is_payed': 1, 'count': 2},
'milanie': {'is_payed': 0, 'count': 1},
'nikola': {'is_payed': 1, 'count': 1}
}
Now we will take a pass over this dictionary and create our true final result:
results = [
{"name": key, "payed": f"{value['is_payed']}/{value['count']}"}
for key, value in results.items()
]
Giving us:
[
{'name': 'maik', 'payed': '1/2'},
{'name': 'milanie', 'payed': '0/1'},
{'name': 'nikola', 'payed': '1/1'}
]

How to get json result with parent - child relation?(see details)

I have two json one is parent and another is child, I want to merge both. Please help. See the example below
Two tables are as below.
var parent = [{id : 1, name : 'India'},
{id : 2, name : 'USA'},
{id : 3, name : 'Japan'},
{id : 4, name : 'UK'}]
var child = [{id: 1, parentId: 1, city : 'Ahmedabad', population:100},
{id: 2, parentId: 1, city : 'Mumbai', population:200},
{id: 3, parentId: 2, city : 'NewYork', population:300},
{id: 4, parentId: 2, city : 'Chicago', population:400},
{id: 5, parentId: 3, city : 'Tokyo', population:500}]
I want to result like below
var result =
[{id : 1, name : 'India', city:[{name : 'Ahmedabad', population:100},{name : 'Mumbai', population:200}]},
{id : 2, name : 'USA', city:[{name : 'NewYork', population:300},{name : 'Chicago', population:400}]},
{id : 3, name : 'Japan', city:[{name : 'Tokyo', population:500}]},
{id : 4, name : 'UK', city:[]}
]
var result =JSON.parse(JSON.stringify(parent));
result.map(function (d, i) {
d.city = (child.filter(function (d1) {
if (d1.parentId == d.id) {
return d1;
}
})).map(function(d2){
return {city:d2.city,population:d2.population}
});
})
Now result variable contains your expected result.

what should be the mongo query for this

Below if a document from my collection of over 20,000,000 documents.
I need to find documents by a particular zip, out of these documents I need to select one record from each postal address (ADDR, CITY, STATE, ZIP, APT) and which has a age value of 18 or higher.
The results need to be limited to a number as well which is entered by the end-user.
{
"_id" : ObjectId("55e86e98f493590878bb45d7"),
"RecordID" : 84096380,
"FN" : "Michael",
"MI" : "",
"LN" : "Horn",
"NAME_PRE" : "MR",
"ADDR" : "160 Yankee Camp Rd",
"CITY" : "Telford",
"ST" : "TN",
"ZIP" : 37690,
"APT" : "",
"Z4" : 2200,
"DPC" : 605,
"CAR_RTE" : "R001",
"WALK_SEQ" : 228,
"LOT" : "0136A",
"FIPS_ST" : 47,
"FIPS_CTY" : 179,
"LATITUDE" : 36.292787,
"LONGITUDE" : -82.568171,
"ADDR_TYP" : 1,
"MSA" : 3660,
"CBSA" : 27740,
"ADDR_LINE" : 3,
"DMA_SUPPR" : "",
"GEO_MATCH" : 1,
"CENS_TRACT" : 61900,
"CENS_BLK_GRP" : 1,
"CENS_BLK" : 17,
"CENS_MED_HOME_VALUE" : 953,
"CENS_MED_HH_INCOME" : 304,
"CRA" : "",
"Z4_TYP" : "S",
"DSF_IND" : 1,
"DPD_IND" : "N",
"PHONE_FLAG" : "Y",
"PHONE" : NumberLong("4237730233"),
"TIME_ZN" : "E",
"GENDER" : "M",
"NEW_TO_BLD" : "",
"SOURCES" : 19,
"BASE_VER_DT" : 20101,
"COMP_ID" : NumberLong("3769001836"),
"IND_ID" : 1,
"INF_HH_RANK" : 1,
"HOME_OWNR_SRC" : "V",
"DOB_YR" : 1975,
"DOB_MON" : 7,
"DOB_DAY" : 10,
"EXACT_AGE" : 39,
"AGE" : 39,
"HH_INCOME" : "D"
}
if you are using mongoose, we can chain the operations by dot(.) operator. Since i see all your needs is conditional here is the example -
Person.
find({
ZIP: "37690",
ADDR : "",
STATE : "", //so on
AGE: { $gt: 18 }
}).
limit(10).
exec(callback);
more info - http://mongoosejs.com/docs/queries.html
You need to use aggregate operation.
var pipeline = [
{
$match: {ZIP: 37690, AGE: {$gt: 18}}
}, {
$group: {
_id: {ADDR: '$ADDR', CITY: '$CITY', STATE: '$STATE', ZIP: '$ZIP', APT: '$APT'},
PHONE: {$first: '$PHONE'}
}
},
{$limit: 10}
];
db.mycoll.aggregate(pipeline)
enhance the above to project whatever fields you require in results
I think This query will solve your problem.
Person.find({
ZIP: "37690",
AGE: { $gt: 18 }
}).
limit(50).
exec(callback);

Resources