Python 3, create new dictionary using list and another dictionary - python-3.x

Could you help me wiht my issue ? Let's say that I have few list with ID's their members, like below:
team_A = [1,2,3,4,5]
team_B = [6,7,8,9,10]
team_C = [11,12,13,14,15]
and now I have a dictionary with their values:
dictionary = {5:23, 10:68, 15:68, 4:1, 9:37, 14:21, 3:987, 8:3, 13:14, 2:98, 7:74, 12:47, 1:37, 6:82, 11:99}
I would like to take correct elements from dictionary and create new dictionary for team A, B and C, like below:
team_A_values = {5:23, 4:1, 3:987, 2:98, 1:37}
Could you give advice how to do that ? Thanks for your help

You can do something like below by just Iterating through the lists
team_A = [1,2,3,4,5]
team_B = [6,7,8,9,10]
team_C = [11,12,13,14,15]
dictionary = {5:23, 10:68, 15:68, 4:1, 9:37, 14:21, 3:987, 8:3, 13:14, 2:98, 7:74, 12:47, 1:37, 6:82, 11:99}
team_A_values = {}
for i in team_A:
team_A_values[i] = dictionary[i]
print(team_A_values )
can repeat this to team B and team C
in that case you can do like this
team_values = [{i: dictionary[i] for i in team_A },{i: dictionary[i] for i in team_B},{i: dictionary[i] for i in team_C}]
teamA,teamB,teamC = team_values
print(team_values)
print(teamA)
print(teamB)
print(teamC)
in one line you can do like this
team_values = [{i: dictionary[i] for i in team } for team in [team_A ,team_B ,team_C]]
teamA,teamB,teamC = team_values
print(team_values)
print(teamA)
print(teamB)
print(teamC)

Related

How to get a unique pattern from a list

I have a list like this:
[ '0D',
'0A,0C',
'0C,0A',
'0C,0E,0D,0F',
'0C,0D,0E,0F',
'0B,0G',
'0B,0F'
]
In this list '0A,0C' and '0C,0A'.Also '0C,0E,0D,0F' &
'0C,0D,0E,0F' are similar. How to get the unique items from a list like this. I tried set but I guess the functionality of set is a bit different.
´set´ is good, if you use ´split´ first:
l = ['0D', '0A,0C', '0C,0A', '0C,0E,0D,0F', '0C,0D,0E,0F', '0B,0G', '0B,0F']
for i in range(len(l)):
l[i] = ','.join(sorted(l[i].split(',')))
l = set(l)
# {'0A,0C', '0B,0F', '0B,0G', '0C,0D,0E,0F', '0D'}

Concat 1 to n items into new spark column

I try to have a dynamic concat of fields, based on some configuration settings the goal is to have a new fields with merged values of 1 to n fields.
language = "JP;EN"
language = list(str(item) for item in language.split(";"))
no_langs = len(language)
# check if columns for multi-language exists
for lang in language:
doc_lang = "doctor.name_" + lang
if doc_lang not in case_df.columns:
case_df_final = AddColumn(case_df, doc_lang)
### combine translations of masterdata
case_df = case_df.withColumn(
"doctor",
F.concat(
F.col(("doctor.name_" + language[0])),
F.lit(" // "),
F.col(("doctor.name_" + language[1])),
),
)
What I would like to achieve is that the new column is dynamic depending of the amount of languages configured. E.g. If only one language is used the result would be like this.
case_df = case_df.withColumn(
"doctor",
F.col(("doctor.name_" + lang[0]))
)
For 2 languages or more it should pick all the languages based on the order in the list.
Thanks for your help.
I am using Spark 2.4. with Python 3
The expected output would be the following
Final working code is the following:
# check if columns for multi-language exists
for lang in language:
doc_lang = "doctor.name_" + lang
if doc_lang not in case_df.columns:
case_df = AddColumn(case_df, doc_lang)
doc_lang_new = doc_lang.replace(".", "_")
case_df = case_df.withColumnRenamed(doc_lang, doc_lang_new)
doc_fields = list(map(lambda k: "doctor_name_" + k, language))
case_df = case_df.withColumn("doctor", F.concat_ws(" // ", *doc_fields))
Thanks all for the help and hints.

how to create list of dictionary in this code?

I have some names and scores as follows
input = {
'Maths': dict(Mohsen=19, Sadegh=18, Hafez=15),
'Physics': dict(Sadegh=16, Hafez=17, Mohsen=17),
'Chemistry': dict(Hafez=13),
'Literature': dict(Sadegh=14),
'Biology': dict(Mohsen=16, Sadegh=10),
}
if a person don't have any lesson its score consider zero also get avrege of scores's person and sort final list by averge and i want to get an output like this.
answer = [
dict(Name='Sadegh', Literature=14, Chemistry=0, Maths=18, Physics=16, Biology=10, Average=11.6),
dict(Name='Mohsen', Maths=19, Physics=17, Chemistry=0, Biology=16, Literature=0, Average=10.4),
dict(Name='Hafez', Chemistry=13, Biology=0, Physics=17, Literature=0, Maths=15, Average=9),
]
how to do it?
Essentially, you have a dictionary, where the information is arranged based on subjects, where for each subject, you have student marks. You want to collection all information related to each student in separate dictionaries.
One of the approaches which can try, is as below:
Try converting the data which you have into student specific data and then you can calculate the Average of the Marks of all subjects for that student. There is a sample code below.
Please do note that, this is just a sample and you should be trying
out a solution by yourself. There are many alternate ways of doing it and you should explore them by yourself.
The below code works with Python 2.7
from __future__ import division
def convert_subject_data_to_student_data(subject_dict):
student_dict = {}
for k, v in subject_dict.items():
for k1, v1 in v.items():
if k1 not in student_dict:
student_dict[k1] = {k:v1}
else:
student_dict[k1][k] = v1
student_list = []
for k,v in student_dict.items():
st_dict = {}
st_dict['Name'] = k
st_dict['Average'] = sum(v.itervalues()) / len(v.keys())
st_dict.update(v)
student_list.append(st_dict)
print student_list
if __name__ == "__main__":
subject_dict = {
'Maths': dict(Mohsen=19, Sadegh=18, Hafez=15),
'Physics': dict(Sadegh=16, Hafez=17, Mohsen=17),
'Chemistry': dict(Hafez=13),
'Literature': dict(Sadegh=14),
'Biology': dict(Mohsen=16, Sadegh=10),
}
convert_subject_data_to_student_data(subject_dict)
sample_input = {
'Maths': dict(Mohsen=19, Sadegh=18, Hafez=15),
'Physics': dict(Sadegh=16, Hafez=17, Mohsen=17),
'Chemistry': dict(Hafez=13),
'Literature': dict(Sadegh=14),
'Biology': dict(Mohsen=16, Sadegh=10),
}
def foo(lessons):
result = {}
for lesson in lessons:
for user in lessons[lesson]:#dictionary
if result.get(user):
#print(result.get(user))
result.get(user).setdefault(lesson, lessons[lesson].get(user,0))
else:
result.setdefault(user, dict(name=user))
result.get(user).setdefault(lesson,lessons[lesson].get(user,0))
#return list(result.values())
return result.values()
#if name == '__main__':
print(foo(sample_input))

python substring (slicing) compare not always working

`list1 = ["Arizona","Atlanta","Baltimore","Buffalo","Carolina","Chicago",
"Cincinnati","Cleveland","Dallas","Denver","Detroit","Green Bay","Houston",
"Indianapolis","Jacksonville","Kansas City","L.A. Chargers","L.A. Rams",
"Miami","Minnesota","New England","New Orleans","NY Giants","NY Jets",
"Oakland","Philadelphia","Pittsburgh","San Francisco","Seattle",
"Tampa Bay","Tennessee","Washington"]
a = "New Orleans at Oakland"
k = a.find("at")
print (k)
for n in range(0,31):
# b = list1[n]
# print(b[0:k-1]+" "+a[0:k-1])
idx = a.find(list1[n], 0, k-1)
if idx > 0:
print(n)
break
print ("awa team at index" + str(n+1))
for n in range(0,31):
idx = a.find(list1[n], k+2, len(a))
if idx > 0:
print(n)
break
print ("hom team at index" + str(n+1))`
I just started python 2 days ago and I cannot get this to work completely. The program finds the team in the second for loop correctly, but doesn't find the team in the first for loop. I put in the statements that are commented out to see if the strings were somehow truncated, but they are correct. Can anyone tell me what is wrong here?
There's no need to brute force the search. Python has methods that accomplish what you need.
list1 = ["Arizona", "Atlanta", "Baltimore", "Buffalo", "Carolina", "Chicago",
"Cincinnati", "Cleveland", "Dallas", "Denver", "Detroit", "Green Bay", "Houston",
"Indianapolis", "Jacksonville", "Kansas City", "L.A. Chargers", "L.A. Rams",
"Miami", "Minnesota", "New England", "New Orleans", "NY Giants", "NY Jets",
"Oakland", "Philadelphia", "Pittsburgh", "San Francisco", "Seattle",
"Tampa Bay", "Tennessee", "Washington"]
a = "New Orleans at Oakland"
# Create a list of the teams involved in the game
teams = a.split(" at ")
# Iterate through the teams involved in the game
for team in teams:
# The index() method returns the lowest index in list that obj appears
index = list1.index(team)
# If the the team was found then index is valid
if index:
print(index)
print(list1[index])
if you just want to have the index, you can use the .index() you do not have to "loop"
Example code:
list1 = ["Arizona","Atlanta","Baltimore","Buffalo","Carolina","Chicago",
"Cincinnati","Cleveland","Dallas","Denver","Detroit","Green Bay","Houston",
"Indianapolis","Jacksonville","Kansas City","L.A. Chargers","L.A. Rams",
"Miami","Minnesota","New England","New Orleans","NY Giants","NY Jets",
"Oakland","Philadelphia","Pittsburgh","San Francisco","Seattle",
"Tampa Bay","Tennessee","Washington"]
a = "New Orleans at Oakland"
a = a.split(' at ')
idx_home_team = list1.index(a[0])
idx_away_team = list1.index(a[1])
print(idx_home_team, idx_away_team)

identify smallest date in a dictionary within dictionary

My data is arranged in dictionaries within dictionaries, like so:
dict = {subdict1:{}, subdict2:{},...}
where
subdict1 = { subdict_a: {"date":A, "smallest_date":False}, subdict_b : {"date":B, "smallest_date": False},...}
I'd like to loop through the subdictionaries a,b,c... and identify which of the dates A, B, C... is the smallest in each subdictionary, and change the value of 'smallest_date' to True.
How to approach this problem? I tried something like this, but couldn't quite finish it:
for subdict_number, values1 in dict.items():
smallest_date = None
for subdict_alphabet, values2 in values1.items():
if smallest_date == None or smallest_date > values2["date"]
smallest_date = values2["date"]
smallest_subdict = subdict_alphabet
And then some magic where as the loop within subdict closes sets
dict[subdict][smallest_subdict]["date"] = smallest_date
and then continues to the next subdict to do the same thing.
I can't finish this. Can you help me out? A completely different approach can be used, but as a beginner I couldn't think of one.
I've tried to keep the naming explanatory.
Given the input dictionary:
main_dict = { 'subdict1' : {'subdict_1a': {"date":1, "smallest_date":False},
'subdict_1b' : {"date":2, "smallest_date": False}},
'subdict2': {'subdict_2a': {"date":3, "smallest_date":False},
'subdict_2b' : {"date":4, "smallest_date": False}}}
Iterate through the subdicts and declare variables:
for subdict in main_dict:
min_date = 10000000
min_date_subsubdict_name = None
Iterate through the subsubdicts and determine the minimum
for subsubdict in main_dict[subdict]:
if main_dict[subdict][subsubdict]['date'] < min_date:
min_date = main_dict[subdict][subsubdict]['date']
min_date_subsubdict_name = subsubdict
Inside the first loop, but outside the second loop:
main_dict[subdict][min_date_subsubdict_name]['smallest_date'] = True
This should return the output maindict:
{'subdict2': {'subdict_2a': {'date': 3, 'smallest_date': True}, 'subdict_2b': {'date': 4, 'smallest_date': False}}, 'subdict1': {'subdict_1a': {'date': 1, 'smallest_date': True}, 'subdict_1b': {'date': 2, 'smallest_date': False}}}

Resources