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

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 :: pd dataframe get column names 
Python :: pytest parametrize 
Python :: python sqlite dict 
Python :: pandas series to numpy array 
Python :: install lz4 python 3 
Python :: clear cookies selenium python 
Python :: implicit conversion in python example 
Python :: convert x unicode utf 8 bytes to u python 
Python :: dice rolling simulator python 
Python :: python if string is null or whitespace 
Python :: dict.fromkeys with list as value 
Python :: convert pandas column type 
Python :: sort df by column 
Python :: pathlib path get directory of current file 
Python :: Flatten List in Python Using List Comprehension 
Python :: reverse python dict 
Python :: pipilika search engine 
Python :: timeit jupyter 
Python :: pandas reorder columns by name 
Python :: promote a row in panda dataframe to header 
Python :: python reverse linked list 
Python :: pandas find location of values greater than 
Python :: python how to use input 
Python :: finding the index of an item in a pandas df 
Python :: apostrophe in python 
Python :: how to add element at first position in array python 
Python :: pandas groupby percentile 
Python :: Math Module sqrt() Function in python 
Python :: django creating calculated fields in model 
Python :: python warning 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =