Hack Forums
[Python] How to count the number of items between 2 list items - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] How to count the number of items between 2 list items (/showthread.php?tid=4978107)



How to count the number of items between 2 list items - Dyst0pia - Sep 6, 2015

Hi guys,

I have a list with 6 items in it. I need to output how many items there are between 2 of the list items the users asks for.

How can I do this?

So for example this is my code:

Code
bus_stops= ['New York', 'Los Angeles', 'New Jersey ', 'Boston', 'Miami', 'Mexico']

start_station = input("What is your start destination")
end_station = input("What is your end destination")

totalstations = #STUCK HERE

I need to calculate how many bus stops there are between the user submitted start and end stations in the bus_stop list.

I've read that this is possible with the numPY library but I have't found a way to do it yet.


* - cobaltscraper - Sep 6, 2015

Code
bus_stops= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']

start_station = input("What is your start destination")
end_station = input("What is your end destination")

start_pos = 0
end_pos = 0

for i in range(len(bus_stops)):
    if start_station == bus_stops[i]:
        start_pos = i + 1
        break

for i in range(len(bus_stops)):
    if end_station == bus_stops[i]:
        end_pos = i
        break

totalstations = end_pos - start_pos
print totalstations



* - c x y k - Sep 6, 2015

Loop until you have position of x, same with y.

y - x = d

Pretty self explanatory.


* - Dyst0pia - Sep 6, 2015

(Sep 6, 2015 02:09 PM)cobaltscraper Wrote:  
Code
bus_stops= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']

start_station = input("What is your start destination")
end_station = input("What is your end destination")

start_pos = 0
end_pos = 0

for i in range(len(bus_stops)):
    if start_station == bus_stops[i]:
        start_pos = i + 1
        break

for i in range(len(bus_stops)):
    if end_station == bus_stops[i]:
        end_pos = i
        break

totalstations = end_pos - start_pos
print totalstations

Thank you! that worked perfectly, I completely forgot about looping 😖


* - SIM-1866423 - Sep 6, 2015

PHP Code

<?php 
bus_stops
= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']
start_station = 'New York'
end_station = 'Boston'
print (bus_stops.index(end_station)-1) - bus_stops.index(start_station) 

:)


* - Introvert - Sep 7, 2015

Umm... There is no need for a loop here.
PHP Code

<?php 
bus_stops
= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']

start_station = input("What is your start destination")
end_station = input("What is your end destination")

total = max(bus_stops.index(start_station), bus_stops.index(end_station)) - min(bus_stops.index(start_station), bus_stops.index(end_station)) 



* - Dyst0pia - Sep 8, 2015

(Sep 7, 2015 09:46 AM)Introvert Wrote:  Umm... There is no need for a loop here.
PHP Code

<?php 
bus_stops
= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']

start_station = input("What is your start destination")
end_station = input("What is your end destination")

total = max(bus_stops.index(start_station), bus_stops.index(end_station)) - min(bus_stops.index(start_station), bus_stops.index(end_station)) 

Thanks for this, it does make it simpler. 🤝