Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (3.4k points)

1 Answer

0 like 0 dislike
by (3.4k points)
 
Best answer

List slicing (selecting parts of lists)-

Section 1: Using the third "step" argument

lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

lst[::2]

# Output: ['a', 'c', 'e', 'g']

lst[::3]

# Output: ['a', 'd', 'g']

Section 2: Selecting a sublist from a list

lst = ['a', 'b', 'c', 'd', 'e']

lst[2:4]

# Output: ['c', 'd']

lst[2:]

# Output: ['c', 'd', 'e']

lst[:4]

# Output: ['a', 'b', 'c', 'd']

Section 3: Reversing a list with slicing

a = [1, 2, 3, 4, 5]

# steps through the list backwards (step=-1)

b = a[::-1]

# built-in list method to reverse 'a'

a.reverse()

if a = b:

print(True)

print(b)

# Output:

# True

# [5, 4, 3, 2, 1]

Section 4: Shifting a list using slicing

def shift_list(array, s):

"""Shifts the elements of a list to the left or right.

Args:

array - the list to shift

s - the amount to shift the list ('+': right-shift, '-': left-shift)

Returns:

shifted_array - the shifted list"""

# calculate actual shift amount (e.g., 11 --> 1 if length of the array is 5)

s %= len(array)

# reverse the shift direction to be more intuitive

s *= -1

# shift array with list slicing

shifted_array = array[s:] + array[:s]

return shifted_array

my_array = [1, 2, 3, 4, 5]

# negative numbers

shift_list(my_array, -7)

>>> [3, 4, 5, 1, 2]

# no shift on numbers equal to the size of the array

shift_list(my_array, 5)

>>> [1, 2, 3, 4, 5]

# works on positive numbers

shift_list(my_array, 3)

>>> [3, 4, 5, 1, 2]

Winter 10 Days boot-camp classes(7 HRS Daily) will start from 5, 20 & 27 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to chandwaglobal@gmail.com.

Goeduhub Important Lists Our Youtube Channels (For free E-learning)
About Us List of IITs Goeduhub Technologies
Contact Us List of NITs AI and Big Data-HADOOP E-Learning Series
  List of PSUs Smart Learning PLC-SCADA, IoT and Raspberry-PI
  List of Exams After Graduation  
...