在数组中重新编号元素的有效方法(Efficient way of re-numbering elements in an array)
我是python的新手,我正在尝试实现遗传算法,但需要一些操作代码的帮助。
我用这种方式制定了问题:
- 每个个体
I
都用一串M
整数表示I
每个元素e
取0到N
的值- 0 -
N
每个数字必须至少出现在I
中一次e
的值并不重要,只要每个唯一值元素具有相同的唯一值(将它们视为类标签)e
小于或等于N
- 每个
I
N
可以不同在应用交叉操作后,我可能会生成违反这些约束中的一个或多个的子项,因此我需要找到一种方法来重新对元素进行编号,以便它们保留其属性,但符合约束条件。
例如:
parent_1 (N=5): [1 3 5 4 2 1|0 0 5 2] parent_2 (N=3): [2 0 1 3 0 1|0 2 1 3] *** crossover applied at "|" *** child_1: [1 3 5 4 2 1 0 2 1 3] child_2: [2 0 1 3 0 1 0 0 5 2]
child_1
显然仍然满足所有约束,因为N = 5并且所有值0-5在数组中至少出现一次。问题在于子2 - 如果我们使用
max(child_2)
计算N的方法,我们得到值5,但如果我们计算唯一值的数量,那么N = 4,这是N的值应该是什么。 我所要求的(以非常漫长的方式,被授予)是一个好的,pythonic方式这样做:child_2: [2 0 1 3 0 1 0 0 5 2] *** some python magic *** child_2': [2 0 1 3 0 1 0 0 4 2] *or* child_2'': [0 1 2 3 1 2 1 1 4 0]
child_2''
用于说明值本身并不重要,只要唯一值的每个元素映射到相同的值,就会满足约束。这是我到目前为止所尝试的:
value_map = [] for el in child: if el not in value_map: value_map.append(el) for ii in range(0,len(child)): child[ii] = value_map.index(child[ii])
这种方法可以工作并返回类似于
child_2''
的结果,但我无法想象它在字符串上迭代两次的方式非常有效,所以我想知道是否有人对如何使其更好有任何建议。谢谢,对于这么简单的问题这么长的帖子感到抱歉!
I am reasonably new to python and am trying to implement a genetic algorithm, but need some assistance with the code for one of the operations.
I have formulated the problem this way:
- each individual
I
is represented by a string ofM
integers- each element
e
inI
takes a value from 0 toN
- every number from 0 -
N
must appear inI
at least once- the value of
e
is not important, so long as each uniquely valued element takes the same unique value (think of them as class labels)e
is less than or equal toN
N
can be different for eachI
after applying the crossover operation i can potentially generate children which violate one or more of these constraints, so i need to find a way to re-number the elements so that they retain their properties, but fit with the constraints.
for example:
parent_1 (N=5): [1 3 5 4 2 1|0 0 5 2] parent_2 (N=3): [2 0 1 3 0 1|0 2 1 3] *** crossover applied at "|" *** child_1: [1 3 5 4 2 1 0 2 1 3] child_2: [2 0 1 3 0 1 0 0 5 2]
child_1
obviously still satisfies all of the constraints, as N = 5 and all values 0-5 appear at least once in the array.The problem lies with child 2 - if we use the
max(child_2)
way of calculating N we get a value of 5, but if we count the number of unique values then N = 4, which is what the value for N should be. What I am asking (in a very long winded way, granted) is what is a good, pythonic way of doing this:child_2: [2 0 1 3 0 1 0 0 5 2] *** some python magic *** child_2': [2 0 1 3 0 1 0 0 4 2] *or* child_2'': [0 1 2 3 1 2 1 1 4 0]
child_2''
is there to illustrate that the values themselves dont matter, so long as each element of a unique value maps to the same value, the constraints are satisfied.here is what i have tried so far:
value_map = [] for el in child: if el not in value_map: value_map.append(el) for ii in range(0,len(child)): child[ii] = value_map.index(child[ii])
this approach works and returns a result similar to
child_2''
, but i can't imagine that it is very efficient in the way it iterates over the string twice, so i was wondering if anyone has any suggestions of how to make it better.thanks, and sorry for such a long post for such a simple question!
满意答案
您需要多次迭代列表,我认为没有办法解决这个问题。 毕竟,在开始更改元素(第二遍)之前,首先必须确定不同元素的数量(第一遍)。 但是请注意,由于对
index
的重复调用而not in
列表中具有O(n),因此可能具有最多O(n ^ 2)的不同元素的数量。或者,您可以使用
dict
而不是value_map
的list
。 字典比列表具有更快的查找速度,因此,复杂性应该确实在O(n)的数量级上。 您可以使用(1)字典理解来确定旧值到新值的映射,以及(2)用于创建更新子项的列表理解。value_map = {el: i for i, el in enumerate(set(child))} child2 = [value_map[el] for el in child]
或者使用
for
循环就地更改孩子。for i, el in enumerate(child): child[i] = value_map[el]
You will need to iterates the list more than once, I don't think there's any way around this. After all, you first have to determine the number of different elements (first pass) before you can start changing elements (second pass). Note, however, that depending on the number of different elements you might have up to O(n^2) due to the repetitive calls to
index
andnot in
, which have O(n) on a list.Alternatively, you could use a
dict
instead of alist
for yourvalue_map
. A dictionary has much faster lookup than a list, so this way, the complexity should indeed be on the order of O(n). You can do this using (1) a dictionary comprehension to determine the mapping of old to new values, and (2) a list comprehension for creating the updated child.value_map = {el: i for i, el in enumerate(set(child))} child2 = [value_map[el] for el in child]
Or change the child in-place using a
for
loop.for i, el in enumerate(child): child[i] = value_map[el]
相关问答
更多消除大型数组中某些元素的有效方法(The efficient way to eliminate some elements in a large array)
将数组中的元素插入现有Html元素的有效方法是什么?(What's the efficient way to insert elements from array to existing Html element?)
是否有一种有效且安全的方法来反转数组中的所有元素?(Is there an efficient and secure way to reverse all elements in an array?)
从阵列中删除元素的最有效方法,然后减小数组的大小(Most efficient way to remove an element from an array, then reduce the size of the array)
编号数组元素 - Bash脚本(Numbering Array Elements - Bash Scripting)
检查数组中元素是否已更改的最有效方法(Most efficient way to check if elements in an array have changed)
使用numpy测试来自一个数组的每个元素是否存在于另一个数组中的最有效方法(Most efficient way to test whether each element from one array exists in a another array, using numpy)
在Perl中匹配数组元素最有效的过程?(Most efficient process of matching array elements in Perl?)
在数组中重新编号元素的有效方法(Efficient way of re-numbering elements in an array)
向量中的编号元素(Numbering elements in vector)
相关文章
更多报错说找不到abbrev这个方法,但Array有这个方法的吧?
scala数组操作
Java 数组
Guava处理java byte类型工具类-Bytes类
动态拼接JSON数组的问题
怎么把2个数组里面相同的元素组合成一个新的数组
2014最有效的微店推广方法
Guava Chars类-char类型的实用工具类
Guava 处理java short实用工具类-Shorts类
关于Ibatis中的resultMap元素的问题
最新问答
更多绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)
如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)
ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)
SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)
宁夏银川永宁县望远镇哪里有修mp5的?
我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)
郑州会计培训班
如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)
在响应图像上叠加网格(Overlay grid on responsive image)
无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!