<jupyter_start><jupyter_text>Steelers, Cowboys, and Bears Using Sets to Explore American Football Teams How Many Players Have Been on Each Team?<jupyter_code>%matplotlib inline
# Python magic to display in the web browser
!pip install matplotlib_venn
import pickle
from matplotlib_venn import venn2_circles, venn2, venn3
import matplotlib.pyplot as plt
with open("./football.pickle","rb") as f0:
    teams = pickle.load(f0)<jupyter_output><empty_output><jupyter_text>``teams`` is a [dictionary](https://docs.python.org/3/tutorial/datastructures.htmldictionaries) with* **keys** equal to name of football teams* **values** equal to a [**list**](https://docs.python.org/3.4/tutorial/introduction.htmllists) of all the players that have played on those teams<jupyter_code>type(teams)
teams.keys()
type(teams['steelers'])<jupyter_output><empty_output><jupyter_text>Let's look at the list of Steelers players  Size of a collectionEvery Python collection has a size (length) associated with it. This is a measure of how many objects are in the collection. This is accessed through the **``len()``** function. (Remember our definition of a function: it takes something in (in this case a collection) and returns something (in this case the length of the collection).<jupyter_code>print(len(teams))
print(len(teams['steelers']))<jupyter_output>3
1668<jupyter_text>We can create a **set** from the **list** of Steelers players Python has a ``set()`` function that takes another type of collection (e.g. a list) and creates a set<jupyter_code>steelers_set = set(teams['steelers'])
len(steelers_set)<jupyter_output><empty_output><jupyter_text>Why are the sizes (lengths) of the two sets different?* Lists do not have to have unique elements, but sets do (definition of a set). There are two names that two sets of players shared. Python Aside finding the duplicate playersWe're focusing on Python [**sets**](https://docs.python.org/3.4/tutorial/datastructures.htmlsets), but here is a way to find the names using a Python [**Counter**](https://docs.python.org/3/library/collections.htmlcollections.Counter). Quite simply, Counters count things. They can then return the most commonly occuring items that they counted.<jupyter_code>from collections import Counter

steelers_count = Counter(teams['steelers'])
steelers_count.most_common(10)<jupyter_output><empty_output><jupyter_text>So there have been two <NAME> (who would have thought!) and two <NAME> (not so surprising). Or perhaps they were n the team at two seperate times.  Now let's make sets for the other two teams<jupyter_code>bears_set = set(teams['bears'])
cowboys_set = set(teams['cowboys'])

len(bears_set),len(cowboys_set)<jupyter_output><empty_output><jupyter_text>Let's look at the sets with a Venn diagram<jupyter_code>venn3([steelers_set, cowboys_set, bears_set],
      ("Steelers","Cowboys","Bears"))<jupyter_output><empty_output><jupyter_text>Anything Seem Suspicious About These Data?* The [Chicago Bears](https://en.wikipedia.org/wiki/Chicago_Bears) are a very old football team, dating back to 1919. Do we really believe that there have been eight times as many [Steelers](https://en.wikipedia.org/wiki/Pittsburgh_Steelers) (founded 1933) and over four times as many [Cowboys](https://en.wikipedia.org/wiki/Dallas_Cowboys) (founded 1960)?    * Maybe something problematic with our Wikipedia data?     * Check against another source?  Which Steelers have also been Cowboys or Bears?This question involves * three sets (Steelers, Cowboys, and Bears)* two set operations    * "have also been" $\rightarrow$ **AND** $\rightarrow$ **INTERSECTION**    * "or" $\rightarrow$ **UNION** We can write this out in set notation$ \text{Steelers} \cap (\text{Cowboys} \cup \text{Bears})$<jupyter_code>steelers_on_other_teams = steelers_set.intersection(
                               bears_set.union(cowboys_set))
print("There have been %d Steelers that have played on other teams."\
      %len(steelers_on_other_teams))
print("These Steelers are")
print(steelers_on_other_teams)<jupyter_output><empty_output><jupyter_text>What players have played on all three teams?This question involves three sets (Steelers, Cowboys, Bears) and two Set operations: **Intersection** and **Intersection.** Writing this in set notation$ \text{Steelers} \cap \text{Cowboys} \cap \text{Bears}$<jupyter_code>steelers_set.intersection(bears_set.intersection(cowboys_set))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10744920993227991, 'fraction_numerical': 0.006997742663656885, 'mean_word_length': 6.158319870759289, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '243207', 'license': 'MIT', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1356, 'n_words': 448, 'path': 'Football.ipynb', 'repo_name': 'dychangfeng/my_decart_camp', 'size': '4440'}