Lists, Tuples And Dictionaries - Purdue University

1y ago
7 Views
1 Downloads
1.81 MB
19 Pages
Last View : 4m ago
Last Download : 3m ago
Upload by : Luis Wallis
Transcription

Lists, Tuples andDictionariesHORT 59000Lecture 10Instructor: Kranthi Varala

Core data types Numbers Strings Lists Dictionaries Tuples Files Sets

References and dynamic typing Dynamic typing allows changing the type of a variable. A ‘42’ now changes the apparent data type of A to aninteger.ASampleStringB42 The reference from A to ‘Sample String’ is removed. B still points to the ‘Sample String’ object. If all variable reference are removed from an object, theobject will be marked for removal by Python. The process of removing dereferenced objects is calledgarbage collection

Lists List is a general sequence object that allowsthe individual items to be of different types. Equivalent to arrays in other languages. Lists are mutable, i.e., a list can be changedwithout having to create a new list object

Lists: Common Methods L.append() : Adds one item to the end of the list.L.extend() : Adds multiple items to the end of the list.L.pop(i) : Remove item ‘i’ from the list. Default:Last.L.reverse() : Reverse the order of items in list.L.insert(i,item): Inserts ‘item’ at position i.L.remove(item) : Finds ‘item’ in list and deletes it fromthe list. L.sort(): Sorts the list in- place i.e., changes thesequence in the list.

MultiDimensional Lists Lists are of arbitrary length and and easily be nested. Simplest nested lists are 2 –dimensional matrices. my2DList 23015913126101423711153481216my2DList

MultiDimensional Lists Nested Lists need not be homogeneous. my2DList 3acat1216my2DList

Arbitrary dimensional Lists Nested Lists need not be of the same length. my2DList 3a1216my2DListcat

Arbitrary dimensional Lists Nested Lists can have arbitrary depth as well. subL [[’p’,’q’],[‘r’,’s’]] my2DList List237e3acat1215010pq1rs

Lists as sequences of references myList Str[Ref]Str[Ref]intStrintStrint

Lists as sequences of references myList [Ref]Str[Ref][Ref]intStrintint[Ref]StrStr

Lists are mutable!! subL [[’p’,’q’],[‘r’,’s’]] myList 2],[’beta’,14,15,subL]] r’,’s’]]]] subL[0][1] ‘z’ r’,’s’]]]]

Tuples Tuples are immutable general sequenceobjects that allows the individual items to be ofdifferent types. Equivalent to lists, except that they can’t bechanged.

Tuples Tuple.count(value) : Returns number of occurences ofvalue. Tuple.index(value,[start,stop]) : Returns first index ofvalue. Typically used to maintain data integrity within theprogram.Even single elements need a commaParentheses () are optional

Dictionaries Dictionaries are unordered collections of objects,optimized for quick searching. Instead of an index, objects are identified by their ‘key’. Each item within a dictionary is a ‘key’:’value’ pair. Equivalent to hashes or associative arrays in otherlanguages. Like lists and tuples, they can be variable-length,heterogeneous and of arbitrary depth. ’Keys’ are mapped to memory locations by a hashfunction

Hash function A hash function converts a given key value intoa ‘slot’ where its value will be stored. A hash function always takes a fixed amount oftime and always returns the same slot for thesame ‘key’. When program searches for a ‘key’ in thedictionary, the slot it should be in is calculatedand the value in it, if any, is returned. Creating hashes is expensive, searching ischeap.

Dictionaries

Dictionaries: Common Methods D.keys() : List of keysD.values() : List of valuesD.clear() : remove all itemsD.update(D2) : Merge key values from D2 into D.NOTE: Overwrites any matching keys in D. D.pop(key) : returns the value of given key andremoves this key:value pair from dictionary.

Summary: Lists vs. Tuples vs. Dictionaries All three data types stores a collection of items. All three allow nesting, heterogeneity and arbitrarydepth. Choice of data type depends on intended use: Lists : Best suited for ordered collections of items where theorder or the items themselves may need to be changed. Tuples: Best suited for maintaining a copy of the collectionthat will not be accidentally changed during the program. Dictionary : Best suited for storing labeled items, especially incollections where frequent searching is required.

Dictionaries Dictionaries are unordered collections of objects, optimized for quick searching. Instead of an index, objects are identified by their 'key'. Eachitemwithin a dictionary is a 'key':'value' pair. Equivalent to hashes or associative arrays in other languages. Like lists and tuples, they can be variable-length,

Related Documents:

Python Scipy Data I/O Visualization Python is an object oriented language “In Python, we do things with stuff!” things operations stuff objects Type Example Numbers 128, 3.14, 4 5j Strings ’Rony , "Giovanni’s" Lists [1,"string",2.45] Tuples (1,"string",2.45) Strings, Lists and Tuples are sequences. Strings and Tuples are .

Dictionaries! Dictionaries are Pythonʼs most powerful data collection! Dictionaries allow us to do fast database-like operations in Python! Dictionaries have different names in different languages! Associative Arrays - Perl / Php! Properties or Map or HashMap - Java! Property Bag - C# / .Net!

Lets now combine what we have learned with dictionaries and tuples. Lets make the .NBA Superstar Basketball Dictionary! CHALLENGE PROBLEM Take in the below NBA player data, and create tuples for them. Here is the format: The first value in the tuple is the number of championships won, and the second is the number of season the player has played.

the cost of creating new dictionaries in the first place may be pro-hibitively expensive and time-consuming for many researchers. For example, many of the LIWC dictionaries used up to 8 judges across several stages in order to manually expand the word lists (Pennebaker et al., 2015). Given that the constructs measured by existing dictionaries are

Purdue Printing Services The School of Pharmacy and Pharmaceutical Sciences Purdue University Heine Pharmacy Building, Room 104 575 Stadium Mall Drive West Lafayette, IN 47904-2091 (765) 494-1361 (765) 494-7800 Fax www.pharmacy.purdue.edu The Purdue Pharmacist is published three times a year for alumni

The data structures available in python are lists, tuples and dictionaries. Listsare like one-dimensional arrays (but you can also have lists of other lists), dictionariesare associative arrays (a.k.a. hash tables) and tuplesare immutable one-dimensional arrays (Python "arrays" can be of any type, so you can mix e.g. integers, strings, etcin

IJL Dictionaries: usefulness and usability 4 seen dictionaries as an essential contribution to work on endangered languages, and as such they have been mainly concerned with the task of preserving languages for future study or revival. In the past the main audience for dictionaries of these languages has been felt to be linguists and other

The American Revolution Part One: The events leading up the Revolutionary War (1750 – 1775) Background Historically speaking, right now “we” are British. The Colonies are an extension of Britain, so we share their government, their identity, their pride, and also their enemies. There is NO United States of America. Taunton Flag, flown by colonists to show unity with the British crown .