Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I have question like this :
Car_detail= {
'Car_1': {
'color': 'Red',
'type': 'Car 1'
},
'Car_2': {
'color': 'purple',
'type': 'Car 2'
}
}
how can I print the type of each car I mean the result will be
type: Car 1
type: Car 2
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 days ago.
Improve this question
I'm trying to edit a dictionary. My logic works without the use of a function, but once I add a function to the mix, it doesn't.
The results of the following code are what I desire.
thDic = {
'123': {
'type': 'Web Map'
},
'456': {
'type': 'Web Mapping Application'
}
}
for k,v in thDic.items():
v['type'] = 'asdf'
thDic
{'123': {'type': 'asdf'}, '456': {'type': 'asdf'}}
However, when I put the for loop into a function, it stops giving me the desired results.
def test(thDic):
for k,v in thDic.items():
v['type'] = 'asdf'
return thDic
thDic
{'123': {'type': 'Web Map'}, '456': {'type': 'Web Mapping Application'}}
You did not call the function
def test(thDic):
for k,v in thDic.items():
v['type'] = 'asdf'
return thDic
thDic = test(thDic)
thDic
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to add a dictionary to a list that is itself the value in a dictionary key value pair.
user_data = {
"watched": []
}
movie = {
"title": 'Title A',
"genre": 'Horror',
"rating": 3.5
}
How would I add the movie value to the watched list value?
This will do what you want:
In [2]: user_data["watched"].append(movie)
In [3]: user_data
Out[3]: {'watched': [{'title': 'Title A', 'genre': 'Horror', 'rating': 3.5}]}
Note that accessing a dictionary returns the value associated with the given key, so user_data["watched"] gives us the list object associated with that key "watched". Since we have a list, we can use the .append() method to add the movie dictionary to the list.
The answer to your question is:
user_data["watched"].append(movie)
In addition, If you have a list of movies like:
movie = [
{
"title": 'Title A',
"genre": 'Horror',
"rating": 3.5
},
{
"title": 'Title B',
"genre": 'Action',
"rating": 4
}
]
then you can use the assignment operator
user_data["watched"] = movie
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a function that returns a list of dictionaries like this:
[{'Status': 'Deleted', 'Name': "My First Test"}, {'Status': 'Modified', 'Name': "My First Test"}]
As you can see, "My First Test" is in there twice. Normally this wouldn't be an issue, however, based on what I know about what's happening on the back-end, the only dict that I actually want is the "Modified" dict.
Essentially, I'm looking for a way to say "if dict['Status'] == 'Modified' and dict['Status'] == 'Deleted' for the same Name, delete the one with the 'Deleted' status."
I don't know if I understood well your question.
But it's a tip:
list = [
{
'Status': 'Deleted',
'Name': "My First Test"
},
{
'Status': 'Modified',
'Name': "My First Test"
}]
filterd_list = [l for l in list if l['Status'] == 'Modified']
print(filterd_list) # Only the modified one will be printed
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm creating a dictionary from the college scorecard API, but I'm having trouble joining integer queries to my dictionary (ie. "2015.student.size"). How would I do that in my code? I've tried "str() for f in", but that doesn't seem to work.
This is what I've written in Python so far:
import requests
import json
def main():
url = 'https://api.data.gov/ed/collegescorecard/v1/schools.json'
payload = {
'api_key': "api_key_here",
'_fields': ','.join([
'school.name',
'school.school_url',
'school.city',
'school.state',
'school.zip',
]),
'school.operating': '1',
'2015.academics.program_available.assoc_or_bachelors': 'true',
'2015.student.size__range': '1..',
'school.degrees_awarded.predominant__range': '1..3',
'school.degrees_awarded.highest__range': '2..4',
'id': '240444',
}
data = requests.get(url, params=payload).json()
for result in data['results']:
print(','.join(result.values()))
main()
What happens when I run the program:
vagrant#vagrant:/vagrant/scripts$ python test.py
Madison,www.wisc.edu,University of Wisconsin-Madison,WI,53706-1380
When I add "print data":
{u'results': [{u'school.city': u'Madison', u'school.school_url': u'ww
w.wisc.edu', u'school.name': u'University of Wisconsin-Madison', u'sc
hool.state': u'WI', u'school.zip': u'53706-1380'}], u'metadata': {u'p
er_page': 20, u'total': 1, u'page': 0}}
I think you are making this more difficult than necessary. Instead of the for loop, you can do
print(data)
Or if you want each dictionary from the list to be on its own line, instead of
print(','.join(result.values()))
Just do
print(result)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
My data is in the following format:
[{u'value': 7681, u'time': u'2017-07-12T12:15:54.107488923Z'}, {u'value':
7672, u'time': u'2017-07-12T12:26:01.295268409Z'}]
I need to remove all the u prefixes from this data. How can I do that using Python 2.7? In fact, I want it to be like:
[{'value': 7681, 'time': '2017-07-12T12:15:54.107488923Z'}, {'value':
7672, 'time': '2017-07-12T12:26:01.295268409Z'}]
It's unclear what ResultSet is and its format from your question, however the following example code might be helpful:
import csv
csv_filename = 'result_set.csv'
ResultSet = {"(u'maxbotix_depth', None)": [{u'time': u'2017-07-12T12:15:54.107488923Z',
u'value': 7681},
{u'time': u'2017-07-12T12:26:01.295268409Z',
u'value': 7672}]}
with open(csv_filename, mode='wb') as csv_file:
writer = csv.writer(csv_file)
for obj in ResultSet["(u'maxbotix_depth', None)"]:
time, value = obj[u'time'], obj[u'value']
print('time: {}, value: {}'.format(time, value)) # optional
writer.writerow((time, value))
Printed output:
time: 2017-07-12T12:15:54.107488923Z, value: 7681
time: 2017-07-12T12:26:01.295268409Z, value: 7672
Contents of file created:
2017-07-12T12:15:54.107488923Z,7681
2017-07-12T12:26:01.295268409Z,7672