Python ordered dictionary: Why is [] notation required to change dictionary values using a for loop?
Tag : python , By : Robert Daniel Pickar
Date : March 29 2020, 07:55 AM
wish of those help In the loop, name and val are bound to each of the objects in the mapping in turn. Simply rebinding the names will not modify the original iterable.
|
Initialize dictionary, loop through list, add words and increment counter in dictionary
Date : March 29 2020, 07:55 AM
around this issue You are doing this in a confusing and unnecessarily complicated way. collections.Counter can do most of the work for you: import collections
words = {'abandon':-2,'abandoned':-2,'abandons':-2,'abducted':-2,'abduction':-2,
'abductions':-2,'abhor': -3}
# throw away the values in the dictionary because they aren't being used
words = words.keys()
# this is a list [] not a dictionary ()
sentence = ['abandon', 'abandoned', 'abhorrent', 'hello', 'smart', 'hello',
'die', 'happy', 'sad', 'up', 'down', 'happy', 'smart','cool',
'clean', 'mean']
counts = collections.Counter([word for word in sentence if word not in words])
print counts
Counter({'hello': 2, 'smart': 2, 'happy': 2, 'down': 1, 'die': 1, 'up': 1,
'sad':1, 'abhorrent': 1, 'clean': 1, 'mean': 1, 'cool': 1})
|
Prevent Overwrite of Nested Dictionary Values while Creating Dictionary in a For Loop
Date : March 29 2020, 07:55 AM
help you fix your problem I am very new to Python and am trying to write a script that loops through an existing dictionary and reorganizes the data so it is in a nested dictionary. The existing dictionary is created from some code I found online that turns a row from an SQL query into a dictionary. I know this is a bit redundant, I just don't know how to edit this code to make it display what I want. , You create a d dictionary with: d = {}
D[station] = d
while row is not None:
station = row[u'STATION']
# ....
d = {} # Here, we create a new dict
for i in ('date_obs', 'temperature', 'altimeter', 'dewpoint', 'windspeed', 'winddirection'):
d[i] = locals()[i]
D[station] = d # D[station] now refers to our new dict
D={}
row = curs.fetchone
while row is not None:
station = row[u'STATION']
D[station] = {'date_obs': row[u'DATE_OBSERVATION'],
'temperature': row[u'TMPC'],
'altimeter': row[u'ALTM'],
'dewpoint': row[u'DWPC'],
'windspeed': row[u'SPED'],
'winddirection': row[u'DRCT']
}
row = curs.fetchone()
print D
fields = {'date_obs': u'DATE_OBSERVATION', 'temperature': u'TMPC'} # and so on...
D={}
row = curs.fetchone
while row is not None:
station = row[u'STATION']
D[station] = { key: row[field] for key, field in fields.items() }
row = curs.fetchone()
print D
|
for loop creates a dictionary with x entries but after the loop, length of dictionary is < x
Tag : python , By : user157654
Date : March 29 2020, 07:55 AM
To fix the issue you can do Check to see if your Key already exists before assigning a Value to it. # Not sure if str(tuple(i)) will work - regardless apply logic like this to make the Key unique
counter = 0
while((str(tuple(i)) + '_' + str(counter)) in genFit.keys()):
counter += 1
genFit[str(tuple(i) + '_' + str(counter)] = tmp
|
Using a for loop to create a dictionary. How do you stop the for loop after the dictionary is constructed?
Date : March 29 2020, 07:55 AM
hop of those help? I am trying to write a script to quickly get statistics for a class I am teaching based on grades entered into an excel sheet. , I think you should first do is to create a list of students. student_list = ['Student 1','Student 2','Student 3']
dic = {}
for student_name in student_list:
alias = "Student" + str(randint(1,1000))
dic.update({alias : student_name})
from random import randint
student_list = ['Arnold','Ben','Carl']
dic = {}
for student_name in student_list:
alias = "Student" + str(randint(1,1000))
dic.update({alias : student_name})
print(dic)
|