Difference between revisions of "Python:DataTypes"

From wiki
Jump to navigation Jump to search
(21 intermediate revisions by the same user not shown)
Line 17: Line 17:
 
:Check if a variable exists and is global.
 
:Check if a variable exists and is global.
  
Note: Variables are pointers to objects, not the object itself.
+
<strong>NOTE:</strong> Variables are pointers to objects, not the object itself. dict2 = dict1 does not make a new dictionary. Both variables point to the same dictionary. To make a copy use dict2 = dict(dict1). Same for lists, sets and tuples.  
  
 
==list==
 
==list==
Line 24: Line 24:
 
;lst1 = []
 
;lst1 = []
 
:Initialize an empty list
 
:Initialize an empty list
 +
 +
;lst1 = [0] * 10
 +
:Create a list with 10 elements 0
  
 
;lst1.append(2)
 
;lst1.append(2)
 
:Add the '2' object to the end of lst1
 
:Add the '2' object to the end of lst1
 +
 +
;lst1.extend(list2)
 +
:Add the elements of list2 object to the end of lst1
  
 
;lst1.pop(n)
 
;lst1.pop(n)
Line 46: Line 52:
 
:Return the iterable object sorted as list
 
:Return the iterable object sorted as list
  
[https://wiki.python.org/moin/HowTo/Sorting More on sorting] e.g. using keys.
+
;<nowiki>srtlist = sorted(alist, key = lambda x: (x[4], x[0]))</nowiki>
 +
:Return alist sorted on the third and the first element of each list in alist (alist is a list of lists)
 +
 
 +
[https://wiki.python.org/moin/HowTo/Sorting More on sorting].
  
;print '[%s]' % ', '.join(map(str, lst1))
+
;print '[%s]' % ', '.join(map(str, alist))
;print (','.join(lst1))
+
;print (','.join(alist))
:Print lst1 as [<element>,..]
+
:Print alist as <element1>,<element2>,...
 +
;print ('\n'.join(alist))
 +
:Print alist with each element on another line
  
 
;Put items matching a regular expression in newlist.
 
;Put items matching a regular expression in newlist.
 
More on regular expressions in [[Python:Strings#Regular_Expressions_(regexp)]]
 
More on regular expressions in [[Python:Strings#Regular_Expressions_(regexp)]]
 +
 +
NOTE: filter applies a function (regexp matching in this case) on the object (list1). The object is modified. A filter object is returned. A filter object is not a list. However, when you iterate over it, it does return the matching elements.
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
import re
 
import re
newlist = filter(re.compile(<regular expression>).search,list1)
+
filterobject = filter(re.compile(<regular expression>).search,list1)    # This returns a filter object you can iterate over but not slice like filterobject[0]
 +
newlist = list(filter(re.compile(<regular expression>).search,list1)))  # This returns a real list
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
;Select from a list of lists.
 +
<code>newist = [i for i in lists if 'SERACHSTRING' in str(i)]</code>
 +
 +
;Check if some values are in a list (works for sets too)
 +
<code>if [ i for i in alist if i in [value1,value2]]:</code>
  
 
==set==
 
==set==
Line 73: Line 93:
  
 
;set1 = set([<values>])
 
;set1 = set([<values>])
:Initialize a set with <values>. Note the list-format of <values>.
+
;set1 = {<val1>,<val2>}
 +
:Initialize a set with objects. Note the list-format of <values>.
  
 
;set1.add(2)
 
;set1.add(2)
Line 83: Line 104:
 
;unionset = set1.union(set2)
 
;unionset = set1.union(set2)
 
:Combine the sets (e.g.to remove duplicates)
 
:Combine the sets (e.g.to remove duplicates)
 +
 +
;intersectset = set1.intersection(set2)
 +
:All elements that exist in both sets
  
 
;diffset = set1 - set2
 
;diffset = set1 - set2
 +
;diffset = set1.difference(set2)
 
:diffset will have all elements of set1 that are not in set2
 
:diffset will have all elements of set1 that are not in set2
  
 
==Tuple==
 
==Tuple==
Class of iterable, immutable objects. Results from database queries are by default returned as tuple.
+
Class of iterable, immutable objects. It's an immutable list. Results from database queries are by default returned as tuple.
  
 
;tpl1 = ()
 
;tpl1 = ()
Line 106: Line 131:
 
:Test if key exists in dict. <code>if dict[key]:</code> will throw a keyerror if it does not exist.  
 
:Test if key exists in dict. <code>if dict[key]:</code> will throw a keyerror if it does not exist.  
  
;list(dict1.keys())
+
;dict1.keys()
 
:[[Python:DataTypes#list|List]] of keys in dict1
 
:[[Python:DataTypes#list|List]] of keys in dict1
;list(dict1.values())
+
;dict1.values()
 
:[[Python:DataTypes#list|List]] of values in dict1
 
:[[Python:DataTypes#list|List]] of values in dict1
;list(dict1.items())
+
;dict1.items()
 
:[[Python:DataTypes#list|List]] of key-value pairs [[#Tuple]]s in dict1
 
:[[Python:DataTypes#list|List]] of key-value pairs [[#Tuple]]s in dict1
  
Line 116: Line 141:
 
:Add dict2 to dict1. Duplicate keys are overwritten in dict1.
 
:Add dict2 to dict1. Duplicate keys are overwritten in dict1.
  
;<code>dict1.pop(key)</code>
+
;<code>dict1.pop(key,None)</code>
 
:Remove key from dict1, return dict1[key] if successful, None if key does not exist in dict1.
 
:Remove key from dict1, return dict1[key] if successful, None if key does not exist in dict1.
  
Line 122: Line 147:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
from collections import defaultdict
 
from collections import defaultdict
dict = defaultdict{lambda: defaultdict()}
+
dict = defaultdict(lambda: defaultdict())
 
dict["name1"]["street"] = "mystreet"
 
dict["name1"]["street"] = "mystreet"
 
   
 
   
Line 151: Line 176:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
[value for key, value in d.items() if 'searchstring' in key]
 
[value for key, value in d.items() if 'searchstring' in key]
 +
[value for key, value in d.items() if re.search(pattern,key)]
 
[value for key, value in d.items() if key == 'keyname']
 
[value for key, value in d.items() if key == 'keyname']
 +
# List of keys that match
 +
[key for key in d.keys() if key == 'keyname']
 
</syntaxhighlight>
 
</syntaxhighlight>
 
This very non-intuitive syntax is the same as:
 
This very non-intuitive syntax is the same as:
Line 161: Line 189:
 
return list1
 
return list1
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
;<code>sorted(dict1.items(), key=lambda x: x[1], reverse=False)</code>
 +
:Sort a dict on value, return a list of tuples (key,value) sorted on value
  
 
==None==
 
==None==
Line 172: Line 203:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
for i in range (2,8,2):
+
for i in range (2,5,1):
 
     print(i)
 
     print(i)
 +
2
 +
3
 +
4
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
==DateTime==
 
==DateTime==
Lots more to do than setting timestamps (to be added)
+
 
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
from datetime import datetime
 
from datetime import datetime
Line 183: Line 217:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Other formats:
 
Other formats:
:%s - Seconds since epoc (January 1, 1970 00:00:00)
+
:%s - Seconds since [https://www.epochconverter.com/ epoc] (January 1, 1970 00:00:00)
 
:%f - Nanoseconds or Milliseconds depends on what your system supports
 
:%f - Nanoseconds or Milliseconds depends on what your system supports
 +
 +
;datetime.datetime(year, month, day, hour, minute, second).strftime('%s')
 +
:Date to [https://www.epochconverter.com/ epoc]; hour, minute and second are optional.
  
 
;Get an unique integer. Time is cheaper than datetime.
 
;Get an unique integer. Time is cheaper than datetime.
Line 196: Line 233:
 
from datetime import datetime
 
from datetime import datetime
 
timestamp = datetime.utcnow().strftime("%s%f")
 
timestamp = datetime.utcnow().strftime("%s%f")
 +
</syntaxhighlight>
 +
 +
;Convert date strings <nowiki>(1-jan-1991 12:30)</nowiki> to a datetime object
 +
<syntaxhighlight lang=python>
 +
from dateutil.parser import parse
 +
datetimeobject = parse(datestring)
 +
</syntaxhighlight>
 +
 +
;Calculations (7 days before now)
 +
<syntaxhighlight lang=python>
 +
from datetime import date
 +
from datetime import timedelta
 +
newdate = date.today() - timedelta(days=7)
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 21:09, 10 February 2021


Object Classes

Lots of things to tell about strings, they have there own string page.

Also for numpy (module for scientific arithmetic) there is a special page

Objects are iterable if they can contain more than 1 ordered objects (string, list, tuple, dict). Objects are mutable if their content can be changed (list, set, dict)

isinstance(<obj>, <class>)
Boolean (returns True or False) to check if <obj> is an instance of <class>
if in locals()
Check if a variable exists as local
if in globals()
Check if a variable exists and is global.

NOTE: Variables are pointers to objects, not the object itself. dict2 = dict1 does not make a new dictionary. Both variables point to the same dictionary. To make a copy use dict2 = dict(dict1). Same for lists, sets and tuples.

list

Class of iterable, mutable objects. Lists can be compared to arrays in other languages. Lists can contain a mixture of all kind of objects.

lst1 = []
Initialize an empty list
lst1 = [0] * 10
Create a list with 10 elements 0
lst1.append(2)
Add the '2' object to the end of lst1
lst1.extend(list2)
Add the elements of list2 object to the end of lst1
lst1.pop(n)
Remove and return nth element from lst1. Last element if n is not specified.
lst1 = list(object)
Convert object to a list (object is e.g. set, tuple or string)
count = lst1.count[x]
Return the number of occurrences of x in lst1
lst1.index(x)
Return the position of x (first occurrence) in lst1
lst1.sort()
Sort lst1 and return 'None' object
lst2 = sorted(iterable)
Return the iterable object sorted as list
srtlist = sorted(alist, key = lambda x: (x[4], x[0]))
Return alist sorted on the third and the first element of each list in alist (alist is a list of lists)

More on sorting.

print '[%s]' % ', '.join(map(str, alist))
print (','.join(alist))
Print alist as <element1>,<element2>,...
print ('\n'.join(alist))
Print alist with each element on another line
Put items matching a regular expression in newlist.

More on regular expressions in Python:Strings#Regular_Expressions_(regexp)

NOTE: filter applies a function (regexp matching in this case) on the object (list1). The object is modified. A filter object is returned. A filter object is not a list. However, when you iterate over it, it does return the matching elements.

import re
filterobject = filter(re.compile(<regular expression>).search,list1)    # This returns a filter object you can iterate over but not slice like filterobject[0]
newlist = list(filter(re.compile(<regular expression>).search,list1)))  # This returns a real list
Select from a list of lists.

newist = [i for i in lists if 'SERACHSTRING' in str(i)]

Check if some values are in a list (works for sets too)

if [ i for i in alist if i in [value1,value2]]:

set

Class of iterable, mutable objects. Objects added to sets are hashed. Therefor:

  • Only immutable objects can be added to a set.
  • Sets cannot hold duplicate objects (adding an object again does not change the set).
  • Checking if a set holds an object is very fast.

A set is iterable but has no order, therfor:

  • You can loop over a set like for a in set:
  • You cannot take a slice from a set.
set1 = set()
Initialize an empty set
set1 = set([<values>])
set1 = {<val1>,<val2>}
Initialize a set with objects. Note the list-format of <values>.
set1.add(2)
Add the '2' object to set1. You can add only 1 object at a time.
set1.discard(2)
Remove the '2' object from set1 (returns None object)
unionset = set1.union(set2)
Combine the sets (e.g.to remove duplicates)
intersectset = set1.intersection(set2)
All elements that exist in both sets
diffset = set1 - set2
diffset = set1.difference(set2)
diffset will have all elements of set1 that are not in set2

Tuple

Class of iterable, immutable objects. It's an immutable list. Results from database queries are by default returned as tuple.

tpl1 = ()
Initialize an empty tuple

Dictionary or dict

Class of iterable, mutable objects. Dictionary's can be compared to perl hashes. Check the Python:JSON page too.

dict1 = {}
Initialize an empty dictionary.
dict1 = { column1: value1, column2: value2 }
Initialize dictionary with data

Check collections defaultdict for automatic key creation.

if key in dict:
Test if key exists in dict. if dict[key]: will throw a keyerror if it does not exist.
dict1.keys()
List of keys in dict1
dict1.values()
List of values in dict1
dict1.items()
List of key-value pairs #Tuples in dict1
dict1.update(dict2)
Add dict2 to dict1. Duplicate keys are overwritten in dict1.
dict1.pop(key,None)
Remove key from dict1, return dict1[key] if successful, None if key does not exist in dict1.

Code example:

from collections import defaultdict
dict = defaultdict(lambda: defaultdict())
dict["name1"]["street"] = "mystreet"
 
for name in dict:
   print name
   for key2 in dict[name]:
      print key2,dict[name][key2]
 
for name in dict:
   print name
   for key2 in sorted(dict[name].keys()):
      print key2,dict[name][key2]

Recursively search a key:

def search_dict(data,skey=None):
    result = None
    if skey in data:
        result = data[skey]
    else:
        for key in data:
            if isinstance(data[key],dict):
                result = search_dict(data[key],skey)
    return result

Create a list of dict values where the key matches a string (List comprehension for dicts)

[value for key, value in d.items() if 'searchstring' in key]
[value for key, value in d.items() if re.search(pattern,key)]
[value for key, value in d.items() if key == 'keyname']
# List of keys that match
[key for key in d.keys() if key == 'keyname']

This very non-intuitive syntax is the same as:

list1 = list[]
for key,value in d.items():
    if 'searchstring' in key():
        list1.append(value)
return list1
sorted(dict1.items(), key=lambda x: x[1], reverse=False)
Sort a dict on value, return a list of tuples (key,value) sorted on value

None

The None object is returned e.g. if nothing is found in a re.search. The None object is not an empty string

Range

Constructor of immutable sequences of integers. Use with list, set, tuple to create the desired object, or for loops.

range(start,stop,step)
Generic format. If you leave out step, step = 1. If only 1 parameter is provided, it is the stop number, start = 0, step = 1
for i in range (2,5,1):
    print(i)
2
3
4

DateTime

from datetime import datetime
timestamp = datetime.now().strftime("%y%m%d_%H%M%S")

Other formats:

%s - Seconds since epoc (January 1, 1970 00:00:00)
%f - Nanoseconds or Milliseconds depends on what your system supports
datetime.datetime(year, month, day, hour, minute, second).strftime('%s')
Date to epoc; hour, minute and second are optional.
Get an unique integer. Time is cheaper than datetime.
import time as t
# Use UTC-time 1 tick per second
nonce = int(t.mktime(t.gmtime()))
# This has greater precision (nanoseconds) but is in local time, not unique when the clock is set back.
nonce = int(t.time()*10000)
# I use datetime as that is loaded most time anyhow for other timestamps
from datetime import datetime
timestamp = datetime.utcnow().strftime("%s%f")
Convert date strings (1-jan-1991 12:30) to a datetime object
from dateutil.parser import parse
datetimeobject = parse(datestring)
Calculations (7 days before now)
from datetime import date
from datetime import timedelta
newdate = date.today() - timedelta(days=7)

Slicing

You can address all iterable datatypes partly or in a difference sequence.

object[b:e:s]
Generic format where b=Begin (counting starts at 0), e=End, s=Stepsize (negative stepsize starts counting at the end)

Examples:

'last element'[-1]
'All elements except the last'[:-1]
'All elements in reversed order'[::-1]

'All elements from the second'[1:]
'Second until 5th element (element 1,2,3 and 4)'[1:5]  
'Elements in reversed order (element 5,4,3 and 2)'[5:1:-1]
'Element 1,3 and 5'[1:6:2]