Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

How to count the number of items between 2 list items

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.
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
Loop until you have position of x, same with y.

y - x = d

Pretty self explanatory.
(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 😖
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) 

:)
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)) 
(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. 🤝


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Fivepoint0Oct 23, 2018 05:24 PM 3 189 Oct 23, 2018 05:24 PM
Last Post: Fivepoint0
[Python] by Mucius ScaevolaAug 14, 2017 11:08 AM 2 182 Aug 14, 2017 11:08 AM
Last Post: Mucius Scaevola
[Python] by Ineed2learn2eatJul 13, 2017 10:48 AM 3 227 Jul 13, 2017 10:48 AM
Last Post: Ineed2learn2eat