i have a special list like this:
[0,0,0,1,0,1,0,1,1,1,0,1]
I want it map to a char list like:
['+','+','+','-','+','-','+','-','-','-','+','-']
here is some code. it multiplies probabilities to calculate joint probabilities:
def solve_all_asigned(self,joint_s):
mult_val=1
for tab in self.tables:
if tab['decision']:
continue
name=tab['name']
nr=name.replace('.',' ')
nr_spd=re.split(' ',nr)
val=''
check_list=[x in joint_s.keys() for x in nr_spd]
if False in check_list:
continue
val=''.join(map(joint_s.get,nr_spd))
mult_val=mult_val*tab[val]
return mult_val
n=22
joint_s2={}
all_combinations=list(itertools.product([0,1],repeat=n))
for binlist in all_combinations:
for i in range(n):
joint_s2[nan_set[i]]='+' if binlist[i]=='0' else '-'
s.append(self.solve_all_asigned(joint_s2))
ss=sum(s)
joint_s:{'AA': 'nan', 'AC': 'nan', 'AB': 'nan', 'AE': 'nan', 'AD': 'nan', 'AF': 'nan', 'A': 'nan', 'C': 'nan', 'B': 'nan', 'E': 'nan', 'D': 'nan', 'G': 'nan', 'F': 'nan', 'I': 'nan', 'H': 'nan', 'K': 'nan', 'J': 'nan', 'M': 'nan', 'L': 'nan', 'O': 'nan', 'N': 'nan', 'Q': 'nan', 'P': '+', 'S': 'nan', 'R': 'nan', 'U': 'nan', 'T': 'nan', 'W': 'nan', 'V': 'nan', 'Y': 'nan', 'X': '+', 'Z': 'nan'}
nan_set:['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'J', 'M', 'L', 'O', 'N', 'Q', 'S', 'R', 'U', 'T', 'W', 'V']
tab:{'name': 'A.B', '--': 0.19999999999999996, 'decision': False, '+-': 0.6, '++': 0.4, '-+': 0.8}
how can I do this?
To answer your original question, here's an efficient way to map a list of 0 & 1 integers to plus and minus signs.
output
However, as I said above, this may not actually be necessary for your probability calculations.
Here are some other improvements that can be made to your code.
wastes a lot of RAM. For
n = 22
, theall_combinations
list contains over 4 million 22 item tuples. It's far more efficient to iterate over the output ofproduct
directly:In this line:
You test
binlist[i]
against the string'0'
, but thebinlist
tuples contain the integers 0 and 1, not strings, and Python will not automatically convert numeric strings to integers or vice versa.Another puzzling section is
Why use regex? Why not just use the built-in
str.split
method, egOTOH, if your
name
fields may be separated by one or more spaces as well as by at most a single dot then you can do