Difference between revisions of "Python-datastructures"

From wikipost
Jump to navigationJump to search
 
(7 intermediate revisions by the same user not shown)
Line 17: Line 17:
 
|style="background-color:#f2f2f2;"|<b>Characteristic</b>
 
|style="background-color:#f2f2f2;"|<b>Characteristic</b>
 
|-
 
|-
|Tuple
+
|[[Tuple]]
 
|('January', 'February', 'March', 'April', 'May')
 
|('January', 'February', 'March', 'April', 'May')
 
(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
 
(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
Line 23: Line 23:
 
Referenced with index
 
Referenced with index
 
|surrounded by one single open and closed parenthesis
 
|surrounded by one single open and closed parenthesis
 +
elements separated by a comma
 +
 +
Strings are enclosed by apostrophes or quotes
 
|-
 
|-
|List
+
|[[python-lists|List]]
 
|[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
 
|[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
 
['red', 'green', 'orange', 'blue']
 
['red', 'green', 'orange', 'blue']
Line 32: Line 35:
 
elements separated by a comma
 
elements separated by a comma
  
strings enclosed by single or double quotes
+
Strings are enclosed by apostrophes or quotes
 
|-
 
|-
|Dictionary
+
|[[Dictionary]]
 
|{'Joe':02079343, 'Alice':010534045, 'Bob':01084523}
 
|{'Joe':02079343, 'Alice':010534045, 'Bob':01084523}
 
{'x':234, 'y':345, 'z':456}
 
{'x':234, 'y':345, 'z':456}
Line 42: Line 45:
 
elements separated by a comma
 
elements separated by a comma
  
elements contain a key and a value
+
each element contains one key and one value
  
 
key and value are separated by a colon
 
key and value are separated by a colon
  
strings enclosed by single or double quotes
+
key maybe a string or a number
 +
 
 +
strings (key or value) are enclosed by quotes
 +
 
 +
keys must be unique
 
|-
 
|-
|Set
+
|[[Set]]
 
|([1029, 1238, 7821, 4312, 2716])
 
|([1029, 1238, 7821, 4312, 2716])
 
|Once set, values cannot be modified
 
|Once set, values cannot be modified
Line 59: Line 66:
 
elements separated by a comma
 
elements separated by a comma
  
strings enclosed by single or double quotes
+
strings are enclosed by apostrophes or quotes
 
|-
 
|-
|json
+
|[[json]]
|x
+
|[{ "first": "Joe', "hobbies": "swimming, running" }
 +
{ "first": "Carol', "hobbies": "cycling, climbing" }]
 +
 
 
|Values can be modified, added, deleted
 
|Values can be modified, added, deleted
 
Referenced with a key
 
Referenced with a key
|surrounded by one single open and closed square bracket
+
|surrounded by one single open and closed curly brace (1x record)
 +
multiple records surrounded by one single square open and close bracket
 +
 
 +
records surrounded by curly braces
 +
 
 +
record elements separated by a comma
 +
 
 +
each element contains one key and one value
 +
 
 +
key and value are separated by a colon
 +
 
 +
keys surrounded by double quotes
 +
 
 +
strings are enclosed by apostrophes or quotes
 +
 
 
|-
 
|-
|csv
+
|[[csv]]
|x
+
|silver,32423,joe bloggs,"8,5",023-32425634,,,type=1,f
|y
+
|once set, values cannot be modified
 
|elements separated by a comma
 
|elements separated by a comma
elements enclosed by double quotes if string contains a comma
+
 
 +
one record per line
 +
 
 +
elements enclosed by quotes if string contains a comma
 
|}
 
|}

Latest revision as of 14:29, 23 December 2020

"In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data."

-- source: Wikipedia


Python knows how to work with of several types of data structures without the need to import additional libraries. Sometimes you do need to import a library to provide support (e.g. json, csv, sql) but most of these are already available when you have python installed. In rare cases you may need to build or acquire a third-party library for data structure support.


In these pages we mainly look at the most common data structures in python:


Name Examples Details Characteristic
Tuple ('January', 'February', 'March', 'April', 'May')

(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)

Once set, values cannot be changed

Referenced with index

surrounded by one single open and closed parenthesis

elements separated by a comma

Strings are enclosed by apostrophes or quotes

List [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

['red', 'green', 'orange', 'blue']

Values can be modified, added, deleted

Referenced with index

surrounded by one single open and closed square bracket

elements separated by a comma

Strings are enclosed by apostrophes or quotes

Dictionary {'Joe':02079343, 'Alice':010534045, 'Bob':01084523}

{'x':234, 'y':345, 'z':456}

Values can be modified, added, deleted

Referenced with a key

surrounded by one single open and closed curly brace

elements separated by a comma

each element contains one key and one value

key and value are separated by a colon

key maybe a string or a number

strings (key or value) are enclosed by quotes

keys must be unique

Set ([1029, 1238, 7821, 4312, 2716]) Once set, values cannot be modified

Set values can be added and removed

Set may only contain unique values

Unordered

single square brackets surrounded by single parenthesis

elements separated by a comma

strings are enclosed by apostrophes or quotes

json [{ "first": "Joe', "hobbies": "swimming, running" }

{ "first": "Carol', "hobbies": "cycling, climbing" }]

Values can be modified, added, deleted

Referenced with a key

surrounded by one single open and closed curly brace (1x record)

multiple records surrounded by one single square open and close bracket

records surrounded by curly braces

record elements separated by a comma

each element contains one key and one value

key and value are separated by a colon

keys surrounded by double quotes

strings are enclosed by apostrophes or quotes

csv silver,32423,joe bloggs,"8,5",023-32425634,,,type=1,f once set, values cannot be modified elements separated by a comma

one record per line

elements enclosed by quotes if string contains a comma