Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm a newbie programming in python and I canĀ“t find a element in a complex dict (for me at least).
This dict contains items "FareAttribute" and the same time this class contains elements "FareRule". I want to find the element that matches FareRule.origin_id=="city1" and FareRule.destination_id=="city2".
How I can to find this?
Thanks for any comment in advance. I'm a bit lost
Edit to add dict (output when print first item). The classes belongs to transitfeed library (Google Transit). Right now I can't execute program, I'm out.
{u'AA': <FareAttribute [('currency_type', u'EUR'), ('fare_id', u'AA'), ('payment_method', 0), ('price', 1.5), ('rules', [<FareRule [('contains_id', None), ('destination_id', u'A'), ('fare_id', u'AA'), ('origin_id', u'A'), ('route_id', None)]>]), ('transfer_duration', None), ('transfers', 0)]>,...}
EDIT2 Please try something like this (if python 2.7):
for fare in schedule.GetFareAttributeList():
for rule in fare.GetFareRuleList():
if rule.origin_id == 'B1' and rule.destination_id == 'B1':
print rule
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
What is the code required to print the ContractName sub category (under result) ?
I know to get result I have my dictionary result :
response_dict = response.json()
print()response_dict["result"]
But how do I get ContractName??
{
"status":"1",
"message":"OK",
"result":[
{
"SourceCode":"test",
"ContractName":"DAO",
"CompilerVersion":"v0.3.1-2016-04-12-3ad5e82",
}
]
}
The value of result in your dictionary is a list. That list, in your example, contains one element which is another dictionary.
Therefore:
response_dict['result'][0]['ContractName']
...will give you what you need.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Dictionary:
section of dictionary
My Code:
code
Error says:
3
So how come the date key works fine but for freq it fails?
ps. my first time posting, so am very sorry for the sloppy structure of the post
This can only happen when one of your day is missing the freq parameter.
Try catching the day in which the error is happening. Then manually check that particular entry.
date_list = []
frequency_list = []
try:
for i in obj:
date = obj[i]["date"]
frequency = obj[i]["freq"]
date_list.append(date)
frequency_list.append(frequency)
except:
print(i)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to check if the "nilai" is None/Null
cursor.execute('SELECT s.kode_ktg_id,(SUM(n.nilai_angka * S.nilai_mk)/SUM(s.nilai_mk)) as nilai_ktg FROM mahasiswa_khs k, mahasiswa_convert_nilai n, mata_kuliah_si s WHERE k.nilai = n.nilai_huruf AND k.nim = "%s" AND k.kode = s.kode GROUP BY s.kode_ktg_id',[nim])
nilai = cursor.fetchall()
I check with this
if nilai[0] is None:
But I got error tuple index out of range
This is because nilai is an empty tuple, since it returned no records.
You can check if it is empty with:
if not nilai:
# no records
else:
# at least one record
That being said, in Django you can make use of the Django ORM, which is often safer, and wraps elements in model objects.
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 5 years ago.
Improve this question
I am having trouble to query my database maybe someone could give me a hand.
I am using a django application so I guess Sqlite3 >> and the output I would like to get is the score value
b = Answer.objects.get(id = 23)
which give me an output of :
<Answer: Answer to questionID '4' : AnswerID '23'>
when I do :
b.values
I get a dict in the form :
['{
"1)Long descriptive text":Score,
"2)Long descriptive text":Score,
"3)Long descriptive text":Score,
"4)Long descriptive text":Score
}']
with score beeing an Integer from 0 to 100 so for example "Long descriptive text":85
I need to extract the score using a query but I can't manage to do it
Normaly for a Dict[key:value] I would do a Dict[key] but here I do not know how to do it
could you give me a hand
Thx you very much
This looks suspiciously like Django If so:
so b = Answer.objects.get(id = 23) is not truely that - what you are seeing is the str function of the Answer when you print it off. because you used .get rather then a .filter you get the object rather then a QuerySet (which you can think of as being a list).
Basically, I suspect you shouldn't be using values, but accessing the data... something like
b = Answer.objects.get(id=..)
b.score
or if you wanted to loop over other answers:
answers = Answer.objects.filter(...)
for a in answers:
a.score
for what the .score is, look in your models.py file - look what parameters is has (things looking like score = models.IntegerField() etc, then you would use a.score)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
mother_dict=
{'son_dict':{'Name':'Jason','Age':26},'daughter_dict':{'Name':'Emma','Age':19}}
father_dict={}
father_dict['Child']=mother_dict[son_dict]
I need a way to replace father_dict['Child'] with a dictionary from mother_dict based on input.
I've tried deleting the contents of father_dict and replacing them with the contents of mother_dict with .update(), but that of course adds the whole dictionary, I've tried using input() to ask the user for a child, so if they said 'Jason' it would replace 'Child' with son_dict, but when I got into families with ten or so kids there would need to be ten functions, and if the children's names changed then both the functions and the dictionaries would need to be re-written. I'm hung up on using input to grab a specific dictionary from mother_dict and copying it to father_dict.
Maybe something like the following?
choice = ''
mother_dict= {'son_dict':{'Name':'Jason','Age':26},'daughter_dict':{'Name':'Emma','Age':19}}
father_dict = {}
while choice not in mother_dict:
choice = raw_input('Which dict do you want? ')
father_dict[choice] = mother_dict[choice]
This code gets input until the input is valid (it is in mother_dict), and then it adds that input to father_dict.