简体   繁体   中英

How do I print a specific range of numbers in a list? Python

I've had a look at the similar questions asked on here but I can't seem to figure it out. I'm just starting out learning Python as one of my first programming languages and this is something I need to figure out how to do to move on with a task in my course.

data = [90, 30, 13, 67, 85, 87, 50, 45, 51, 72, 64, 69, 59, 17, 22, 23, 44, 25, 16, 67, 85, 87, 50, 45, 51, 72, 59, 14, 50, 55, 32, 23, 24, 25, 37, 28, 39, 30, 33, 35, 40, 34, 41, 43, 94, 95, 98, 99, 44, 45, 47, 48, 49, 53, 61, 63, 69, 75, 77, 60, 83]

Here is my list of data, I need to print the amount of entries that are between 10 and 20 in the list (there are four)

All I can seem to do is print the 10th to the 20th entry in the list with this print(data[10:20]) but this is not the information I need to extract.

Maybe something like:

print(len([num for num in data if num <= 20 and num >= 10]))

You would use list comprehension to create a list with all numbers of the data-list that are in between 10 and 20 (10 and 20 included).

Then you take the length of that list and print it.

This creates the list of all numbers between 10 and 20:

 my_list_of_numbers = [num for num in data if num <= 20 and num >= 10]

Edit : If you actually want to sum all the numbers that are between 10 and 20, you could do the following:

print(sum([num for num in data if num <= 20 and num >= 10]))

You can use sum here.

sum(1 for num in data if 10<=num<=20)
# 4

You can just do this

sum(10 <= num <= 20 for num in data) @Austin's suggested this in the comments

You can use list comprehension :

data=[90,30,13,67,85,87,50,45,51,72,64,69,59,17,22,23,44,25,16,67,85,87,50,45,51,72,59,14,50,55,32,23,24,25,37,28,39,30,33,35,40,34,41,43,94,95,98,99,44,45,47,48,49,53,61,63,69,75,77,60,83]
L = [x for x in data if 10 <= x <= 20]
print(L)
# [13, 17, 16, 14]
print(len(L))
# 4

So you want the amount of data:

data = [90,30,13,67,85,87,50,45,51,72,64,69,59,17,22,23,44,25,16,67,85,87,50,45,51,72,59,14,50,55,32,23,24,25,37,28,39,30,33,35,40,34,41,43,94,95,98,99,44,45,47,48,49,53,61,63,69,75,77,60,83]

datanew = (10 < x < 20 for x in data)

# print the amount of data
print(sum(datanew))

Output:

4
data = [90, 30, 13, 67, 85, 87, 50, 45, 51, 72, 64, 69, 59, 17, 22, 23, 44, 25, 16, 67, 85, 87, 50, 45, 51, 72, 59, 14, 50, 55, 32, 23, 24, 25, 37, 28, 39, 30, 33, 35, 40, 34, 41, 43, 94, 95, 98, 99, 44, 45, 47, 48, 49, 53, 61, 63, 69, 75, 77, 60, 83]

amount = 0

for x in range(9, 19):
    amount = amount + data[x]

print(amount)

maybe this answer can resolve your problem

A simple for loop with a if statement will work.

data=[90,30,13,67,85,87,50,45,51,72,64,69,59,17,22,23,44,25,16,67,85,87,50,45,51,72,59,14,50,55,32,23,24,25,37,28,39,30,33,35,40,34,41,43,94,95,98,99,44,45,47,48,49,53,61,63,69,75,77,60,83]
count = 0
for number in data:
    if number <= 20 and number >= 10:
        count += 1
print(count)

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