简体   繁体   English

Python列表重新排序,还记得原始顺序吗?

[英]Python list reordering, remember original order?

I'm working on a Bayesian probability project, in which I need to adjust probabilities based on new information. 我正在从事贝叶斯概率项目,在该项目中,我需要根据新信息调整概率。 I have yet to find an efficient way to do this. 我还没有找到一种有效的方法来做到这一点。 What I'm trying to do is start with an equal probability list for distinct scenarios. 我想做的是从针对不同情况的等概率列表开始。 Ex. 例如 There are 6 people: E, T, M, Q, L, and Z, and their initial respective probabilities of being chosen are represented in 有6个人:E,T,M,Q,L和Z,其初始被选择的概率表示为

myList=[.1667, .1667, .1667, .1667, .1667, .1667]

New information surfaces that people in the first third alphabetically have a collective 70% chance of being chosen. 新的信息表面表明,按字母顺序排列的前三分之一的人有70%的总体被选中的机会。 A new list is made, sorted alphabetically by name (E, L, M, Q, T, Z), that just includes the new information. 将创建一个新列表,并按名称(E,L,M,Q,T,Z)的字母顺序进行排序,其中仅包含新信息。 (.7/.333=2.33, .3/.667=.45) (.7 / .333 = 2.33,.3 / .667 = .45)

    newList=[2.33, 2.33, .45, .45, .45, .45)

I need a way to order the newList the same as myList so I can multiply the right values in list comprehension, and reach the adjust probabilities. 我需要一种将newList排序为与myList相同的方法,以便可以在列表理解中乘以正确的值,并达到调整概率。 Having a single consistent order is important because the process will be repeated several times, each with different criteria (vowels, closest to P, etc), and in a list with about 1000 items. 拥有一个一致的顺序很重要,因为该过程将重复多次,每次重复使用不同的标准(元音,最接近P等),并包含大约1000个项目。 Each newList could instead be a newDictionary, and then once the adjustment criteria are created they could be ordered into a list, but transforming multiple dictionaries seems inefficient. 每个newList可以改为一个newDictionary,然后一旦创建了调整条件,就可以将它们排序为一个列表,但是转换多个字典似乎效率很低。 Is it? 是吗? Is there a simple way to do this I'm entirely missing? 有一种简单的方法可以做到这一点,而我完全不知道该怎么办?

Thanks! 谢谢!

For what it's worth, the best thing you can do for the speed of your methods in Python is to use numpy instead of the standard types (you'll thus be using pre-compiled C code to perform arithmetic operations). 对于它的价值,对于Python中的方法速度而言,您可以做的最好的事情是使用numpy而不是标准类型(因此,您将使用预编译的C代码执行算术运算)。 This will lead to a dramatic speed increase. 这将导致速度的急剧提高。 Numpy arrays have fixed orderings anyway, and syntax is more directly applicable to mathematical operations. 无论如何,Numpy数组具有固定的顺序,并且语法更直接适用于数学运算。 You just need to consider how to express the operations as matrix operations. 您只需要考虑如何将这些操作表示为矩阵操作。 Eg your example: 例如您的示例:

myList = np.ones(6) / 6.
newInfo = np.array( [.7/2, .7/2, .3/4, .3/4, .3/4, .3/4] )
result = myList * newInfo

Since both vectors have unit sum there's no need to normalise (I'm not sure what you were doing in your example, I confess, so if there's a subtlety I've missed let me know), but if you do need to it's trivial: 由于两个向量都具有单位和,因此无需进行归一化(我承认,我不确定您在示例中的操作是什么,所以如果我错过了一个微妙之处,请告诉我),但是如果您确实需要这样做,则不重要:

result /= np.sum(result)

Try storing your info as a list of tuples: 尝试将您的信息存储为元组列表:

bayesList = [('E', 0.1667), ('M', 0.1667), ...]

your list comprehension can be along the lines of 您的清单理解力可能与

newBayes = [(person, prob * normalizeFactor) for person, prob in bayesList]

where you've normalizeFactor was calculated before setting up your list comprehension 在设置列表理解之前已计算过normalizeFactor的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM