Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

word pattern in python

def wordPattern(pattern, s):
    dict1 = {}
    dict2 = {}

    for key,value in enumerate(list(s.split(" "))):
        if value not in dict1:
            dict1[value]= [key]
        else:
            dict1[value].append(key)

    for key,value in enumerate(pattern):
        if value not in dict2:
            dict2[value]= [key]
        else:
            dict2[value].append(key)

    return (list(dict1.values()) == list(dict2.values()))
Comment

word pattern python

import string

def word_to_pattern(word):
    # Stores actual-letter to pattern-placeholder mapping
    mapping = {}

    # ZYXW... so we cap pop letters starting with A from the end
    available_pattern_letters = list(string.ascii_uppercase)[::-1]

    pattern = []
    for letter in word.upper():
        if letter not in string.ascii_uppercase:
            # for punctuation etc
            pattern.append(letter)
            continue
        if letter not in mapping:
            # new letter we haven't seen yet in this word
            mapping[letter] = available_pattern_letters.pop()
        pattern.append(mapping[letter])
    return pattern.join("")
Comment

PREVIOUS NEXT
Code Example
Python :: pygame change icon 
Python :: revesing case python 
Python :: send email with python 
Python :: list to set keep order python 
Python :: where to find python interpreter 
Python :: print a to z in python 
Python :: filter function using lambda in python 
Python :: how to close the window in pygame 
Python :: unique words from pandas 
Python :: python strip multiple characters 
Python :: django message framework 
Python :: python counter to list of tuples 
Python :: suppress warning jupyter notebook 
Python :: discord.py get a bot online 
Python :: python boxplot legend 
Python :: numpy stdev 
Python :: networkx create graph from dataframe 
Python :: df to np array 
Python :: get variance of list python 
Python :: python añadir elementos a una lista 
Python :: chrome selenium python 
Python :: python multiply list bt number 
Python :: python -m pip install 
Python :: how to make index column as a normal column 
Python :: select columns from dataframe pandas 
Python :: print items in object python 
Python :: np replace nan 
Python :: append to csv python 
Python :: create a vector of zeros in r 
Python :: python get object attribute by string 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =