首页 > 解决方案 > 试图比较两个列表或一个相同的列表

问题描述

import random
b=[]
o=[]
v=0
g=2
y=0
V=0
q=0
compat=0
alex=[]
zach=[]
while v != 5:
    name="name"
    position="position"
    answers=[]
    for i in range(10):
        answer=random.randint(1,4)
        answers.append(answer)
    b.append(name)
    b.append(position)
    b.append(answers)
    v+=1
print(b)
for ii in range(0,5):
    t=b[g]
    o.append(t)
    g+=3
    l=len(o)
for iii in list(o):
    C = o[y]
    y = y + 1
    alex = []
    for iiii in range(5):
        I = 0
        compat=0
        R=o[q]
        V=0
        Y = C[V]
        for iiiii in range(10):
            r=R[I]
            if r == Y:
                compat+=1
            else:
                compat=compat
            I+=1
            V += 1
            print(compat)
        alex.append(compat)
        print(alex)
    zach.append(alex)
    q+=1
w=len(zach)
print(zach)
print(w)

这是我的代码,它工作得很好。但它应该将每个值与其他值相对,但它不只是放置相同的 Y 值并且不会改变。我已经把 v+=1 放在很多不同的地方,并围绕着几个变量移动,但它没有改变。5 个人应该有 25 个不同的答案,但它不只是打印相同的数字 5 次然后重新启动。任何帮助将不胜感激编辑:这就是我将上面的代码集成到

global compatability
import sqlite3
with sqlite3.connect("Questionare.db") as db:
    cursor = db.cursor()
class mentee:           # these classes are made to create a place where atributes can be filled with variables
    def __init__(self,mentee_name,mentee_position):
        self._mentee_posisition=mentee_position
        self._mentee_name=mentee_name
    def menteereport(self):
        return{"mentee name:":self._mentee_name,"mentee position":self._mentee_posisition}
class mentor:
    def __init__(self,mentor_name,mentor_position):
        self._mentor_position=mentor_position
        self._mentor_name=mentor_name
    def mentorreport(self):        # these are methods and they use a function with the variables instanciated within the class to make this appen
        return {"mentor name":self._mentor_name,"mentor position":self._mentor_position}
class calculation:
    def __init__(self,number_of_questions,compatability,mentoranswers,menteeanwers):
        self._question_number= 1
        self._number_of_questions=number_of_questions
        self._compatability=compatability
        self._mentor_values=mentoranswers
        self._mentee_values=menteeanwers
    def calc(self):
        compat=0
        c=0
        for i in range(0,self._number_of_questions):
            if self._mentee_values[c] == self._mentor_answers[c]:
                compat += 1
                c+=1
            else:
                compat += 0
                c+=0
        compatability=compat/self._number_of_questions
        self._compatability=compatability
        compatability=compatability*100
        print(self._mentee_answers)
        print(self._mentor_answers)
        print("\n{:0.2f}%.\n".format(compatability))
    def mentoranswer(self):
        self._number_of_questions = int(self._number_of_questions)
        self._question_number=1
        for i in range(0,self._number_of_questions):
            answer=input("Q{}".format(self._question_number))
            self._question_number+=1
            self._mentor_answers.append(answer)
    def menteeanswer(self):
        self._number_of_questions = int(self._number_of_questions)
        self._question_number=1
        for i in range(0,self._number_of_questions):
            answer=input("Q{}".format(self._question_number))
            self._question_number+=1
            self._mentee_answers.append(answer)
class timerequirements:
    def __init__(self,mentor_time_allotment,mentee_time_allotment,ideal_length_of_mentoring,ideal_length_of_being_mentored):
        self._mentor_time_allotment=mentor_time_allotment
        self._mentee_time_allotment=mentee_time_allotment
        self._ideal_length_of_mentoring=ideal_length_of_mentoring
        self._ideal_length_of_being_mentored=ideal_length_of_being_mentored
def main(): # this function is created to put the variables into the artibutes so that everything will work.
    v = True
    mentoranswers = []
    menteeanswers = []
    no_of_q = int(input("numebr of questions"))
    while v == True:
        morm = input("are your a mentor or a mentee")
        if morm.lower() == "mentor":
            name = input("name")
            position = input("position")
            answers = []
            for i in range(0, no_of_q):
                answer = int(input("1 or 2"))
                answers.append(answer)
            mentoranswers.append(name)
            mentoranswers.append(position)
            mentoranswers.append(answers)
            print(mentoranswers)
        elif morm.lower() == "mentee":
            name = input("name")
            position = input("position")
            answers = []
            for i in range(0, no_of_q):
                answer = int(input("1 or 2"))
                answers.append(answer)
            menteeanswers.append(name)
            mentoranswers.append(position)
            menteeanswers.append(answers)
            print(menteeanswers)
        elif morm.lower() == "q":
            v = False
        else:
            print("try again")
    print(mentoranswers.mentorreport())
    print(menteeanswers.menteereport())
main()

标签: pythonarrayscompare

解决方案


你增加了太多的噪音。

for iii in list(o):
    C = o[y]
    y = y + 1

你为什么不简单地使用你刚刚创建的变量?它会自动递增

for iii in list(o):
    C = o[iii]

您应该学习如何使用循环和其他一些基础知识。python 官方网站上有很好的教程。这里是for 循环之一。您的大多数变量的存在只是为了重复您已经在做的事情。

我避免了一些高级构造,但我离开了 zip。

import random

mentees = []
names = ['zach', 'alex', 'fred', 'jane', 'sara']

for name in names:
    mentee = dict()
    mentee['name'] = name
    mentee['compatibility'] = []
    answers = []
    for i in range(10):
        answers.append(random.randint(1,4))
    mentee['answers'] = answers
    mentees.append(mentee)

for mentee1 in mentees:
    m1_answers = mentee1['answers']
    for mentee2 in mentees:
        m2_answers = mentee2['answers']
        compat = 0
        # zip fuses both lists into one
        for m1_answer, m2_answer in zip(m1_answers, m2_answers):
            if m1_answer == m2_answer:
                compat += 1
        mentee1['compatibility'].append((mentee2['name'], compat))

print(mentees)

推荐阅读