How do I organize a map In Python [duplicate] - python

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

Related

python - what does the FOR loop do in this list definition? [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 1 year ago.
I don't know Python super well but I know it well enough that I'm trying to translate some Python code to Lua. But I can't figure out what this code is supposed to do.
var_declarations = [
VarDecl(var_node, type_node)
for var_node in var_nodes
]
VarDecl is a class, and var_nodes is a list. Full code is here.
That's called a "list comprehension". It is exactly the same as:
var_declarations = []
for var_node in var_nodes:
var_declarations.append( VarDecl(var_node, type_node) )
It just builds a new list with the result of calling VarDecl one at a time.
This is a list comprehension syntax. Looking at the code snippet that you referred to, it is instantiating the VarDecl class for each var_node in var_nodes list(array) and creating a new list called var_declarations.

How do I make a "iterable variable"? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I want to make a variable that can be changed iterably through for loops, as in:
for i in range(15):
var + i = 12
so that when it is finished, there will be 15 variables that has the number 12. I've done this through code.org (I'm taking computer science) and I want to know if this is possible through python.
Like that?
g = globals()
for i in range(15):
g[f'var{i}'] = 12
It can be useful for practise, to understand how variables storing. In real projects you should use some sort of collections. Usually list or dict.

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)

(Python) Can I store the functions themselves, but not their value, in a list [duplicate]

This question already has answers here:
Python Argument Binders
(7 answers)
Closed 8 months ago.
As you can see from the code below, I'm adding a series of functions to a list.
The result is that each function gets ran and the returned value is added to the list.
foo_list = []
foo_list.append(bar.func1(100))
foo_list.append(bar.func2([7,7,7,9]))
foo_list.append(bar.func3(r'C:\Users\user\desktop\output'))
What I would like to know is, is it possible to have the function stored in the list and then ran when it is iterated upon in a for loop?
Yeah just use lambda:
foo_list = []
foo_list.append(lambda: bar.func1(100))
foo_list.append(lambda: bar.func2([7,7,7,9]))
foo_list.append(lambda: bar.func3(r'C:\Users\user\desktop\output'))
for foo in foo_list:
print(foo())

Does anyone actually know how the order of a set is decided in Python? [duplicate]

This question already has answers here:
'order' of unordered Python sets
(5 answers)
Closed 10 years ago.
There does seem to be some consistency in that calling set() on a string always seems to resolve to the same (non-alabetical) order, and both
set([1,2,3]) & set([1,2,3,4])
and its jumbled up cousin
set([2,3,1]) & set([4,3,1,2])
will result in orderly-looking set([1,2,3]).
On the other hand, something like a bit more racy, such as
from random import randint
set([randint(0,9) for x in range(3)])
will sometimes give something like set([9, 6, 7]) ...
... what is going on here?
You should consider sets as unordered collections
They are stored in a hash table.
Additionally, as you continue to add elements, the hash will be shifted into a larger table, so that order may change dramatically.
There is no guarantee that the order will be the same across different Python versions/implementations.

Categories