简体   繁体   English

如何比较Python中不同功能的两个列表?

[英]how to compare two lists in different functions in Python?

I am making a Django web Application and i am facing a problem in comparing two different lists in different functions我正在制作 Django web 应用程序,在比较不同功能的两个不同列表时遇到问题

def test(request,slug):
    n=request.user
    choice=TestOptions.objects.filter(choice=slug).first()
    que=questions.objects.filter(Subject=choice)  
    question=[]
 
    un=['a','b','c','d','e']
    
    for q in que:
        if q not in question:
            question.append(q)
        else:
            continue
    sampling = random.sample(question, 5)
    print("--------------SamPLING111")
    print(sampling)
    print("--------------SamPLING111")
    correctAnswers=[]
    for j in sampling:
          
            correctAnswers.append(j.answer)

    marks(correctAnswers)
    d = dict(zip(un,sampling))
    return render(request,"code/test.html",{'questions':d})

def acceptAnswer(request):
    answers=[]
    if request.method=="POST":
        answers.append(request.POST['a'])
        answers.append(request.POST['b'])
        answers.append(request.POST['c'])
        answers.append(request.POST['d'])
        answers.append(request.POST['e'])

        score(answers)
    return render(request,"code/dub.html")

def marks(correct):
  list1=[]
  l1=correct

def score(and):
  list2=[]
  l2=ans

function test is passing a list and function acceptAnswer is passing another list my job is to compare those two lists function 测试通过了一个列表,而 function acceptAnswer 正在通过另一个列表 我的工作是比较这两个列表

how can I compare l1 and l2?我如何比较l1和l2?

I am not 100 percent what you are trying to do with these lists, but in order to compare them I would just return them.我不是 100% 你想用这些列表做什么,但为了比较它们,我只会返回它们。 Here is a quick example:这是一个简单的例子:

def marks(correct):
    list1 = []
    l1 = correct
    return l1

def score(answer):
    list2 = []
    l2 = answer
    return l2
    
numbers = [1,2,3]
numbers2 = [1,2,3]
numbers3 = [3,4,5]

print(marks(numbers) == score(numbers2)) # True
print(marks(numbers2) == score(numbers3)) # False

Hopefully this helps!希望这会有所帮助!

Rather than continue with comments I figured I'd elaborate in an answer though it isn't an exact answer to your question I think it is the real answer.与其继续发表评论,我想我会在答案中详细说明,尽管这不是您问题的确切答案,但我认为这是真正的答案。

You really have two issues.你真的有两个问题。 One is a design issue ie how to make your program work correctly and the other is an implementation issue about the scope of variables and how to deal with it.一个是设计问题,即如何使您的程序正常工作,另一个是关于变量 scope 以及如何处理它的实现问题。

I can see in your profile you're a university student and given the nature of the code it seems very likely you're writing your web app for the purposes of learning maybe even an assignment.我可以在您的个人资料中看到您是一名大学生,并且鉴于代码的性质,您似乎很可能正在编写 web 应用程序以用于学习甚至是作业。

If you were doing this outside of a university I'd expect you were seeking practitioner type skills in which case I'd suggest the answer would be to design your application the way Django expects you to, which I would say would translate into storing state information in a database.如果你在大学之外做这件事,我希望你正在寻找从业者类型的技能,在这种情况下,我建议答案是按照 Django 期望你的方式设计你的应用程序,我会说这会转化为存储 state数据库中的信息。

If this is a lab however you may not have covered databases yet.但是,如果这是一个实验室,您可能还没有涵盖数据库。 Labs sometimes have students doing silly things because they can't teach everything at the same time.实验室有时会让学生做一些愚蠢的事情,因为他们不能同时教所有东西。 So your Prof may not expect you to use a database yet.所以你的教授可能还不希望你使用数据库。

Inside a web application you have to consider that the web is request response and that you can get requests from a lot of different sources so you have state management concerns that classical desktop applications don't have.在 web 应用程序中,您必须考虑 web 是请求响应,并且您可以从许多不同的来源获取请求,因此您有 state 管理担心经典桌面应用程序没有。 Who is supposed to see these tests and who is supposed to see the marks and what is the order these things happen?谁应该看到这些测试,谁应该看到标记,这些事情发生的顺序是什么? Should anyone be able to create a test?任何人都应该能够创建测试吗? Should anyone be able to take a test?任何人都应该能够参加考试吗? You might not care yet, eventually you'll want to care about sessions.您可能还不关心,最终您会想要关心会话。 If people are taking their own tests you could store data in a user session but then other people wouldn't see those tests.如果人们进行自己的测试,您可以将数据存储在用户 session 中,但其他人不会看到这些测试。 Generally the correct way to store this sort of state is in a database where you can access it according to what you know about the current request.通常,存储这种 state 的正确方法是在数据库中,您可以根据您对当前请求的了解来访问它。 If this is some sort of basic intro app your Prof may be happy with you doing something kludgy for now.如果这是某种基本的介绍应用程序,您的教授可能会对您现在做一些笨拙的事情感到满意。

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

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