Skip to content

Latest commit

 

History

History
294 lines (214 loc) · 10 KB

DataStructure.md

File metadata and controls

294 lines (214 loc) · 10 KB

← back to Main Page


Data Structure

TYPE DESCRIPTION EXAMPLE UNIQUE VARIABLES IMMUTABLE
list Sequence ['a', 1, 4,0, (tuple), [list]] X X
tuple Sequence ('a', 1, 4.0, (tuple), [list]) X O
dictionary Hashmap(pair) {key1:value1, key2:value2} O(Key) X
set Hashmap {key1, key2} O X

Lists

A MUTABLE Sequence of Data: It can contains different data types.

Atom of a list

  • boolean
  • integer
  • numeric
  • float
  • string
  • another list
  • tuple
  • dictionary
  • set

Operation

mylist = ['q', 'u', 'e', 's', 't', 'i', 'o', 'n']
OPERATION METHOD EXAMPLE RESULT
Generate [a, b]
list(a, b)
['t', 1]
list('t', 1)
['t', 1]
Transform to list
(ex) tuple to list)
list((seq)) list('question') ['q', 'u', 'e', 's', 't', 'i', 'o', 'n']
Offset list[int] mylist[-2**]** 'o'
Slice list[start:end:step] mylist[7:2:-2] ['n', 'i', 's']

Methods

mylist = ['a', 1, 'b']
yrlist = ['e', 'c', 'd']
OPERATION METHOD EXAMPLE RESULT
Append list.append(var) mylist.append(yrlist) ['a', 1, 'b', ['e', 'c', 'd']]
Annex list.extend(list)
list1 += list2
mylist.extend(yrlist)
* mylist += yrlist
['a', 1, 'b', 'e', 'c', 'd']
Insert list.insert(index, var) mylist.insert(1, 'z') ['a', 'z', 1, 'b']
Remove list.remove(var) mylist.remove(1) ['a', 'b']
Get var & remove list.pop( )
list.pop(0)
list.pop(-1**)**
mylist.pop( )
mylist.pop(0)
mylist.pop(-1)
console>'a'
(list) [1, 'b']
console>'b'
(list) ['a', 1]
console>'b'
(list) ['a', 1]
Get index list.index(var) mylist.index('a') 0
Count list.count(var) mylist.count('b') 1
Sort asc: list.sort( )
desc: list.sort(reverse=True)
yrlist.sort()
yrlist.sort(reverse=True)
['c', 'd', 'e']
'e', 'd', 'c']
list + list

Built-in Function

OPERATION FUNCTION EXAMPLE RESULT
Sort & return a new list sorted(list) sorted(yrlist) ['c', 'd', 'e']
Length len(list) len(mylist) 3

Statements

OPERATION STATEMENT EXAMPLE RESULT
Delete del list[ ] del mylist[2] ['a', 1]

Align

mylist = [1, 2, 3]
xylist = mylist
xylist#[1, 2, 3]
  • xylist refers to mylist
    It means the list [1, 2, 3] is assigned to mylist & xylist. (In other words, that list can be called with both of names)
    If a mylist's variable changes, xylist
mylist[1] = 5
mylist#[1, 5, 3]
xylist#[1, 5, 3]

Copy

  • list.Copy( )

  • list( )

  • [ : ]

  • #-- "Copy()" Method is used to copy & edit a list!) --#

  1. Case 1(not using copy())
mylist = [0, 1, 2, 3, 4]
otlist = mylist
otlist[1] = 10
otlist#[0, 10, 2, 3, 4]
mylist#[0, 10, 2, 3, 4]
otlist == mydict#True

#If one is changed, both of dicts will be changed.
  1. Case 2(using copy())
mylist = [0, 1, 2, 3, 4]
otlist = mylist.copy()
otlist[1] = 10
otlist#[0, 10, 2, 3, 4]
mylist#[0, 1, 2, 3, 4]
otdict == mydict#False
#Even if one is changed, the other will NOT be changed.

Concatenate

import itertools as it

alist = [0, 1, 2]
blist = [3, 4]
clist = [5, 6, 7, 8]

alist + blist + clist
it.chain(alist, blist, clist)
sum([alist, blist, clist], [])

↑ Up to the Top


Tuples

A IMMUTABLE Sequence of Data: It can contains different data types.

Type of a tuple (key: value)

KEY VALUE
boolean
integer
numeric
float
string
tuple
boolean
integer
numer### Operationic
float
string
list

Operation

mytuple = 'q', 'u', 'e', 's', 't', 'i', 'o', 'n'
OPERATION METHOD EXAMPLE RESULT
Generate (var1, var2)
tuple(var1, var2)
var1**,** var2
('t', 1)
tuple('t', 1)
't', 1
('t', 1)
Generate(single var) (var1,)
tuple(var1,)
var1**,**
('t',)
tuple('t',)
't',
('t',)
UNPACKING name1, ..., nameN = tuple (len=N) foo, bar = tuple('t', 1) foo # 't', bar # 1

Advantages of TUPLE

  • A Tuple uses less space than a list
  • Items of a tuple are IMMUTABLE; It cannot be damaged

↑ Up to the Top


Dictionaries

Not a sequence;(UNOREDERED) An dictionary cannot be indexed & cannot use offset.

  • Its Key value MUST BE UNIQUE (If overlapped, the Last input will be used.)

Key에 list는 사용 불가, tuple은 사용 가능.(Immutable)
list를 Key로 설정하면 TypeError 발생.

Operation

mydict = {'coffee': 7, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
yrdict = {'milk' : 8, 'juice': 7 }
OPERATION METHOD EXAMPLE RESULT
Generate {a: b}
dict(a, b)
{'water': 20, 'wine': 'outofstock'}
dict('water': 20, 'wine': 'outofstock')
{'water': 20, 'wine': 'outofstock'}
Transform to dict
(ex) tuple to dict)
dict((pair or pairlist)) dict([('milk', 11), ('water': 20)]) {'milk': 11, 'water': 20}
Select dict[key] mydict['milk'] 11
Select All keys dict.keys( ) mydict.keys() dict_keys(['coffee', 'milk', 'water', 'wine'])
Select All values dict.values( ) mydict.values() dict_values([7, 11, 20, 'outofstock'])
Select All elements by tuple dict.items( ) mydict.items() dict_items([('coffee', 7), ('milk', 11), ('water', 20), ('wine', 'outofstock')])
Replace dict[key]=newval
dict[newkey]=newval
mydict['milk']=10
mydict['tea']=10
{'coffee': 7, 'milk': 10, 'water': 20, 'wine': 'outofstock'}
{'coffee': 7, 'milk': 11, 'water': 20, 'wine': 'outofstock', 'tea' = 10}
Update or Join dict1.update(dict2) mydict.update(yrdict) {'coffee': 7, 'milk': 8, 'water': 20, 'wine': 'outofstock', 'juice' = 7 }
Copy dict.**copy( ) ** otdict = mydict.copy() {'coffee': 7, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
Delete del dict[key] del mydict['wine'] {'coffee': 7, 'milk': 11, 'water': 20}
Delete All dict.clear( )
dict = { }
mydict.clear()
mydict = {}
{}
Check a key **'key' in **dict 'beer' in mydict
'coffee' in mydict
False
True
  • #-- "Copy()" Method is used to copy & edit a dict!) --#
  1. Case 1(not using copy())
mydict = {'coffee': 7, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
otdict = mydict
otdict['coffee'] = 10
otdict#{'coffee': 10, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
mydict#{'coffee': 10, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
otdict == mydict#True

#If one is changed, both of dicts will be changed.
  1. Case 2(using copy())
mydict = {'coffee': 7, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
otdict = mydict.copy()
otdict['coffee'] = 10
otdict#{'coffee': 10, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
mydict#{'coffee': 7, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
otdict == mydict#False
#Even if one is changed, the other will NOT be changed.

↑ Up to the Top

Sets

An UNORDERED Data Structure consists of UNIQUE items.(As a dict, only the KEY part)

  • If there are overlapped values, ONLY ONE can remains.
  • Set is used to calculate "Sets" on Mathmatics;Unions,Differences & Intersections

Operation

myset = {'e', 'l', 'e', 'm', 'e', 'n', 't', 's'}
myset#{'e', 'l', 'm', 'n', 't', 's'}
yrset = {'e', 'l', 'e', 'p', 'h', 'a', 'n', 't', 's'}
yrset#{'e', 'l', 'f', 'a', 'n', 't', 's'}

myset = { }#It generates A DICTIONARY, NOT A SET!
mydict = {'coffee': 7, 'milk': 11, 'water': 20, 'wine': 'outofstock'}
OPERATION METHOD EXAMPLE RESULT
Generate set( )
set(list & string & tuple & dict)
{values}
myset = set()
set(mydict)
myset = {'e', 'l', 'e', 'm', 'e', 'n', 't', 's'}
{ }(a set)
{'coffee', 'milk', 'water', 'wine'}#(only the keys)
{'e', 'l', 'm', 'n', 't', 's'}
Union( ) set1.union(set2) myset.union(yrset)
Intersection(&) set1.intersection(set2) myset.intersection(yrset) {'e', 'l', 'n', 't', 's'}
Difference(set1 - set2) set1.difference(set2) myset.difference(yrset) {'m'}
Difference(set2 - set1) set2.difference(set1) yrset.difference(myset) {'f', 'a'}
Symmetric
Difference(^)
(Union - Intersection)
set1.symmetric_difference(set2) myset.union(yrset) {'m', 'f', 'a'}
Subset(<=) set1.issubset(set2) myset.issubset(yrset) False
Superset(>=) set1.issuperset(set2) myset.issuperset(yrset) False

↑ Up to the Top


Ndarray(numpy.ndarray)

Go to Numpy

Operation

import numpy as np
myarray = np.array([0, 1, 2.5, 4, 4.5])

↑ Up to the Top


← back to Main Page