2

I am new to Python and am attempting to parse a list of node paths into each pair presented in the path. For example:

I have a list (for nodes x, y, and z) that looks like this

list = ['xyzx', 'xzyx', 'zxyz', 'zyxz', 'yxzy', 'yzxy']

I am able to split each string at an arbitrary spot, but I need to split them into overlapping ordered pairs to get something like this:

newList = [('xy', 'yz', 'zx'), ('xz', 'zy', 'yx'), etc..]

or an individual list for each permutation would work as well:

newList1 = ['xy', 'yz', 'zx']
newList1 = ['xz', 'zy', 'yx']
etc..

Any ideas?

HSskillet
  • 63
  • 6

3 Answers3

2

You can generate them with a list comprehension, as:

l = ['xyzx', 'xzyx', 'zxyz', 'zyxz', 'yxzy', 'yzxy']

[tuple(s[i:i+2] for i in range(len(s)-1)) for s in l]

# [('xy', 'yz', 'zx'), ('xz', 'zy', 'yx'),
#  ('zx', 'xy', 'yz'), ('zy', 'yx', 'xz'),
#  ('yx', 'xz', 'zy'), ('yz', 'zx', 'xy')]

Note that you should avoid naming your list "list", as this is a Python builtin function.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • Works like a charm, thanks @Thierry Lathuille. Ha, rookie mistake on "list" thanks for pointing that out. – HSskillet Apr 29 '17 at 20:12
0

You could use python list comprehension. For a string s:

[s[i:i+2] for index in range(len(s) - 1)]

s[i:i+2] takes a substring from i to i+2 non inclusive.

Gcmalloc
  • 538
  • 5
  • 8
0

You could use this:

def get_overlapping_pairs(string):
    ret = [] # we will return this later
    for i in range(len(string)-1): # loop through all indices except the last one
        ret.append(string[i:i+2]) # append to ret the two characters
                                  # beginning at that index
    return tuple(ret) # convert to tuple

def get_overlapping_pairs_for_each(stringlist):
    ret = []
    for string in stringlist: # loop through each string
        ret.append(get_overlapping_pairs(string)) # append the overlapping
                                                  # pairs for that string
    return ret

Notice that we didn't look at the last index in the loop in the first function. That's because if we did, it would generate an IndexError when we try to look at the two characters beginning at that index -- there's only one character left by the time we get there.

Anonymous1847
  • 2,568
  • 10
  • 16