\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{gonz95alo} \pdfinfo{ /Title (data-structures-and-methods.pdf) /Creator (Cheatography) /Author (gonz95alo) /Subject (Data structures and methods Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{B8B339} \definecolor{LightBackground}{HTML}{FAFAF2} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Data structures and methods Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{gonz95alo} via \textcolor{DarkBackground}{\uline{cheatography.com/152363/cs/33442/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}gonz95alo \\ \uline{cheatography.com/gonz95alo} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 18th October, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{3.68 cm} x{4.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Lists (and strings)}} \tn % Row 0 \SetRowColor{LightBackground} Concatenation & `my\_list = list\_1 + list\_2` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Append to the end & \seqsplit{`my\_list.append(new\_element)`} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Check if in list & `element in my\_list` \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} Find in list & `position = \seqsplit{my\_list.index(element)`} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Remove last element & `my\_list.pop()` \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} Insert element at position & \seqsplit{`my\_list.insert(position}, element)` \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} Remove from list (by position) & \seqsplit{`my\_list.pop(position)`} \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} Remove from list (by element) & \seqsplit{`my\_list.remove(element)`} \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} Sort & `my\_list.sort()` \tn % Row Count 16 (+ 1) % Row 9 \SetRowColor{white} Reverse order & `my\_list.reverse()` \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} Count elements & `number = \seqsplit{my\_list.count(element)`} \tn % Row Count 19 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{The same operations apply to strings, understood as lists of characters.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sets}} \tn % Row 0 \SetRowColor{LightBackground} Add element & `my\_set.add(element)` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Remove random element & `my\_set.pop()` \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Remove specific element & \seqsplit{`my\_set.remove(element)`} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Add elements from other iterable & \seqsplit{`my\_set.update(iterable)`} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Union, intersection, difference & `new\_set = \seqsplit{my\_set.union(other\_set)} \{\{nl\}\} new\_set = \seqsplit{my\_set.intersection(other\_set)} \{\{nl\}\} new\_set = my\_set.difference(other\_set)\}` \tn % Row Count 13 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} List of keys or values & `keys = my\_dict.keys()` \{\{nl\}\} `values = my\_dict.values()` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} List of tuples (key, value) & `items = my\_dict.items()` \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} Update dictionary (or join two) & `my\_dict.update(\{key: value\})` \{\{nl\}\} \seqsplit{`my\_dict.update(other\_dict)`} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} Remove item by key & `my\_dict.pop(key)` \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} Remove last added item & `my\_dict.popitem()` \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} Copy dictionary & `new\_dict = my\_dict.copy()` \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.72 cm} x{3.28 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List/string slicing}} \tn % Row 0 \SetRowColor{LightBackground} First/last n elements & `my\_list{[}:n{]}`, `my\_list{[}n:{]}` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} From element n to element m, with step s & `my\_list{[}n:m:s{]}` \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comprehensions}} \tn % Row 0 \SetRowColor{LightBackground} List comprehension & `lc = {[}n ** 2 for n in range(10) if n \% 2 == 0{]}` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Set comprehension & `sc = \{n ** 2 for n in range(10) if n \% 2 == 0\}` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Dictionary comprehension & `dc = \{n: n ** 2 for n in range(10) if n \% 2 == 0\}` \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} Generator comprehension & `gc = (n ** 2 for n in range(10) if n \% 2 == 0)` \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.48 cm} x{5.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Tuples}} \tn % Row 0 \SetRowColor{LightBackground} Count elements & `number = \seqsplit{my\_tuple.count(element)`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Element in tuple & `element in my\_tuple` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Find in tuple & `position = \seqsplit{my\_tuple.index(element)`} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.08 cm} x{3.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Advanced iterables}} \tn % Row 0 \SetRowColor{LightBackground} Import relevant modules & `import collections` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Dictionary with default values & `def\_dict = \seqsplit{collections.defaultdict()`} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Dictionary remembering the order of items & `ord\_dict = \seqsplit{collections.OrderedDict()`} \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Numpy arrays}} \tn % Row 0 \SetRowColor{LightBackground} Relevant modules & `import numpy as np` \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Itertools module}} \tn % Row 0 \SetRowColor{LightBackground} Import relevant modules & `import itertools` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Cycle infinitely through iterable elements & \seqsplit{`itertools.cycle(iterable)`} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Loop through the cumulative sum of the iterable & `itertools.accumulate({[}1,1,3,4{]})` \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} Loop through pairwise contiguous elements & \seqsplit{`itertools.accumulate(iterable)`} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} Cartesian product of iterables & \seqsplit{`itertools.product(iter\_a}, iter\_b)` \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} All permutations of an iterable & \seqsplit{`itertools.permutations(iterable)`} \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} Length-r combinations from an iterable & \seqsplit{`itertools.combinations(iterable)`} \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{In the `accumulate()` function's argument, the iterable must contain elements which can be summed together. In other words, it can't have strings and integers at the same time as they can't be summed.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}