Skalenthome.files.wordpress.com



List in List and list comprehension Two-Dimensional List2D list is a nested list, list in listExampleList_in_list=[[1,2,3,4],[1,2,3,4,6]]Print(List_in_list)How To access 2D listPrint(List_in_list[0][1]) first square bracket indicates the row (inside list) and 2nd square bracket indicates the index of elementSum in nested lista = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]s = 0for i in range(len(a)): for j in range(len(a[i])): s += a[i][j]print(s)How To access using For loopa = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]for i in range(len(a)): for j in range(len(a[i])): print(a[i][j], end=' ') print()Creating nested listn = 3m = 4a = [[0] * m] * na[0][0] = 5print(a[1][0])How To take input R = int(input("Enter the number of rows:")) C = int(input("Enter the number of columns:")) # Initialize matrix matrix = [] print("Enter the entries rowwise:") # For user input for i in range(R): # A for loop for row entries a =[] for j in range(C): # A for loop for column entries a.append(int(input())) matrix.append(a) # For printing the matrix for i in range(R): for j in range(C): print(matrix[i][j], end = " ") print()List Comprehensionlist comprehension, the special syntax used by Python in order to fill massive lists.For exampleQ = [x**3 for x in range(11)]isQ = {x3x3: x in {0 ... 10}}List Comprehension and For loop differenceFor LoopsAs you might already know, you use for loops to repeat a block of code a fixed number of times. List comprehensions are actually good alternatives to for loops, as they are more compact. Consider the following example that starts with the variable numbers, defined as a range from 0 up until 10 (not included).Using For loop# Initialize `new_list`new_list = []# Add values to `new_list`for n in numbers: if n%2==0: new_list.append(n**2)# Print `new_list`print(new_list)[0, 4, 16, 36, 64]Using List ComprehensionThis is all nice and well, but now consider the following example of a list comprehension, where you basically do the same with a more compact notation:# Create `new_list` new_list = [n**2 for n in numbers if n%2==0]# Print `new_list`print(new_list)[0, 4, 16, 36, 64]List Comprehensions with ConditionalsUsing Conditional statements# Initialize and empty list `uneven` uneven = []# Add values to `uneven` for x in feet: if x % 2 == 0: x = x / 2 uneven.append(x)# Print `uneven` print(uneven)[64304.0, 59875.0]Using List Comprehension# Define `uneven`uneven = [x/2 for x in feet if x%2==0]# Print `uneven` print(uneven)[64304.0, 59875.0]Multiple If Conditionsdivided = []for x in range(100): if x%2 == 0 : if x%6 == 0: divided.append(x)Using List comprehensiondivided = [x for x in range(100) if x % 2 == 0 if x % 6 == 0]print(divided)[0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96]If-Else Conditions vs list comprehensionTake a look at the following example of such a more complex conditional in a for loop:Using if elsefor x in feet: if x >= 120000: x + 1 else: x+5List comprehension[x+1 if x >= 120000 else x+5 for x in feet][128609, 119755, 122376, 124016]Some Examples on list# calculating sum of elements in a listmyList = [] # creating an empty listfor i in range(5): myList.insert(0, i + 1)print(myList)myList = [10, 1, 8, 3, 5]total = 0for i in range(len(myList)): total += myList[i]#Finding largest number in listmyList = [10, 1, 8, 3, 5]length = len(myList)for i in range(length // 2): myList[i], myList[length - i - 1] = myList[length - i - 1], myList[i]print(myList)myList = [17, 3, 11, 5, 1, 9, 7, 15, 13]largest = myList[0]for i in range(1, len(myList)): if myList[i] > largest: largest = myList[i]print(largest)Now let's find the location of a given element inside a list:myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]toFind = 5found = Falsefor i in range(len(myList)): found = myList[i] == toFind if found: breakif found: print("Element found at index", i)else: print("absent")Note:the target value is stored in the toFind variable;the current status of the search is stored in the found variable (True/False)when found becomes True, the for loop is exited.ExampleThe question is: how many numbers have you hit?The program will give you the answer:drawn = [5, 11, 9, 42, 3, 49]bets = [3, 7, 11, 42, 34, 49]hits = 0for number in bets: if number in drawn: hits += 1print(hits)ExamplemyList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]newList = []for number in myList: # Browse all numbers from the source list.if number not in newList: # If the number doesn't appear within the new list...newList.append(number) # ...append it here.myList = newList[:] # Make a copy of newlist.print("The list with unique elements only:")print(myList) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download