Swarm Intelligence from social interaction
You can pass functions as variables
def add_two(x):
return x+2
def do_something_then_add_three(something_to_do_first, x):
# first call something_to_do_first with the input, then add 3
return something_to_do_first(x) + 3
# We pass add_two (note the lack of brackets beside it)
do_something_then_add_three(add_two, 0)
5
An iterator is an object representing a stream of data. You can call next
on an iterator to get the next value.
from string import ascii_lowercase
ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
iter(ascii_lowercase)
<str_iterator at 0x7fad0bcb2d10>
alphabet_iterator = iter(ascii_lowercase)
next(alphabet_iterator)
'a'
next(alphabet_iterator)
'b'
def add_underscore(x,y):
return f'{x}_{y}'
import itertools
alphabet_iterator = iter(ascii_lowercase)
my_list = [1,3,5,6]
list(itertools.islice(my_list, 3))
[1, 3, 5]
Say we want to create a list of tuples with ( position_in_alphabet, letter ) starting at (1, 'a') like
[(1, 'a'), (2, 'b'), (3, 'c')]
[(1, 'a'), (2, 'b'), (3, 'c')]
alphabet_tuples = []
for i in range(len(ascii_lowercase)):
alphabet_tuples.append((i+1, ascii_lowercase[i]))
alphabet_tuples
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'i'), (10, 'j'), (11, 'k'), (12, 'l'), (13, 'm'), (14, 'n'), (15, 'o'), (16, 'p'), (17, 'q'), (18, 'r'), (19, 's'), (20, 't'), (21, 'u'), (22, 'v'), (23, 'w'), (24, 'x'), (25, 'y'), (26, 'z')]
list(zip(count(start=1), ascii_lowercase))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'i'), (10, 'j'), (11, 'k'), (12, 'l'), (13, 'm'), (14, 'n'), (15, 'o'), (16, 'p'), (17, 'q'), (18, 'r'), (19, 's'), (20, 't'), (21, 'u'), (22, 'v'), (23, 'w'), (24, 'x'), (25, 'y'), (26, 'z')]
import IPython.display as ipd
import numpy as np
import itertools
import itertools
def ascii_wav(times=3):
wave = "°º¤ø,¸¸,ø¤º°`"
return ''.join(itertools.repeat(wave, times))
ascii_wav()
'°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`'