简体   繁体   English

如何将不同的变量归因于列表的每个元素

[英]How to attribut a different variable to each element of list

Watch out I am a beginner. 当心我是一个初学者。

My question is: can I ask Python to attribut a different variable to each element of a list. 我的问题是:我可以要求Python为列表的每个元素分配不同的变量。

I created one list of questions and one list of answer 我创建了一个问题列表和一个答案列表

lst_questions = ['6+11', '3+3', '9+10', '2+7', '11+8', '9+3', '11+9', '3+3', '2+4', '7+4', '4+8', '3+9']
lst_reponses = [17, 6, 19, 9, 19, 12, 20, 6, 6, 11, 12, 12]
var1 = 0
var2 = 0
var3 = 0
var4 = 0
var5 = 0
var6 = 0
var7 = 0
var8 = 0
var9 = 0
var10 = 0
var11 = 0
var12 = 0

a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0
k = 0
l = 0

And I want to have: 我想拥有:

def apply_solution(lst):

    a = lst.index(var1)
    b = lst.index(var2)
    c = lst.index(var3)
    d = lst.index(var4)
    e = lst.index(var5)
    f = lst.index(var6)
    g = lst.index(var7)
    h = lst.index(var8)
    i = lst.index(var9)
    j = lst.index(var10)
    k = lst.index(var11)
    l = lst.index(var12)

    #Solution
    lst[a], lst[b], lst[c], lst[d], lst[e], lst[f], lst[g], lst[h], lst[i], lst[j], lst[k], lst[l] = lst[a], lst[k], lst[j], lst[i], lst[h], lst[g], lst[f], lst[e], lst[d], lst[c], lst[b], lst[l]

    #Verification indice
    #print a,b,c


    return lst


print apply_solution(lst_reponses)

Obviously, I receive error: 显然,我收到错误:

a = lst.index(var1)
ValueError: 0 is not in list

How can I attribute each element of lst_reponses to var1 ... var12 I hope I make myself clear. 我如何将lst_reponses的每个元素归因于var1 ... var12,我希望我自己弄清楚。

Use a dict if you want to associate values: 如果要关联值,请使用dict:

lst_questions = ['6+11', '3+3', '9+10', '2+7', '11+8', '9+3', '11+9', '3+3', '2+4', '7+4', '4+8', '3+9']
lst_reponses = [17, 6, 19, 9, 19, 12, 20, 6, 6, 11, 12, 12]
data  = dict(zip(lst_questions,lst_reponses))

{'2+4': 6, '2+7': 9, '3+3': 6, '9+10': 19, '6+11': 17, '3+9': 12, '9+3': 12, '11+9': 20, '11+8': 19, '7+4': 11, '4+8': 12}

To ask a user question and verify: 向用户提问并验证:

from random import choice
# "3+2" etc.. keys and correct result as value
data = dict(zip(lst_questions, lst_reponses))
# get random key
question = choice(data.keys())

inp = raw_input("What is {}".format(question))

# is user answer match the value 
if int(inp) == data[question]:
    print("Well done")
else:
    print("Incorrect")
lst_reponses = [17, 6, 19, 9, 19, 12, 20, 6, 6, 11, 12, 12]
from collections import defaultdict
dct = defaultdict(list)
for i in xrange(len(lst_reponses)):
    dct["var{}".format(i+1)] = lst_reponses[i]

print dct

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

相关问题 如何将列表的每个元素分配给一个单独的变量? - How to assign each element of a list to a separate variable? 如何将数组的每个元素分配给 python 中的不同变量 - how to assign each element of array to different variable in python 如何为numpy数组变量中的每一行制作不同的元素? - How to make different element for each row in numpy array variable? 如何按每个元素的不同顺序对元组列表进行排序? - How to sort a list of tuples by different order for each element? 如何将可变长度列表的每个元素打印为 python 中的一列? - How to print each element of a list of lists of variable length as a column in python? 如何检查列表中的每个元素是否都相同(设置)变量 - How to check if each element in a list is the same (set) variable Python,为列表的每个元素添加不同的特征 - Python, adding a different characteristic to each element of a list 如何逐个元素地遍历嵌套列表元素以每次创建不同的字符串 - how to iterate through a nested list element by element to create a different string each time 如何将我的 2d 列表中每个列表的每 2 个元素乘以一个变量 - How to multiply every 2 element of each list in my 2d list by a variable 如何比较列表中的每个元素与另一个列表中的每个元素? - how to compare each element in a list with each element in another list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM