简体   繁体   中英

How to call a value at a specific index of a list in python?

I am trying to call a value at a specific index from a list. For an example index, at index[2]. I can't seem how to figure how to do this. I can only get the value at the last index.

# number of arrivals per second
_lambda = 0.00923579

# number of AC arriving
_num_events = 10

_event_num = []
_inter_event_times = []
_event_times = []
_event_time = 0

for i in range(_num_events):
    _event_num.append(i)
    n = random.random()

    _inter_event_time = -math.log(1.0 - n)/_lambda # gap between successive AC in seconds
    _inter_event_times.append(_inter_event_time) # list of gaps between successive AC in seconds

    _event_time = _event_time + _inter_event_time # arrival time of AC
    _event_times.append(_event_time) # list of arrival times of AC

    print(str(i) +',' + str(_inter_event_time) + ',' + str(_event_time))

Please provide some more specific information on what variable in your example you want to retrieve this value. If you want to do it in loop in your last line just use:

print(str(i) +',' + str(_inter_event_times[i]) + ',' + str(_event_times[i]))

You can allways use this syntax: array[index]

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