multiple dict in python [closed] - python

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 7 years ago.
Improve this question
How can I build a nested dictionary from reading configure files in Python, such that I can access it like this:
multi_dict["name1"]["name2"]["name3"] = some_value
My current approach is:
if name1 not in dict:
dict["name1"] = {}
dict["name1"]["name2"] = {}
(etc.)
But this ways seems so boring! Is there a more elegant way to do this?
The configure files that I'm reading are like the following:
[a1]
key1=value1
[a2]
[.a3]
key2=value2
[..a4]
key3=value3
The . prefixes mean "another level of nesting". So in this example, I would set:
dict[a1][key1]=value1
dict[a2][a3][key2]=value2
dict[a2][a3][a4][key3]=value3

Related

how i can organize and use reverse in a dictionary python [closed]

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 months ago.
Improve this question
this is dictionary I don't know how to use sort and reverse.
for some reason don't print the dictionary
disc: {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
I get that the 'disc' is a variable holding the dictionary.
In this case just sort and reverse the values that are lists.
disc = {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
for l in disc.values():
l.reverse()
for l in disc.values():
l.sort()

How to save variables from a console text in Python? [closed]

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
What code should I use to write a program that stores the following content in a variable?
I cannot use an external file.
{'title':'El Más Allá','aka':'E tu vivrai nel terrore - Laldilà','director':'Lucio Fulci', 'year':1981, 'country':'Italia'}
just store it in a dict like this
import json
instr = input(">>>")
myDict = json.loads(instr)

(Python 3.8) Is it possible to call a variable or dict by input? [closed]

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
Like if I type in something in an input prompt, I want to search my modules for a variable or something to reference, like so:
variable_caller = input().function_here
# or:
variable_caller.function_here
Any ideas?
You can use getattr(YOUR_MODULE, variable_selection)
getattr() returns value of YOUR_MODULE."variable_selection".
It gets the value of variable variable_selection and converts it into ID.
For example:
a = "attribute"
getattr(module, a) # Returns module.attribute

formating string and passing argument at runtime [closed]

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 2 years ago.
Improve this question
eg-In this instead of using 21 (a value), I want to use a variable to generalize it
print("{:-^21}".format(".|."*(2*(i+1)-1)))
I want to use something like this
print("{:-^M}".format(".|."*(2*(i+1)-1)))
That can easily enough be done. For example:
M = 40
i = 3
print("{val:-^{width}}".format(width=M, val=".|."*(2*(i+1)-1)))
Outputs:
---------.|..|..|..|..|..|..|.----------
You could also do it with f-strings (note the outer ' because " is used on the inner expression):
print(f'{".|."*(2*(i+1)-1):-^{M}}')

Delete key from python dictionary having specific format [closed]

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 2 years ago.
Improve this question
Key column of my dictionary contain words such as
1. Performance_appraisal
2. Continuous_feedback etc
3. *_appraisal
I need to delete words which are similar to the third example. i.e containing *_ in it
How do I do that?
You can use:
def clean_dict(my_dict):
for key in my_dict.keys():
if '*_' in key:
del my_dict[key]
return clean_dict(my_dict)

Categories