Creating a list: - Tom Kleen



ListsList: an ordered collection of data items. The items can be of any type, but a list's elements are usually of the same type (e.g. all numbers or all strings).List elements are enclosed in [].Do all of today's work in the interactive Python shell.Creating a list:>>> list1 = [1, 2, 3, 4] #4 integers#Elements can be of any type (name and age)>>> list2 = ["Fred", 30] #Elements can be other lists (e.g. name, children)>>> list3 = ["Fred", ["Jacki", "Angela"]]Adding to a list:You can add an element to a list by using the concatenation operator. Note that the concatenation operator requires two lists.>>> list4 = list1 + list2If you want to add a single element to a list, before you can concatenate, you must make the element into a list:>>> list4 = list4 + [5]Lists have a length:>>> len(list1)4>>> len(list2)2>>> len(list3)2>>> len(list3[1])2List elements can be accessed:To access an element, use the same notation (square brackets) that is used by strings.>>> print(list1[0])>>> for i in range(len(list1)):... print(list1[i])You can also iterate over the list elements the same way you could step through a string.>>> for i in list1:... print(i)The items are also numbered the same way as strings. This will print the items in reverse order.>>> for i in range(-1,-len(list1)-1, -1):... print(list1[i])>>> list3[1]['Jacki', 'Angela']>>> list3[1][0]'Jacki'>>> list3[1][0][0:3]'Jac'>>>The in and not in operators work the same:>>> 1 in list1True>>> 5 in list1False>>> "Fred" in list2True>>> "fred" in list2False>>> "Fred" not in list2False>>> "Jacki" in list3False>>> "Jacki" in list3[1]TrueThe + and * operators work the same as with strings:>>> list1 + list2[1, 2, 3, 4, 'Fred', 30]>>> list1*3[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]Lists can be sliced, just like strings:>>> list1[1]2>>> list1[0,2]>>> list1[0:2][1, 2]>>> list1[:2][1, 2]>>> list[2:4][]>>> list1[2:4][3, 4]>>> list1[2:][3, 4]A slice always returns a list:>>> list3[1][1:]['Angela']But just providing an individual subscript returns the element (no brackets).>>> list3[1][1]'Angela'Start with the list of the previous exercise: >>> list1 = [1, 2, 3, 4] #4 integersThen write Python statements to do the following:Append "apple" and 76 to the list as two distinct elements. Result should be [1, 2, 3, 4, 'apple', 76]Append "apple" and 76 to the list as a list. Result should be[1, 2, 3, 4, ['apple', 76]]Insert the value "cat" at position 3 (counting the way computers count, not the way people count). To do this, slice the list into two parts and then put it back together again. Do NOT replace the current item at position 3 (the number 4).Insert the value 99 at the start of the list. Again, do not replace the 1 that is currently at the start of the list.Find the index of "cat". You will need to write a for loop.List AlgorithmsWrite the code (in main) to create this list:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Write code to print the size of the list.Then write the following functions:print_list(a)Print the list, one element per line. Nothing is returned.reverse(a)Return a new list that has the elements of a reversed. Nothing is printed.count_odd(a)Count and return the number of odd elements in the list. You may assume that all elements are integers. Nothing is printed.count_even(a)Count and return the number of even elements in the list. Nothing is printed.sum_odd(a)Return sum of the odd numbers in a list. Nothing is printed.sum_even(a)Return the sum of the even numbers in a list. Nothing is printed.get_odd(a)Return a new list (don't change the original list) that contains only the odd elements from the original list. Nothing is printed.count_frequency(a, n)Return the number of times a specific element occurs in a list. Nothing is printed. For example, to see how many times the number 85 occurs in a list called aList, you would call it like this: count = count_frequency(aList, 85)sum_between(a, low, high)Return the sum of the numbers in a list between two numbers that are passed in. Nothing is printed. For example to sum the numbers in a list called aList between 5 and 10 (including the 5 and the 10), you would call it like this: sum = sum_between(aList, 5, 10)sum_squares(a)Return the sum of the squares of the numbers in the list that is passed to it. Nothing is printed. For example, sum_squares([2,?3,?4]) should return 4+9+16 which is 29.get_unique_elements(a)Returns a copy of a with duplicates removed. Does not modify a. Nothing is printed. Create a new empty list. Step through the original list one element at a time. See if the element is in the new list. If not, add it. If so, do nothing.intersection(a, b)Returns a new list that is the common elements of a and b. Does not modify a or b. Nothing is printed. Create a new empty list. Step through list a one element at a time. See if the element is also in list b. If so, add it to the new list. If not, do nothing.list_diff(a, b)Returns a new list that is all of the elements in a that are not in b. Does not modify a or b. Nothing is printed. Create a new empty list. Step through list a one element at a time. See if the element is also in list b. If so, do nothing. If not, add it to the new list.longest_word_length(aList) Write a Python function that takes a list of words and returns the length of the longest one. It does NOT print anything. Get the first element of aList. Copy its length to a longest_length variable. This is the longest word (because it's the only word) that you have seen so far. Step through aList one element at a time. Compare the length of the current element with the length of the longest word that you have seen so far. If it is longer, then replace longest_length with the length of this word. aList = ["Four", "score", "and", "seven", "years", "ago", "our", "fathers"]print(longest_word_length(aList))The above should print 7.longest_word(aList) Write a Python function that takes a list of words and returns the longest one. It does NOT print anything. This algorithm is identical to the one above, except that you need to keep track of two things: (1) the length of the longest word you have seen so far, and (2) the longest word you have seen so far.aList = ["Four", "score", "and", "seven", "years", "ago", "our", "fathers"]print(longest_word(aList))The above should print fathers.If you use the following list:[654, 58, 127, 79, 227, 254, 778, 494, 308, 368, 792, 696, 690, 713, 805, 940, 224, 402, 41, 995, 858, 627, 814, 458, 299, 417, 649, 446, 618, 500, 898, 877, 760, 741, 3, 818, 621, 296, 333, 578, 520, 593, 78, 314, 764, 585, 136, 716, 647, 455, 454, 314, 728, 101, 179, 549, 956, 798, 442, 795, 812, 491, 147, 931, 891, 429, 735, 433, 635, 494, 938, 679, 133, 841, 781, 471, 868, 242, 566, 44, 668, 388, 737, 646, 568, 538, 150, 328, 160, 479, 27, 528, 107, 709, 875, 108, 911, 516, 709, 358]Count of odd numbers: 45Count of even numbers: 55Sum of even: 28,842Sum of odd: 23,609 ................
................

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

Google Online Preview   Download