Skip to content

Instantly share code, notes, and snippets.

@iamjoross
Created March 10, 2017 13:18
Show Gist options
  • Save iamjoross/897cb3db55e405fe7c3513bbddc6bbe4 to your computer and use it in GitHub Desktop.
Save iamjoross/897cb3db55e405fe7c3513bbddc6bbe4 to your computer and use it in GitHub Desktop.
Python Collection Basics
#set
#------------------------------
#sets are sorted
#mutable like list
set([1,3,4])
type(set())
type({})
#add item in set
low_primes = {1,2,3}
low_primes.add(12)
low_primes.update({19, 4},{10,8})
#.remove() if values doesnt exist error
low_primes.remove(12)
#.discard() if value doesnt exit, it wont return error
low_primes.discard(12)
while(low_primes):
print(low_primes.pop())
#set math
# | or .union(*others) - all of the items from all of the sets
# & or .intersection(*others) - all of the common items between all of the sets
# - or .difference(*others) - all of the items in the first set that are not in the other sets
# ^ or .symmetric_difference(other) - all of the items that are not shared by the two sets
set1 = set(range(10))
set2 = {1,2}
set1.union(set2)
set1 | set2
COURSES = {
"Python Basics": {"Python", "functions", "variables",
"booleans", "integers", "floats",
"arrays", "strings", "exceptions",
"conditions", "input", "loops"},
"Java Basics": {"Java", "strings", "variables",
"input", "exceptions", "integers",
"booleans", "loops"},
"PHP Basics": {"PHP", "variables", "conditions",
"integers", "floats", "strings",
"booleans", "HTML"},
"Ruby Basics": {"Ruby", "strings", "floats",
"integers", "conditions",
"functions", "input"}
}
def covers(single_parameter):
empty_list = []
for keys, values in COURSES.items():
if single_parameter.union(values):
empty_list.append(keys)
return empty_list
def covers_all(single_parameter):
new_empty_list = []
for keys, values in COURSES.items():
if single_parameter.issubset(values):
new_empty_list.append(keys)
return new_empty_list
#TUPLES
#----------------------------------
course_minutes = {"Python": 232, "Java": 345, "PHP": 343}
for course, minutes in course_minutes.items():
print("{} is {} minutes long".format(course,minutes));
for index, letter in enumerate("abcdefghj"):
print("{}:{}".format(index + 1, letter))
def combo(iterable1,iterable2):
tuple_list= []
for idx in range(len(iterable1)):
tuple_list.append((iterable1[idx],iterable2[idx]))
return tuple_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment