Created
          July 28, 2021 17:43 
        
      - 
      
 - 
        
Save villares/548d95484dfc4dbad7b74d9f5ebe62f6 to your computer and use it in GitHub Desktop.  
    Exemplos de combinatória com itertools no Python
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from itertools import combinations | |
| from itertools import combinations_with_replacement # com repetição | |
| from itertools import permutations # a ordem importa | |
| from itertools import product # todos com todos (inclui repetições e a ordem importa) | |
| elementos = ("agua", "terra", "ar", "fogo") | |
| print(list(combinations(elementos, 2))) # 6 | |
| """ | |
| [('agua', 'terra'), ('agua', 'ar'), ('agua', 'fogo'), | |
| ('terra', 'ar'), ('terra', 'fogo'), ('ar', 'fogo')] | |
| """ | |
| print(list(combinations_with_replacement(elementos, 2))) # 10 | |
| """ | |
| [('agua', 'agua'), ('agua', 'terra'), ('agua', 'ar'), ('agua', 'fogo'), | |
| ('terra', 'terra'), ('terra', 'ar'), ('terra', 'fogo'), ('ar', 'ar'), | |
| ('ar', 'fogo'), ('fogo', 'fogo')] | |
| """ | |
| print(list(permutations(elementos, 2))) # 12 | |
| """ | |
| [('agua', 'terra'), ('agua', 'ar'), ('agua', 'fogo'),('terra', 'agua'), | |
| ('terra', 'ar'), ('terra', 'fogo'), ('ar', 'agua'), ('ar', 'terra'), | |
| ('ar', 'fogo'), ('fogo', 'agua'), ('fogo', 'terra'), ('fogo', 'ar')] | |
| """ | |
| print(list(product(elementos, repeat=2))) # 16 | |
| """ | |
| [('agua', 'agua'), ('agua', 'terra'), ('agua', 'ar'), ('agua', 'fogo'), | |
| ('terra', 'agua'), ('terra', 'terra'), ('terra', 'ar'), ('terra', 'fogo'), | |
| ('ar', 'agua'), ('ar', 'terra'), ('ar', 'ar'), ('ar', 'fogo'), | |
| ('fogo', 'agua'), ('fogo', 'terra'), ('fogo', 'ar'), ('fogo', 'fogo')] | |
| """ | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment