By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,627 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,627 IT Pros & Developers. It's quick & easy.

How to get data between two values in a set?

P: 4
just a quick question here. i need to write a function written in python that receives a list of numbers and two values and returns the number of items between those two values. for example if the list is [1,2,3,4] and the two values are 2 and 4 then the function would return 3.
all i have so far is the very beginning
def main():
listdata = raw_input("please enter a string of numbers")
surroundnum = raw_input("enter two values in the list")
May 17 '11 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 621
A hint:
Expand|Select|Wrap|Line Numbers
  1. test_list = [1, 2, 4, 5, 7, 10]
  2. for location in range(len(test_list)):
  3.      print location, test_list[location] 
May 17 '11 #2

P: 4
im not really sure how that helps.... i mean i dont understand how that gets the number in between two given numbers in the list. all i get when i use that code is sets of numbers from the list divided into pairs
May 18 '11 #3

100+
P: 332
It is a hint that was given to you Jake :)

But I have a question for you. What do you mean by between. There are two scenarios.
1. Give me all numbers that are greater than 2 and smaller than 4 in the given list. Or
2. Give me all numbers that are physically located between numbers 2 and 4 in the given list. Example:
[6, 2, 5, 4] would return 5.

Given scenario 2, that would be why dwblas is refering to location in the list...
May 18 '11 #4

P: 4
i meant the physical location im just not sure how the hint applies. i mean i get the list,and the two numbers. i just dont know how to get the two to interact and then pull the number between them
May 18 '11 #5

P: 4
and i didnt mean to sound ungrateful any help i get at this point is greatly appreciated
May 18 '11 #6

100+
P: 332
Once you found the locations, you basically have a lower and upper limit. You should read about slicing a list as well as the index property of a list.
Some more hints:
Expand|Select|Wrap|Line Numbers
  1. test_list=[6, 2, 5, 4]
  2. x=test_list.index("2")
  3. y=test_list.index("4")
  4. z=test_list[x:y] #or probably x+1 and y-1...  may need some validation
  5.  
May 19 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.