Python datatypes: Difference between revisions
From wikinotes
(→list) |
|||
Line 10: | Line 10: | ||
|- | |- | ||
|} | |} | ||
'''Problem'''<br> | '''Problem'''<br> | ||
If two variables refer to the same mutable object, | If two variables refer to the same mutable object, | ||
changing either variable changes the other. | changing either variable changes the other. | ||
Line 20: | Line 17: | ||
<source lang="python"> | <source lang="python"> | ||
listA= ['a','b','c'] | listA= ['a','b','c'] | ||
listB= listA | listB= listA # both listB and listA point to object of listA now | ||
listA[2]= 'z' | listA[2]= 'z' | ||
listA, listB # ['a', 'z', 'c'], ['a', 'z', 'c'] | listA, listB # ['a', 'z', 'c'], ['a', 'z', 'c'] | ||
</source> | </source> | ||
'''Non-Nested Datatypes''' | '''Non-Nested Datatypes'''<br> | ||
The solution is to copy your original data into an entirely new object. | The solution is to copy your original data into an entirely new object. | ||
<source lang="python"> | <source lang="python"> | ||
listA= ['a','b','c'] | listA= ['a','b','c'] | ||
listB= listA[:] | listB= listA[:] # taking full slice of listA forces creation of new object | ||
</source> | </source> | ||
'''Nested Datatypes''' | '''Nested Datatypes'''<br> | ||
<source lang="python"> | <source lang="python"> | ||
import copy | import copy | ||
Line 44: | Line 38: | ||
listB = copy.deepcopy(listA) | listB = copy.deepcopy(listA) | ||
</source> | </source> | ||
</blockquote><!-- mutable --> | </blockquote><!-- mutable --> | ||
</blockquote><!-- intro --> | </blockquote><!-- intro --> |
Revision as of 19:52, 17 July 2021
concepts
mutable vs immutable
MUTABLE: lists, dict, set
The object containing your variable data can be changed. IMMUTABLE: bool, string, int, long, tuple, frozenset
The object containing your variable data cannot be changed. (for new data, python creates separate new object) Problem
If two variables refer to the same mutable object, changing either variable changes the other.listA= ['a','b','c'] listB= listA # both listB and listA point to object of listA now listA[2]= 'z' listA, listB # ['a', 'z', 'c'], ['a', 'z', 'c']
Non-Nested Datatypes
The solution is to copy your original data into an entirely new object.listA= ['a','b','c'] listB= listA[:] # taking full slice of listA forces creation of new objectNested Datatypes
import copy listA = [ ['a','b'], ['c','d'] ] listB = copy.deepcopy(listA)
datatypes
text
str = 'text' str[0] # Prints first character of the string str[2:5] # Prints characters starting from 3rd to 5th str[2:] # Prints string starting from char after 2 str * 2 # Prints string two times str.upper() # string as all uppercase str.lower() # string as all lowercasestring formatting
'{} {}-items found'.format(3, 'list') # substitute each {} with an item '{found} {type}-items found'.format(**{'found':3, 'list':'list'}) # dict-substitution sys.stdout.write("{:<7}{:<51}{:<25}\n".format(code, name, industry)) # left/right alignment and preformatted spacingSee str.format
string templating
# less strict than formatting import string template = string.Template('my name is ${firstname} ${lastname}') template.safe_substitute(firstname='will') >>> 'my name is will ${lastname}'
split/join
'/a/b/c.txt'.split('/') >>> ['a','b','c.txt'] ['a','b','c.txt'].join('/') >>> '/a/b/c.txt'zfill
'15'.zfill(4) >>> '00015'numbers
int = 123 # int long = 123L # long (very big integers) float = 123.0 # float (a C double object)collections
list
Mutable, resizable collection of items (of any type).
list # Prints complete list list[0] # Prints first element of the list list[1:3] # Prints elements starting from 2nd till 3rd list[2:] # Prints elements starting from 3rd element list * 2 # Prints list two times list + tinylist # Prints concatenated lists list.append(4,5) # adds to the end of the list ex: ["abc", "efg", "4", "5"] list.append([4,5]) # list entry is a list 4,5 ex: ["abc", "efg"] ["4", "5"] list.remove(4) # removes 4 from list list.index('abc') # returns index of listItem 'abc' list.pop(0) # remove listItem at index 0 ",".join(['a', 'b']) # join list items by a comma ['cat','dog','fish'].index('dog') # Returns index number of entry 'dog' in list 'a' in ['a','b','c'] # Prints True or False if item exists in list max([1, 36, 2]) # prints highest number in list min([1, 36, 2]) # prints lowest number in list len([1, 36, 2]) # prints number of entries in list [x for x in ('a','b','c')] # list-comprehension. produces a list ['a','b','c'] [x for x in ('a','b','c') if x == 'a'] # list-comprehension with iftuple
immutable collection of items (of any type).
tuple = tuple() # empty tuple tuple = (,) # empty tuple tuple = (1,2,3) # tuple with items 1,2,3 tuple[1] # refers to '2' tuple[1:] # items with index 1-onwards tuple[:-1] # items before last index tuple[1:-1] # items between 1st/last itemset
mutable, unordered collection of unique items.
set = set() set = {1,2,3} set.issubset({1,2}) # {1,2} is made entirely from items in var ? set.issuperset({1,2,3,4}) # {1,2,3,4,5} contains all items from var ? set.union({4,5}) # items from both {4,5} and var set.difference({3,4,5}) # {3,4,5} with items from var removed from it set.add(4) # add item to set (ignored if already exists) set.pop(4) # remove item from setdict
mutable collection of key-value pairs. Can be nested.
dict = {} dict = {'a':123, 'b':456} dict['a'] del dict['a'] # remove item 'a' from dict dict.pop('a') # remove/return item 'a' from dict dict.get('a',None) # retrieve value of 'a', default to Nonecollection variations
namedtuple
Defines an arbitrary type, a self-documenting tuple. Even the repr is updated so that you know what the item is, and what each assignment is. Think of this like a faster, immutable dictionary.
import collections cartype = collections.namedtuple('car',['model','license','year']) car = cartype('honda', 'BHXZ921', 2018) car = cartype(model='honda', license='BHXZ921', year=2018) print(car.year) #> 2018 print(car.model) #> 'honda'defaultdict
TODO:
todoOrderedDict
TODO:
todo