“Liste de la tranche en python” Réponses codées

Python tranche un tableau

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array
Example:
>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
>>> a[1:4]
[2, 3, 4]
Dentedghost

Python List Specing

thislist = ["1.apple", "2.banana", "3.cherry", "4.orange", "5.kiwi", "6.melon", "7.mango"]

# thislist[starting index(including) : end index(not including):step]

print(thislist[:]) #Output: ['1.apple', '2.banana', '3.cherry', '4.orange', '5.kiwi', '6.melon', '7.mango']
print(thislist[1:]) #Output: ['2.banana', '3.cherry', '4.orange', '5.kiwi', '6.melon', '7.mango']
print(thislist[:5]) #Output: ['1.apple', '2.banana', '3.cherry', '4.orange', '5.kiwi']
print(thislist[2:5]) #Output: ['3.cherry', '4.orange', '5.kiwi']
print(thislist[::3]) #Output: ['1.apple', '4.orange', '7.mango']
print(thislist[1::2]) #Output: ['2.banana', '4.orange', '6.melon']
print(thislist[1:6:3]) #Output: ['2.banana', '5.kiwi']
#1

tranchant dans la liste Python

list_example = ["python","ruby","java","javascript","c#","css","html"]
print(list_example[3])#javascript
print(list_example[0])#python
print(list_example[6])#html
print(list_example[0:3]) #will print the programming language under python and javascript
print(list_example[:3]) all languages under python to javascript
Programmer of empires

Liste de la tranche en python

# List slicing in Python

my_list = ['p','r','o','g','r','a','m','i','z']

# elements from index 2 to index 4
print(my_list[2:5])

# elements from index 5 to end
print(my_list[5:])

# elements beginning to end
print(my_list[:])

#['o', 'g', 'r']
#['a', 'm', 'i', 'z']
#['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']
David Cao

Comment trancher la liste

# Python program to demonstrate
# Removal of elements in a List
 
# Creating a List
List = ['S','O','F','T','H','U',
        'N','T','.','N','E','T']
print("Initial List: ")
print(List)
 
# Print elements of a range
# using Slice operation
Sliced_List = List[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_List)
 
# Print elements from a
# pre-defined point to end
Sliced_List = List[5:]
print("\nElements sliced from 5th "
      "element till the end: ")
print(Sliced_List)
 
# Printing elements from
# beginning till end
Sliced_List = List[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_List)
Outrageous Ostrich

Réponses similaires à “Liste de la tranche en python”

Questions similaires à “Liste de la tranche en python”

Plus de réponses similaires à “Liste de la tranche en python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code