简体   繁体   中英

Comparing list values (numbers, order matters) in Python

I tried looking for the answer for this everywhere, I'm sure it is really simple, but somehow I can't make it work.

I have two lists, which have exactly the same quantity of values, and I want to know how many of those match exactly value and position. This is the code I have been using:

ListA = [1, 2, 3, 4, 5]

solved = 0

While solved == 0:
   rr = 0
   i = 0

   userinput = input("Enter a 5 digit number):")
   ListB = []

   for u in userinput:
        ListB.append(u)

   While i < 5:
        if ListA[i] == ListB[i]:
            rr = rr + 1
   print(rr)
   solved = 1

The output I'm looking for is, for example, if the input is 12763, 2, but it never seems to evaluate the if as true, so it never adds 1 to rr . I tried a for loop and variations of this setup, but I can't see where I am wrong.

Thanks a lot for your time!

ListA contains integers, while ListB will contain (single character) strings. Try using int(ListB[i]) instead.

Moreover, the inner while loop is infinite since i never changes.

Seems to me like you're overcomplicating things. A couple of list comprehensions should do the trick:

ListB = [int(x) for x in userinput]
rr = sum([ListA[i] == ListB[i] for i in range(len(ListA))])

You have several problems:

  1. While should be while
  2. The input you receive from the user is a string, so when you iterate you should convert it int because you wants to compare integers.
  3. Your i variable is never increasing, so you are in an infinite loop.

Try this:

ListA = [1, 2, 3, 4, 5]

solved = 0

while solved == 0:
   rr = 0
   i = 0

   userinput = input("Enter a 5 digit number):")
   ListB = []

   for u in userinput:
        ListB.append(int(u))

   while i < 5:
        if ListA[i] == ListB[i]:
            rr = rr + 1
        i += 1
   print(rr)
   solved = 1

The input() function returns a string. You need to covert the individual digits to int s before comparing them. You can also use zip() to compare corresponding items from two lists:

>>> listA = [1, 2, 3, 4, 5]
>>> userinput = input("Enter a 5 digit number: ")
Enter a 5 digit number: 12567
>>> listB = [int(x) for x in userinput]
>>> sum(x == y for x, y in zip(listA, listB))
2

I think a solution could be this :

First while not While also you never increment i into second while and last how you can get copy into list input? You use space or how? How did you know when you have 5 number? I supose they are divided by space so ?

ListA = [1, 2, 3, 4, 5]

solved = 0

while solved == 0:
  rr = 0
  i = 0

  userinput = input("Enter a 5 digit number: ")
  ListB = [int(x) for x in userinput.split(' ') if x.isdigit()]

  while i < 5:
    if ListA[i] == ListB[i]:
      rr += 1
    i += 1
  print(rr)
  solved = 1

If intput is 1 2 3 4 5 so output 5

UPDATE : If input is 5 digits you havent to split the space so look for the other user solutions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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