![](/img/trans.png)
[英]How to sort list elements by specific indexes of specific values in python
[英]How to replace values at specific indexes of a python list?
如果我有一个清单:
to_modify = [5,4,3,2,1,0]
然后声明另外两个列表:
indexes = [0,1,3,5]
replacements = [0,0,0,0]
如何将to_modify
的元素作为 index 的indexes
,然后将to_modify
中的相应元素设置为replacements
,即运行后, indexes
应该是[0,0,3,0,1,0]
。
显然,我可以通过 for 循环做到这一点:
for ind in to_modify:
indexes[to_modify[ind]] = replacements[ind]
但是还有其他方法可以做到这一点吗? 我可以以某种方式使用operator.itemgetter
吗?
您的代码最大的问题是它不可读。 Python代码规则第一,如果它不可读,没有人会看它足够长的时间来从中获取任何有用的信息。 始终使用描述性变量名称。 差点没抓到你代码的bug,用好名字再看一遍,慢动作重放风格:
to_modify = [5,4,3,2,1,0]
indexes = [0,1,3,5]
replacements = [0,0,0,0]
for index in indexes:
to_modify[indexes[index]] = replacements[index]
# to_modify[indexes[index]]
# indexes[index]
# Yo dawg, I heard you liked indexes, so I put an index inside your indexes
# so you can go out of bounds while you go out of bounds.
很明显,当您使用描述性变量名称时,您正在使用来自自身的值来索引索引列表,这在这种情况下没有意义。
此外,当并行迭代 2 个列表时,我喜欢使用zip
函数(如果您担心内存消耗,也可以使用izip
,但我不是那些迭代纯粹主义者之一)。 所以试试这个。
for (index, replacement) in zip(indexes, replacements):
to_modify[index] = replacement
如果您的问题仅与数字列表有关,那么我会说@steabert 有您正在寻找的那些 numpy 东西的答案。 但是,您不能将序列或其他可变大小的数据类型用作 numpy 数组的元素,因此如果您的变量to_modify
中包含类似的内容,则最好使用 for 循环来执行此操作。
numpy 具有允许您使用其他列表/数组作为索引的数组:
import numpy
S=numpy.array(s)
S[a]=m
为什么不只是:
map(s.__setitem__, a, m)
有点慢,但我认为可读:
>>> s, l, m
([5, 4, 3, 2, 1, 0], [0, 1, 3, 5], [0, 0, 0, 0])
>>> d = dict(zip(l, m))
>>> d #dict is better then using two list i think
{0: 0, 1: 0, 3: 0, 5: 0}
>>> [d.get(i, j) for i, j in enumerate(s)]
[0, 0, 3, 0, 1, 0]
您可以使用operator.setitem
。
from operator import setitem
a = [5, 4, 3, 2, 1, 0]
ell = [0, 1, 3, 5]
m = [0, 0, 0, 0]
for b, c in zip(ell, m):
setitem(a, b, c)
>>> a
[0, 0, 3, 0, 1, 0]
它是否比您的解决方案更具可读性或效率? 我不知道!
对于 a 中的索引:
这将导致index
采用a
元素的值,因此将它们用作索引不是您想要的。 在 Python 中,我们通过实际迭代容器来迭代容器。
“但是等等”,你说,“对于a
每个元素,我需要使用m
的相应元素。我应该如何在没有索引的情况下做到这一点?”
简单的。 我们将a
和m
转换为对的列表(a 中的元素,m 中的元素),并迭代这些对。 这很容易做到 - 只需使用内置库函数zip
,如下:
for a_element, m_element in zip(a, m):
s[a_element] = m_element
要使其按照您尝试的方式工作,您必须获取要迭代的索引列表。 这是可行的:例如,我们可以使用range(len(a))
。 但不要那样做! 这不是我们在 Python 中做事的方式。 实际上,直接迭代你想要迭代的东西是一个美丽的、解放思想的想法。
那么 operator.itemgetter 呢?
在这里并不重要。 operator.itemgetter
的目的是将索引的行为变成某种东西,变成类似函数的东西(我们称之为“可调用”),以便它可以用作回调(例如,用于排序或最小/最大操作)。 如果我们在这里使用它,我们必须在每次循环中重新调用它来创建一个新的 itemgetter,这样我们就可以立即使用它一次并把它扔掉。 在上下文中,这只是忙碌的工作。
您可以使用字典解决它
to_modify = [5,4,3,2,1,0]
indexes = [0,1,3,5]
replacements = [0,0,0,0]
dic = {}
for i in range(len(indexes)):
dic[indexes[i]]=replacements[i]
print(dic)
for index, item in enumerate(to_modify):
for i in indexes:
to_modify[i]=dic[i]
print(to_modify)
输出将是
{0: 0, 1: 0, 3: 0, 5: 0}
[0, 0, 3, 0, 1, 0]
elif menu.lower() == "edit":
print ("Your games are: "+str (games))
remove = input("Which one do you want to edit: ")
add = input("What do you want to change it to: ")
for i in range(len(games)) :
if str(games[i]) == str(remove) :
games[i] = str(add)
break
else :
pass
pass
为什么不这样使用呢? 直接从它被删除的地方替换,无论如何你可以添加数组和do .sort .reverse 如果需要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.