So I've been tring to use .index
with 2D arrays just so I can do some tests on my code, but it just comes up with an error saying that the value which is in the list actually isn't.
For example, from one project (where i was trying to revise network layering whilst practicing some coding), I tried doing this but didnt work:
answers = [['Application Layer','HTTP','HTTPS','SMTP','IMAP','FTP'],['Transport Layer','TCP','UDP'],['Network Layer','ARP','IP','ICMP'],['Data Link layer']]
correct = 0
incorrect = 0
qs = answers[randint(0,3)][0]
print(answers.index(qs))
print(qs)
As you can see, I'm trying to get back the value of 'qs' by using index but no luck.
I've seen a few other posts saying to use numpy, but how would I do this without using numpy?
CodePudding user response:
You can do it like this.
answers = [['Application Layer','HTTP','HTTPS','SMTP','IMAP','FTP'],['Transport Layer','TCP','UDP'],['Network Layer','ARP','IP','ICMP'],['Data Link layer']]
correct = 0
incorrect = 0
qs = answers[randint(0,3)][0]
for i, answer in enumerate(answers):
if qs in answer:
print(i)
break
print(qs)