Manipulating Arrays.

Slides:



Advertisements
Similar presentations
Managerial Decision Making and Problem Solving Computer Lab Notes 1.
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Slides 2c: Using Spreadsheets for Modeling - Excel Concepts (Updated 1/19/2005) There are several reasons for the popularity of spreadsheets: –Data are.
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
Click on File - > Open. Click on Database Click on “Open”
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
C++ for Engineers and Scientists Third Edition
Mary K. Olson PS Reporting Instance – Query Tool 101.
Access 2007 ® Use Databases How can Access help you to find and use information?
MICROSOFT EXCEL Form 4 Spreadsheets Revision Instructions: Go through slides and complete all exercises. Up to exercise 5. Save it on the computer or on.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 3 Single-Table Queries
Lists in Python.
How could the database be most efficiently searched to find all of the inventions of Samuel Morse? A. Inventor = "Morse" C. Invention = "telegraph" B.
Question 10 What do I write?. Spreadsheet Make sure that you have got a printout of your spreadsheet - no spreadsheet, no marks!
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
1 / 23 © 2006, Universal Tax Systems, Inc. All Rights Reserved. Working with Tax Returns Objectives –In this chapter you will learn how to: Get Started.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
EXCEL Intro to Microsoft Excel. Objectives for the Week Content ObjectivesLanguage Objectives I can create and manipulate charts, graphs, and reports.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
September 26, 2011 Sorting and Searching Lists. Agenda Review quiz #3 Team assignment #1 due tonight  One per team Arcade game Searching  Linear  Binary.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Working with Data Lists.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Paul A. Harris, Ph.D. Director, GCRC Informatics How to use query function in Microsoft Access.
Lists in Python List methods and functions. List methods MethodSemantics list.append(x)Adds x to the right end of list, changes original list list.sort()Sorts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Spreadsheet I n Concepts & operations. Concepts n Workbook: Excel file n Worksheet: sheet n Row: 1-???? n Column: A - Z, AA - ?? n Cell n Cell address.
MSAA PRESENTS: AN EXCEL TUTORIAL
Chapter 6 Modifying Cell Styles
Databases.
Algorithmic complexity: Speed of algorithms
Chapter 5 Ordered List.
Microsoft Visual Basic 2005: Reloaded Second Edition
Interfacing with a database
Hashing CSE 2011 Winter July 2018.
Statistics and Spreadsheet Functions
Chapter 7 Arrays.
Sorting Cases and Variables
BASIC PROGRAMMING FOR DATA ANALYSIS
GO! with Microsoft® Access e
Arrays An Array is an ordered collection of variables
Chapter 5 Ordered List.
Describing algorithms in pseudo code
Python I/O.
CNG 140 C Programming (Lecture set 8)
searching Concept: Linear search Binary search
Fundamentals of Data Structures
Algorithmic complexity: Speed of algorithms
Data Structures (CS212D) Week # 2: Arrays.
Topics Sequences Introduction to Lists List Slicing
Arrays Week 2.
Spreadsheets, Modelling & Databases
Arrays Part 2.
Python Lists.
Topics Sequences Lists Copying Lists Processing Lists
Algorithmic complexity: Speed of algorithms
10 JavaScript: Arrays.
Topics Sequences Introduction to Lists List Slicing
2d Arrays.
Chapter 6 Modifying Cell Styles
Lec 21 More Fun with Arrays: For Loops
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Introduction to Computer Science
Presentation transcript:

Manipulating Arrays

Sorting all data ascending students = [["dave", 14, "11DA"], ["liz", 18, "7ED"], ["kerry", 7, "8MD2"]] print (students) print ("") ## sort the list ### students.sort() print(students) Uses the name of the list and .sort() Sorts the lists alphabetically by the first item in each list, the name

Sorting data by choice, by age students = [["dave", 14, "11DA"], ["liz", 18, "7ED"], ["kerry", 7, "8MD2"]] print (students) ## sort the list by a column age### def getKey(item): return item[1] print(sorted(students, key=getKey)) Create a function or a key to store the item position number, age in in positon 1 Uses return item[1] to sort by position one, the ages

Sorting all data by choice, ascending or descending students = [["dave", 14, "11DA"], ["liz", 18, "7ED"], ["kerry", 7, "8MD2"]] print (students) print ("") print(sorted(students, reverse=True)) This list is sorted in reverse by the name data

Sorting all data by single column data, ascending or descending students = [["dave", 14, "11DA"], ["liz", 18, "7ED"], ["kerry", 7, "8MD2"]] print (students) ## sort the list by a column age### def getKey(item): return item[1] print(sorted(students, key=getKey, reverse=True)) print(sorted(students, key=getKey, reverse=False)) Sorts by age column, finds the item postion [1] the age and then reverse = True to sort in descending order

Searching for data in a list (list 2) students = [["dave", 14, "11DA"], ["liz", 18, "7ED"], ["kerry", 7, "8MD2"]] print (students) #search for data in a particular list (list 1) for i in students[1]: if i == 'liz': print ("Found") else: print ("Not Found" Searches for ‘Liz’ in list 2 finds it in the first column, but not the other two

Searching for data in a list (list 3) students = [["dave", 14, "11DA"], ["liz", 18, "7ED"], ["kerry", 7, "8MD2"]] print (students) #search for data in a particular list (list 3) for i in students[2]: if i == 'liz': print ("Found") else: print ("Not Found") Searches for ‘Liz’ in list 3, does not find the entry, "kerry", 7, "8MD2’, no Liz

Searching for data in a column students = [["dave", 14, "11DA"], ["liz", 18, "7ED"], ["kerry", "liz", "8MD2"]] found = "dave" in (i[0] for i in students) print (found) found = "dave" in (i[1] for i in students) Searches for the name “dave” in each of the lists in position 0. the first entry. Searches the second column for dave, this is False as the column contains a number.

Copying a list numbers = [[3, 5, 78], [89, 34, 23]] #array = [['3', '5', '78'], ['89', '34', '23']] #array new_array = numbers print(numbers) print(new_array) ### however this is not a copy of the list new_array = list(numbers) Must use the list method to copy a new list. Otherwise the copy overwrites to position in memory of the original list (it is mot a new list)

Copying a list: Looking for data and copying numbers = [[3, 5, 78], [89, 34, 23]] new_list_three = [] for i in numbers[0]: if i > 10: new_list_three.append(i) print (new_list_three) Searches for items in one sub lits with a value greater than 10 Then writes them to a new list called (new_list_three)

Copy a list, searching for data in all columns numbers = [[3, 5, 78], [89, 34, 23]] #array = [['3', '5', '78'], ['89', '34', '23']] #array new_list_three = [] ## search one coloumn in all lists) for i in (i[1] for i in numbers): if i > 10: new_list_three.append(i) print (new_list_three) Search each sub list in the array, each item and checks column two [1] if the number is bigger than 10 then it appends it to new list.

Running calculations on Arrays SUM students = [[14, 8, 1], [7, 8, 9], [12, 6, 9]] #add row 1, 14+7+12 print (sum(row[0] for row in students)) For the last column use: print (sum(row[2] for row in students)) Takes the first entry in each row [0] and uses the sum function to add them together 14+7+12 = 33 Row can be replaced with any word you could use column if you prefer

Running calculations on Arrays Average (Mean) from statistics import mean students = [[14, 8, 10], [7, 8, 10], [12, 6, 10]] print (mean(column[2] for column in students)) Find the mean value of column 3, 10+10+10 = 30

Running calculations on Arrays Max / Min from statistics import mean students = [[14, 8, 10], [7, 8, 10], [12, 6, 10]] print (max(row[0] for row in students)) Replace with min for the lowest number Works on strings as well returning the alphabetical positions

Saving 2d array to a CSV file import csv students = [['dave', 14, '11DA'], ['liz', 18, '7ED'], ['kerry', 'liz', '8MD2']] myfile = open('output.csv','w') wr = csv.writer(myfile, quotechar=None) wr.writerows(students) myfile.close() Creates a file called output, writes each item in the list into the CSV file

Reading from CSV file to an array import csv students = [] with open('output.csv', 'r') as f: reader = csv.reader(f) students = list(reader) print (students)