python 3.x - Mapping specific letters onto a list of words -


can please suggest approach write piece of codes automatically maps letter in letter_str onto dic_key (a dict key string type contains dashes match length of words in word_lst)?

so, mapping occurs if letter appears in every words in list @ same position no matter how many words in list.

if no letter appears @ position words in word list new_dic_key '----'. please see examples below

thanks

word_lst = ['ague', 'bute', 'byre', 'came', 'case', 'doze'] dic_key = '----' letters_str ='abcdefghijklmnopqrstuvwxyz'  new_dic_key = '---e' 

if

word_list = ['bute', 'byre']  new_dic_key = 'b--e' 

or

word_list = ['drek', 'drew', 'dyes']  new_dic_key = 'd-e-' 

if words in word_list of same length code give want:

word_list = ['drek', 'drew', 'dyes'] cols = [] in range(len(word_list[0])):     cols.append([])  word in word_list:     i, ch in enumerate(word):         cols[i].append(ch)  pattern = [item[0] if len(set(item)) == 1 else '-' item in cols] print ''.join(pattern) d-e- 

explanation:

we initialize cols list of list. contain 2 dimensional representation of letters in words of word_list. after populating cols looks like:

[['d', 'd', 'd'], ['r', 'r', 'y'], ['e', 'e', 'e'], ['k', 'w', 's']] 

so final result new_dic_key contain letter if elements in sub-list above have same letter, otherwise contain -. achieved using list comprehension pattern.

hope helps.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -