简体   繁体   中英

Compare two lists from user input, item by item, and return the larger value between the lists

I have no prior knowledge of python or programming and learning as I go. Below is what I am working with and I'm just unsure how to go about this.

Write a function that takes two lists of floats as input. The lists must be the same length, and you can rely on users always properly giving lists of identical length. The function should determine the number of items in the first list that are greater than the corresponding item in the second list (ie is the item at position 5 in the first list bigger than the item at position 5 in the second list). The number of values in the first list that are bigger should be returned as an integer (so if no items in the first list are bigger than the corresponding items in the second list return 0, if 5 items are bigger than their corresponding item in the second list, return 5).

list_1 = []
list_2 = []

# asking number of elements to put in list_1
num_1 = int(input("Enter number of elements in list_1: "))

# iterating till num to append elements in list
for i in range(1, num_1 + 1):
    ele = int(input("Enter elements: "))
    list_1.append(ele)
  
# print maximum element
print("Largest element is:", max(list_1))

# asking number of elements to put in list_2
num_2 = int(input("Enter number of elements in list_2: "))

# iterating till num to append elements in list
for i in range(1, num_2 + 1):
    ele = int(input("Enter elements: "))
    list_2.append(ele)
  
# print maximum element
print("Largest element is:", max(list_2))

I figure this is completely wrong as I am suppose to compare each item by item in the two lists. Any tips? Thank you!

Your code correctly gets the user input, so I will talk about the task after that.

(But you could write for i in range(1, num_1 + 1): instead of for i in range(num_1): . In a python idiom, people would even write for _ in range(num_1): .)

The idea is to make a variable counter that is initially 0 . Looping over the two lists simultaneously, we increment the counter when the condition (an element in list_1 is greater than the corresponding element in list_2 ).

The most basic code that implement this would be the following:

list_1 = [1,3,5,7]
list_2 = [6,4,2,0]

count = 0
for i in range(len(list_1)):
    if list_1[i] > list_2[i]:
        count += 1 # add one

print(count) # 2

This might be what the assignment is looking for. But people usually dislike to see the pattern for... in range(len(...)): . A slightly more pythonic way is to use zip :

count = 0
for x, y in zip(list_1, list_2):
    if x > y:
        count += 1

Better yet, in my opinion, is to use list (or generator) comprehension instead of the explicit for :

count = sum(x > y for x, y in zip(list_1, list_2))

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