Python, access data Dictionary in Dictionary [duplicate] - python

This question already has answers here:
Accessing dictionary of dictionary in python
(7 answers)
Closed 6 years ago.
I have data like this :
As you can see : dictionary:list[{dictionary1},{dictionary2} ,... ]
How i can create loop to access all 'type' inside of every small dictionary ?
Thanks very much !

I had an answer after read instructions,
i can access data with code like this :
for item in result_records:
getValues = result_records.get(item) # Return list of dictionary
for subDict in getValues: # With every dictionary
if(subDict['type'] == 'VIEW_ITEM'):
Thanks all very much for helping me !

Related

Sort list containing dictionary according to date [duplicate]

This question already has answers here:
How do I sort a list of dictionaries by a value of the dictionary?
(20 answers)
Closed 12 months ago.
I want to sort the below-provided list based on the 'date' value using DSA and not any external/internal modules in Python. I tried but am unable to come up with any working solution here.
Kindly help me with that.
a = [{"date":[14,1,2020],"stockValue":-0.57357144},
{"date":[9,2,2021],"stockValue":-0.66407406},
{"date":[10,2,2020],"stockValue":-0.62166667}]
The expected answer will be as below.
a = [{"date":[14,1,2020],"stockValue":-0.57357144},
{"date":[10,2,2020],"stockValue":-0.62166667},
{"date":[9,2,2021],"stockValue":-0.66407406}]
a.sort(key=lambda x: x['date'], reverse=True)

Generating Variable Name Dynamically In Python 3 [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I'm porting some Python 2 code to Python 3
I know this code snippet is bad practice, but I am looking for a way replace the exec() calls. I basically get "None" back, as predicted by the migration documents. I tried eval() but I get syntax error messages.
What are the alternatives dynamically generating variable names?
value = "test"
for field in ['overviewSynopsis', 'callsToAction_productLevel']:
exec(field +'_value = ""')
exec(field +'_value = value')
print(exec(field + "_value"))
Use a dictionary instead. You can map a key name to a value.

Possible to convert string to a named variable or alternative? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
generating variable names on fly in python [duplicate]
(6 answers)
Closed 3 years ago.
I have a ticker and I want to check a specific list of tickers to see if the ticker is found. If it is found, it will replace it.
The new tickers come from another data source and therefore do not know which specific list of tickers to check. In order to find that list, I can pass the lists name as a string but upon iterating the code (naturally) recognizes this as string as opposed to a list to iterate.
Is there a way to have the code/function recognize that the string is actually a specific list to be checked? In reading other questions, I know this may not be possible...in that case what is an alternative?
list_1=['A','B']
list_2=['C','D']
old_ticker='A'
new_ticker='E'
assigned_list='list_1'
def replace_ticker(old_ticker,new_ticker,list):
for ticker in list:
if new_ticker in list:
return
else:
list.append(new_ticker)
list.remove(old_ticker)
replace_ticker(old_ticker,new_ticker,assigned_list)
You key the needed lists by name in a dictionary:
ticker_directory = {
"list_1": list_1,
"list_2": list_2
}
Now you can accept the name and get the desired list as ticker_directory[assigned_list].
list_1=['A','B']
list_2=['C','D']
lists = {
'list_1':list_1,
'list_2':list_2
}
old_ticker='A'
new_ticker='E'
assigned_list='list_1'
def replace_ticker(old_ticker,new_ticker,list_name):
if old_ticker not in lists[list_name]:
return
else:
lists[list_name].append(new_ticker)
lists[list_name].remove(old_ticker)
replace_ticker(old_ticker,new_ticker,assigned_list)
print(lists[assigned_list])
This is the complete program from what i perceived.
#prune already answered this, I have just given the whole solution
There are at least two possibilities:
1 As noted in comments kind of overkill but possible:
Use eval() to evaluate string as python expressions more in the link:
https://thepythonguru.com/python-builtin-functions/eval/
For example:
list_name = 'list_1'
eval('{}.append(new_ticker)'.format(list_name))
2 Second
Using locals() a dictionary of locally scoped variables similiar to the other answers but without the need of creating the dict by hand which also requires the knowledge of all variables names.
list_name = 'list_1'
locals()[list_name].append(new_ticker)

How do I organize a map In Python [duplicate]

This question already has answers here:
How do I sort a dictionary by value?
(34 answers)
Closed 6 years ago.
I am new to python and I am just beginning to learn the syntax. I would like to create a map (like in java but I understand that they may be called something else in python) so I would assume the code might look something like this
map = {}
map.append("somestring": 12)
map.append("anotherstring": 10)
map.append("yetanotherstring": 15)
'''
then organize the map by the numbers from largest to smallest
so if I were to then run through the map with a for loop it would
print
"yetanotherString" : 15
"somestring" : 12
"anotherstring" : 10
'''
I am clueless In almost every step of this process from declaring a "map" to organizing it by the ints. Though organizing it by the ints is my biggest problem.
They're called dictionaries!
Try like this:
pythonDict = {}
pythonDict['somestring'] = 12
See more in http://learnpythonthehardway.org/book/ex39.html
To learn more about iterating through the dictionary see https://docs.python.org/2/library/itertools.html

python default dictionary calling key values [duplicate]

This question already has answers here:
Python Dictionary DataStructure which method d[] or d.get()?
(5 answers)
Closed 8 years ago.
I have written a bit of code using
setting value
dic["key"] = "someval"
and fetching it the same way
print dic["key"]
then I discovered that an alternative way to fetch a dictionary value is to use
print dic.get("key")
I want all my code to use the same method, so should I rewrite all using dic.get("key") ?
If you have a flat dictionary and you want to add or modify a key/value pair, than the best way is to use a simple assignment:
h[key] = value
The get method is useful when you want to get a value from a dictionary for existing keys or use a default value for non-existing keys:
print h.get(key, "Does not exist!")

Categories