tours_data = []
for token in input().split():
tours_data.append(int(token))
slice_size = len(tours_data)//4
spring_tours = tours_data[-8:2]
summer_tours = tours_data[-6:4]
fall_tours = tours_data[-4:6]
winter_tours = tours_data[-2:]
print(f'Number of tour dates in each season: {slice_size}')
print(f'All year tours: {tours_data}')
print(f'Spring tours: {spring_tours}')
print(f'Summer tours: {summer_tours}')
print(f'Fall tours: {fall_tours}')
print(f'Winter tours: {winter_tours}')```
#🔒 List Slicing (Anybody know how to fix this issue????)
31 messages · Page 1 of 1 (latest)
@young bane
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
i think you're slicing backwards
negative slices refer to the end of the list
ie. -4 means go 4 back from wherever the end of the list is
would it work if I wer to just used numbers or number of elements that dont apply to other input/ other set of lists?
!slice
Sequence slicing
Slicing is a way of accessing a part of a sequence by specifying a start, stop, and step. As with normal indexing, negative numbers can be used to count backwards.
Examples
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:] # from element 2 to the end
['c', 'd', 'e', 'f', 'g']
>>> letters[:4] # up to element 4
['a', 'b', 'c', 'd']
>>> letters[3:5] # elements 3 and 4 -- the right bound is not included
['d', 'e']
>>> letters[2:-1:2] # Every other element between 2 and the last
['c', 'e']
>>> letters[::-1] # The whole list in reverse
['g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> words = "Hello world!"
>>> words[2:7] # Strings are also sequences
"llo w"
wdym?
my question was confusing lmao im trying to find a better way to ask it
The program I'm working with takes in an input so any input can be given
but if I sliced from index[0] would it work with all the inputs?
this prints an empty list because you're going from the 4th element from the end (ie. 7) to the 1st element from the start (ie. 1) ```py
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(L[-4 : 1])
you cant magically loop back around, that doesnt make sense
(memory wise)
!e
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(L[-4 : 1])```
!e py L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(L[-4 : 1])
@young bane :white_check_mark: Your 3.12 eval job has completed with return code 0.
[]
you wanna either count backwards by including a step in the slicing, or reverse the numbers
you’re hardcoding your solution to work on slice sizes of 2
you compute slice_size but then you don’t use it anywhere, and you do indexing like [-2:]
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.