问题描述
我有一个像这样的特殊列表:
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}
我该怎么做?
推荐答案
要回答您的原始问题,这是一种映射0&的列表的有效方法.加号和减号1个整数.
To answer your original question, here's an efficient way to map a list of 0 & 1 integers to plus and minus signs.
binlist = [0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1]
a = ['+-'[u] for u in binlist]
print(a)
输出
['+', '+', '+', '-', '+', '-', '+', '-', '-', '-', '+', '-']
但是,正如我上面说的那样, 对于您的概率计算实际上并不是必需的.
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.
all_combinations=list(itertools.product([0,1],repeat=n))
for binlist in all_combinations:
浪费了很多的RAM.对于n = 22
,all_combinations
列表包含超过400万个22个项目元组.直接迭代product
的输出效率更高:
wastes a lot of RAM. For n = 22
, the all_combinations
list contains over 4 million 22 item tuples. It's far more efficient to iterate over the output of product
directly:
for binlist in itertools.product([0, 1], repeat=n):
在这一行:
joint_s2[nan_set[i]]='+' if binlist[i]=='0' else '-'
您针对 string '0'
测试了binlist[i]
,但是binlist
元组包含 integers 0和1,而不是字符串,并且Python不会自动将数字字符串转换为整数,反之亦然.
You test binlist[i]
against the string '0'
, but the binlist
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
nr=name.replace('.',' ')
nr_spd=re.split(' ',nr)
为什么要使用正则表达式?为什么不只使用内置的str.split
方法,例如
Why use regex? Why not just use the built-in str.split
method, eg
nr_spd = name.split('.')
OTOH,如果您的name
字段可能被一个或多个空格以及最多单个点分隔,那么您可以这样做
OTOH, 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
nr_spd = name.replace('.', ' ', 1).split()
这篇关于将二进制列表转换为字符列表的最佳方法(两个特殊字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!