How to add a variable to a dictionary in Python? - python
I want to add on to my current dictionary without hard-coding. I want to distinguish between stores by adding on -A and -B based on the station someone is working in.
a_dict = {'A': [['LA', 'Sallys', 'Associate '], ['Hollywood', 'Tonys', 'Shelf'], ['Compton', 'Sally', 'Shelves']],'B': [['SAC', 'Sallys', 'Associate '], ['Townsland', 'Tonys', 'Shelf'], ['Compton', 'Tiffanys', 'Shelves']]}
b_dict = {'Site':"", 'Store':"", 'Station':""}
for key in a_dict:
b_dict.update(a_dict)
#print(b_dict[key[0]])
#print(value[0])
output = [
{'Site':val[0][0], 'Store':val[1][1], 'Station':val[2]}
for vals in a_dict.values()
for val in vals
]
print(output)
The code currently prints out this:
[{'Site': 'L', 'Store': 'a', 'Station': 'Associate '}, {'Site': 'H', 'Store': 'o', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'a', 'Station': 'Shelves'}, {'Site': 'S', 'Store': 'a', 'Station': 'Associate '}, {'Site': 'T', 'Store': 'o', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'i', 'Station': 'Shelves'}]
[{'Site': 'L', 'Store': 'a', 'Station': 'Associate '}, {'Site': 'H', 'Store': 'o', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'a', 'Station': 'Shelves'}, {'Site': 'S', 'Store': 'a', 'Station': 'Associate '}, {'Site': 'T', 'Store': 'o', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'i', 'Station': 'Shelves'}]
But I want it to print out this:
[{'Site': 'L', 'Store': 'a-A', 'Station': 'Associate '}, {'Site': 'H', 'Store': 'o-B', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'a-B', 'Station': 'Shelves'}, {'Site': 'S', 'Store': 'a-A', 'Station': 'Associate '}, {'Site': 'T', 'Store': 'o-B', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'i-B', 'Station': 'Shelves'}]
[{'Site': 'L', 'Store': 'a-A', 'Station': 'Associate '}, {'Site': 'H', 'Store': 'o-B', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'a-B', 'Station': 'Shelves'}, {'Site': 'S', 'Store': 'a-A', 'Station': 'Associate '}, {'Site': 'T', 'Store': 'o-B', 'Station': 'Shelf'}, {'Site': 'C', 'Store': 'i-B', 'Station': 'Shelves'}]
So like if the associate is shelf or shelves than the store would be -B if not the store should be -A.
Here you go.
a_dict = {'A': [['LA', 'Sallys', 'Associate '], ['Hollywood', 'Tonys', 'Shelf'], ['Compton', 'Sally', 'Shelves']],'B': [['SAC', 'Sallys', 'Associate '], ['Townsland', 'Tonys', 'Shelf'], ['Compton', 'Tiffanys', 'Shelves']]}
b_dict = {'Site':"", 'Store':"", 'Station':""}
for key in a_dict:
b_dict.update(a_dict)
#print(b_dict[key[0]])
#print(value[0])
output = [
{'Site':val[0][0], 'Store':val[1][1], 'Station':val[2]}
for vals in a_dict.values()
for val in vals
]
for x in output:
if x["Station"] in ("Shelf","Shelves"):
x["Store"] += "-B"
else:
x["Store"] += "-A"
print(output)
You said what you needed to do in the last line of your question.
Related
how to get all value in list in same time
following show the my list value (variable is "data") [{'letters': ['R', 'V', 'X', 'U', 'M', 'Z', 'B', 'O', 'R'], 'words': ['RVX', 'BOM', 'RUB', 'RUZ', 'MBOOO', 'RMR'], 'score': 51}, {'letters': ['P', 'X', 'M', 'R', 'D', 'S', 'I', 'C', 'E'], 'words': ['PXM', 'RDS', 'ICE', 'PRI', 'DSCE', 'PXM', 'MRE'], 'score': 54}] Then I used the following code print(*data) output was {'letters': ['R', 'V', 'X', 'U', 'M', 'Z', 'B', 'O', 'R'], 'words': ['RVX', 'BOM', 'RUB', 'RUZ', 'MBOOO', 'RMR'], 'score': 51} {'letters': ['P', 'X', 'M', 'R', 'D', 'S', 'I', 'C', 'E'], 'words': ['PXM', 'RDS', 'ICE', 'PRI', 'DSCE', 'PXM', 'MRE'], 'score': 54} but I want to get that output with comma with saparate two JSON Like this {'letters': ['R', 'V', 'X', 'U', 'M', 'Z', 'B', 'O', 'R'], 'words': ['RVX', 'BOM', 'RUB', 'RUZ', 'MBOOO', 'RMR'], 'score': 51}, {'letters': ['P', 'X', 'M', 'R', 'D', 'S', 'I', 'C', 'E'], 'words': ['PXM', 'RDS', 'ICE', 'PRI', 'DSCE', 'PXM', 'MRE'], 'score': 54} Any one can help me Thank you
Is this what you are trying to accomplish? print(*data, sep=",\n") That will put your items on separate lines, with commas after all but the last.
You can try this: print(*data, sep=',')
Updating dictionaries of list from another dictionary python
I have two list of dictionaries: I am trying to compare test2 with test1 and update accordingly. test1 = [{'names': ['a', 'b', 'c'], 'country': 'USA', 'state': 'Texas'}, {'names': ['d', 'e', 'f'], 'country': 'Australia', 'state': 'Melbourne'}, {'names': ['i', 'j', 'k'], 'country': 'canada', 'state': 'Toronto'}, {'names': ['l', 'm', 'n'], 'country': 'Austria', 'state': 'Burgenland'}] test2 = [{'code': 4286, 'list_of_countries': ['USA', 'Australia', 'Colombia', 'Denmark', 'Greece', 'Iceland']}, {'code':4287, 'list_of_countries': ['Texas', 'Argentina', 'Austria', 'Bangladesh', 'canada']}] Expected Output: test2 = [{'names':['a', 'b', 'c', 'd', 'e', 'f'], 'country': ['USA', 'Australia'], 'state': ['Texas', 'Melbourne'], 'code':4286}, {'names':['i', 'j', 'k', 'l', 'm', 'n'], 'country': ['canada', 'Austria'], 'state': ['Toronto','Burgenland'], 'code':4287}] Tried below snippet: By searching the test1 country in test2 list_of_countries: for i in test1: for j in test2: a = [] if i.get('country') in j.get('list_of_countries'): a.append({'country':i.get('country'), 'state':i.get('state'}) j.update(a)
You can transform test2 to a dictionary, associating each entry in list_of_countries with their proper key. Then, you can use this result for grouping: test2 = [{'code': 4286, 'list_of_countries': ['USA', 'Australia', 'Colombia', 'Denmark', 'Greece', 'Iceland']}, {'code': 4287, 'list_of_countries': ['Texas', 'Argentina', 'Austria', 'Bangladesh', 'canada']}] test1 = [{'names': ['a', 'b', 'c'], 'country': 'USA', 'state': 'Texas'}, {'names': ['d', 'e', 'f'], 'country': 'Australia', 'state': 'Melbourne'}, {'names': ['i', 'j', 'k'], 'country': 'canada', 'state': 'Toronto'}, {'names': ['l', 'm', 'n'], 'country': 'Austria', 'state': 'Burgenland'}] d = {i:k['code'] for k in test2 for i in k['list_of_countries']} Now, you can create a series of defaultdicts associated with the country code. By looping over the country/state dicts in test1, you can keep a running update of the states and countries that are associated with each code: from collections import defaultdict new_d = dict(zip(d.values(), [defaultdict(list) for _ in d])) for i in test1: for a, b in i.items(): new_d[d[i['country']]][a].append(b) r = [{'code':a, **b, 'names':[j for k in b['names'] for j in k]} for a, b in new_d.items()] The final list comprehension transforms new_d to your desired format, a list of dictionaries. Output: [{'code': 4286, 'names': ['a', 'b', 'c', 'd', 'e', 'f'], 'country': ['USA', 'Australia'], 'state': ['Texas', 'Melbourne']}, {'code': 4287, 'names': ['i', 'j', 'k', 'l', 'm', 'n'], 'country': ['canada', 'Austria'], 'state': ['Toronto', 'Burgenland']}]
Python to convert RNA seq into single-letter Amino Acid sequence
I need some assistance in writing a code that will convert a given RNA nucleotide sequence into an Amino Acid sequence. I've currently been given 2 dictionaries to use: one of Amino Acid codons and their respective 3-letter codes, and one of the 3-letter codes and their corresponding 1-letter code. I need to write a code that will take a give RNA sequence and output the single letter code. Below I've included the 2 provided dictionaries. RNA_codon_table = { # U 'UUU': 'Phe', 'UCU': 'Ser', 'UAU': 'Tyr', 'UGU': 'Cys', # UxU 'UUC': 'Phe', 'UCC': 'Ser', 'UAC': 'Tyr', 'UGC': 'Cys', # UxC 'UUA': 'Leu', 'UCA': 'Ser', 'UAA': '---', 'UGA': '---', # UxA 'UUG': 'Leu', 'UCG': 'Ser', 'UAG': '---', 'UGG': 'Trp', # UxG # C 'CUU': 'Leu', 'CCU': 'Pro', 'CAU': 'His', 'CGU': 'Arg', # CxU 'CUC': 'Leu', 'CCC': 'Pro', 'CAC': 'His', 'CGC': 'Arg', # CxC 'CUA': 'Leu', 'CCA': 'Pro', 'CAA': 'Gln', 'CGA': 'Arg', # CxA 'CUG': 'Leu', 'CCG': 'Pro', 'CAG': 'Gln', 'CGG': 'Arg', # CxG # A 'AUU': 'Ile', 'ACU': 'Thr', 'AAU': 'Asn', 'AGU': 'Ser', # AxU 'AUC': 'Ile', 'ACC': 'Thr', 'AAC': 'Asn', 'AGC': 'Ser', # AxC 'AUA': 'Ile', 'ACA': 'Thr', 'AAA': 'Lys', 'AGA': 'Arg', # AxA 'AUG': 'Met', 'ACG': 'Thr', 'AAG': 'Lys', 'AGG': 'Arg', # AxG # G 'GUU': 'Val', 'GCU': 'Ala', 'GAU': 'Asp', 'GGU': 'Gly', # GxU 'GUC': 'Val', 'GCC': 'Ala', 'GAC': 'Asp', 'GGC': 'Gly', # GxC 'GUA': 'Val', 'GCA': 'Ala', 'GAA': 'Glu', 'GGA': 'Gly', # GxA 'GUG': 'Val', 'GCG': 'Ala', 'GAG': 'Glu', 'GGG': 'Gly' # GxG } singleletter = {'Cys': 'C', 'Asp': 'D', 'Ser': 'S', 'Gln': 'Q', 'Lys': 'K', 'Trp': 'W', 'Asn': 'N', 'Pro': 'P', 'Thr': 'T', 'Phe': 'F', 'Ala': 'A', 'Gly': 'G', 'Ile': 'I', 'Leu': 'L', 'His': 'H', 'Arg': 'R', 'Met': 'M', 'Val': 'V', 'Glu': 'E', 'Tyr': 'Y', '---': '*'}
You can do this with a list comprehension: [singleletter[RNA_codon_table[s[i:i+3]]] for i in range(0, len(s),3)] For example, >>> s = 'UUUGAUAGC' >>> [s[i:i+3] for i in range(0, len(s),3)] ['UUU', 'GAU', 'AGC'] >>> [RNA_codon_table[s[i:i+3]] for i in range(0, len(s),3)] ['Phe', 'Asp', 'Ser'] >>> [singleletter[RNA_codon_table[s[i:i+3]]] for i in range(0, len(s),3)] ['F', 'D', 'S'] Or, with BioPython: >>> from Bio.Seq import Seq >>> from Bio.Alphabet import IUPAC >>> s = Seq('UUUGAUAGC', IUPAC.unambiguous_rna) >>> s.translate() Seq('FDS', IUPACProtein())
remove items from list in python
>>> n ['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', '', '', '', '', '', 'AGC', 'S', '', '', '', '', '', 'GGC', 'G', 'UGA', 'Stop', '', '', 'CGA', 'R', '', '', '', '', '', 'AGA', 'R', '', '', '', '', '', 'GGA', 'G', 'UGG', 'W', '', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', ''] >>> for item in n: ... if item=='': ... n.remove(item) ... >>> n ['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', 'AGC', 'S', 'GGC', 'G', 'UGA', 'Stop', 'CGA', 'R', 'AGA', 'R', 'GGA', 'G', 'UGG', 'W', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', ''] How to explain that iterative "remove" operation can't remove all the '' elements in the list?
You can also use a list comprehension: >>> n = ['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', '', '', '', '', '', 'AGC', 'S', '', '', '', '', '', 'GGC', 'G', 'UGA', 'Stop', '', '', 'CGA', 'R', '', '', '', '', '', 'AGA', 'R', '', '', '', '', '', 'GGA', 'G', 'UGG', 'W', '', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', ''] >>> n = [x for x in n if x != ''] # or more simply [x for x in n if x] >>> print n ['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', 'AGC', 'S', 'GGC', 'G', 'UGA', 'Stop', 'CGA', 'R', 'AGA', 'R', 'GGA', 'G', 'UGG', 'W', 'CGG', 'R', 'AGG', 'R', 'GGG', 'G']
To expand a bit on why you shouldn't change a list this way as you're iterating over it, consider the following simplification of what you're doing: >>> a = range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for item in a: ... print item ... a.remove(item) ... 0 2 4 6 8 >>> a [1, 3, 5, 7, 9] As this illustrates, by changing the list, you're also changing which elements you act on in successive iterations. That's almost never a good idea and appears not to be what you're trying to accomplish. Instead, how about a list comprehension: [item for item in mylist if item != '']
Another option is while '' in n: n.remove('')
Instead of trying to remove the blanks, try instead to make a new array which contains only the non-blank items a = ['UUU', 'F', 'CUU', 'L', 'AUU', 'I', 'GUU', 'V', 'UUC', 'F', 'CUC', 'L', 'AUC', 'I', 'GUC', 'V', 'UUA', 'L', 'CUA', 'L', 'AUA', 'I', 'GUA', 'V', 'UUG', 'L', 'CUG', 'L', 'AUG', 'M', 'GUG', 'V', 'UCU', 'S', 'CCU', 'P', 'ACU', 'T', 'GCU', 'A', 'UCC', 'S', 'CCC', 'P', 'ACC', 'T', 'GCC', 'A', 'UCA', 'S', 'CCA', 'P', 'ACA', 'T', 'GCA', 'A', 'UCG', 'S', 'CCG', 'P', 'ACG', 'T', 'GCG', 'A', 'UAU', 'Y', 'CAU', 'H', 'AAU', 'N', 'GAU', 'D', 'UAC', 'Y', 'CAC', 'H', 'AAC', 'N', 'GAC', 'D', 'UAA', 'Stop', 'CAA', 'Q', 'AAA', 'K', 'GAA', 'E', 'UAG', 'Stop', 'CAG', 'Q', 'AAG', 'K', 'GAG', 'E', 'UGU', 'C', 'CGU', 'R', 'AGU', 'S', 'GGU', 'G', 'UGC', 'C', 'CGC', 'R', '', '', '', '', '', 'AGC', 'S', '', '', '', '', '', 'GGC', 'G', 'UGA', 'Stop', '', '', 'CGA', 'R', '', '', '', '', '', 'AGA', 'R', '', '', '', '', '', 'GGA', 'G', 'UGG', 'W', '', '', '', '', '', 'CGG', 'R', '', '', '', '', '', 'AGG', 'R', '', '', '', '', '', 'GGG', 'G', ''] b = [] for x in a: if len(x)>0: b.append(x)
As dylrei mentioned, list comprehension is a pythonic solution, but is has a drawback with (very) huge list since it copies the list. If you have a huge list and the item you want to remove is only a few times in the list, then I suggest the following (less pythonic) solution: >>> l=[1,2,3,4,5,6,1,2,3,1,2,1,1] >>> while True: ... try: ... l.remove(1) ... except ValueError: ... break ... >>> l [2, 3, 4, 5, 6, 2, 3, 2]
Splitting string into groups of specific length
I was wondering what the best way to split up a string such as "abcdefghijklmnopqrstuvwxyz" into a list of groups of 2 (splitting the string into: ["ab", "bc", "cd", ... "yz]) is in Python. Or what about in groups of 3: splitting the string into ["abc", "bcd", "cde", ... "xyz"] Thanks!
Here is one that works for any iterable >>> from itertools import tee, izip, islice >>> chunksize = 2 >>> s = 'abcdefghijklmnopqrstuvwxyz' >>> t = tee(s, chunksize) >>> for i, j in enumerate(t): ... next(islice(j, i, i), None) ... >>> ["".join(k) for k in izip(*t)] ['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi', 'ij', 'jk', 'kl', 'lm', 'mn', 'no', 'op', 'pq', 'qr', 'rs', 'st', 'tu', 'uv', 'vw', 'wx', 'xy', 'yz'] If s is always a str, this is more straight forward >>> [s[i: i + chunksize] for i in range(len(s) + 1 - chunksize)] ['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi', 'ij', 'jk', 'kl', 'lm', 'mn', 'no', 'op', 'pq', 'qr', 'rs', 'st', 'tu', 'uv', 'vw', 'wx', 'xy', 'yz']
This is a one-liner: def split_by_len(text, chunksize): return [text[i:(i+chunksize)] for i in range(len(text)-chunksize+1)]
def segment_string(s, segment_len): return [s[i:i+segment_len] for i in range(len(s) - (segment_len - 1))] >>> for i in range(5): print segment_string("abcdefghijklmnopqrstuvwxyz", i) ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] ['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi', 'ij', 'jk', 'kl', 'lm', 'mn', 'no', 'op', 'pq', 'qr', 'rs', 'st', 'tu', 'uv', 'vw', 'wx', 'xy', 'yz'] ['abc', 'bcd', 'cde', 'def', 'efg', 'fgh', 'ghi', 'hij', 'ijk', 'jkl', 'klm', 'lmn', 'mno', 'nop', 'opq', 'pqr', 'qrs', 'rst', 'stu', 'tuv', 'uvw', 'vwx', 'wxy', 'xyz'] ['abcd', 'bcde', 'cdef', 'defg', 'efgh', 'fghi', 'ghij', 'hijk', 'ijkl', 'jklm', 'klmn', 'lmno', 'mnop', 'nopq', 'opqr', 'pqrs', 'qrst', 'rstu', 'stuv', 'tuvw', 'uvwx', 'vwxy', 'wxyz']
>>> groups = zip(s, s[1:], s[2:]) >>> ["".join(g) for g in groups] ['abc', 'bcd', 'cde', 'def', 'efg', 'fgh', 'ghi', 'hij', 'ijk', 'jkl', 'klm', 'l mn', 'mno', 'nop', 'opq', 'pqr', 'qrs', 'rst', 'stu', 'tuv', 'uvw', 'vwx', 'wxy' , 'xyz']