Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-替换列表中的特定数字_Python_List - Fatal编程技术网

Python-替换列表中的特定数字

Python-替换列表中的特定数字,python,list,Python,List,我有一份调查问卷的分数: list= [1, 2, 2, 4, 1, 4, 1, 3, 2, 2, 2, 1, 1, 1, 4, 3, 4, 2, 1, 1, 1, 1, 1, 2, 2, 2, 4, 1, 4, 1, 2, 4, 4, 4, 4, 4, 2, 3, 2, 3, 3, 3] 某些问题需要反向评分。 “Rscores”是需要反向评分的索引列表,这意味着对于这些分数,如果是1,则需要用4替换,如果是2,则需要用3替换 Rscores = [1, 6, 11, 16, 21, 28,

我有一份调查问卷的分数:

list= [1, 2, 2, 4, 1, 4, 1, 3, 2, 2, 2, 1, 1, 1, 4, 3, 4, 2, 1, 1, 1, 1, 1, 2, 2, 2, 4, 1, 4, 1, 2, 4, 4, 4, 4, 4, 2, 3, 2, 3, 3, 3]
某些问题需要反向评分。 “Rscores”是需要反向评分的索引列表,这意味着对于这些分数,如果是1,则需要用4替换,如果是2,则需要用3替换

Rscores = [1, 6, 11, 16, 21, 28, 33, 38, 43, 49, 57, 8, 46, 2, 7, 12, 17, 22, 25, 35, 40]
我已经尝试过这个,以及它的许多变体,但它不起作用:

for Rscores in list:
    if list[Rscores] == 1:
        list[Rscores] = 4
    elif list[Rscores] == 2:
        list[Rscores] = 3
    elif list[Rscores] == 3:
        list[Rscores] = 2
    elif list[Rscores] == 4:
        list[Rscores] = 1
如果有人能帮忙,我将非常感激。
提前感谢

这将创建一个新列表,并更正必要的分数

lst= [1, 2, 2, 4, 1, 4, 1, 3, 2, 2, 2, 1, 1, 1, 4,
      3, 4, 2, 1, 1, 1, 1, 1, 2, 2, 2, 4, 1, 4, 1,
      2, 4, 4, 4, 4, 4, 2, 3, 2, 3, 3, 3]

Rscores = [1, 6, 11, 16, 21, 28, 33, 38, 43, 49, 57,
           8, 46, 2, 7, 12, 17, 22, 25, 35, 40]

rectified_scores = [5-x if i in Rscores else x for i, x in enumerate(lst)]
enumerate
生成一个对序列(i,x),其中
i
是元素索引,
x
是元素索引的值<如果Rscores else x中的i是标准索引的分数,则code>5-x是
Rscores
列表中索引的分数的倒数

我重命名了您的列表以避免“隐藏”Python类型的名称。如果将
Rscores
设置为一组,您的代码可能会运行得稍微快一些,但它并不需要优化;他在工作

list= [1, 2, 2, 4, 1, 4, 1, 3, 2, 2, 2, 1, 1, 1, 4, 3, 4, 2, 1, 1, 1, 1, 1, 2, 2, 2, 4, 1, 4, 1, 2, 4, 4, 4, 4, 4, 2, 3, 2, 3, 3, 3]

for n, i in enumerate(list):
    if i == 1:
        list[n] = 4
    if i == 2:
        list[n] = 3
    if i == 3:
        list[n] = 2
    if i == 4:
        list[n] = 1
print(list)
希望能帮上大忙

L=[1,2,2,4,1,4,1,3,2,2,1,1,1,4,3,4,4,4,2,3,2,1,1,1,1,2,2,4,1,4,2,4,4,2,3,3]
Rscores=[1,6,11,16,21,28,33,38,43,49,57,8,46,2,7,12,17,22,25,35,40]
def反向存储(分数):
如果得分=1:
返回4
elif分数==2:
返回3
elif分数==3:
返回2
elif分数==4:
返回1
def rscoredList(左):
对于Rscores中的idx:
如果idx

我认为您列出的示例中的一个问题是,
Rscores
中的索引超出了列表的范围。(57被列为要反转的索引,但它不能是,因为
len(L)==42

对于列表中的rscore:
不,您需要
Rscores
中的idx。1.不要命名变量
列表
,例如将其更改为
L
<代码>列表
是一个内置功能。2.用Rscores中的
为idx更改for循环,然后用
[idx]
唱出所有的
[Rscores]
。注意,计算反向分数仅为5分。您可以在Rscores上做一个for循环,检查列表上的索引是否正确,并更改列表:Rscores中的i:if list[i]==1:etc。。。。在任何情况下,如果您使用示例运行它,它都会失败,因为您的列表len是42,而在Rscores中,您的值大于42,因此它将因“列表索引超出范围”而失败@Carlo1585:就是这样!如果list==1,则rscores中的for i为。。。。谢谢,不客气