How to search a list of tuples

I have a list of tuples. For example, I have the following:

a=[('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'),('an', 'o'),('an', 'o'),('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig', 'p')] 

And another list,

 b = ['k','h','p'] 

I would like to find a pattern in list b from the second element of list a.

In the above example, the output should return,

 [('jamy','Park','kick'),('car','rock','pig')] 

Can someone help me achieve my goals?

+6
source share
5 answers
 c = [(a[x][0], a[x+1][0], a[x+2][0]) for x, _ in enumerate(a) if a[x][1] == b[0] and a[x+1][1] == b[1] and a[x+2][1] == b[2]] 
+1
source

Assuming length b is 3, the following code will work even if a contains 'k','h','p' so that they don't always follow each other properly, as in:

 a=[('test', 'k'), ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('car', 'k'), ('an', 'r'),('rock', 'h'), ('see','k'), ('it','h'),('an', 'o'),('works', 'p')] b = ['k','h','p'] 

will produce:

 [('jamy', 'Park', 'kick'), ('see', 'it', 'works')] 

Code:

 letters_a = "".join(str(tup[1]) for tup in a) letters_b = "".join(str(letter) for letter in b) regex = re.compile(r'(%s)[^%s]*(%s)[^%s]*(%s)' % (letters_b[0],letters_b[:2],letters_b[1],letters_b,letters_b[2])) #for this example, the above line translates to: #regex = re.compile(r'(k)[^kh]*(h)[^khp]*(p)') match = re.finditer(regex, letters_a) results=[] for m in match: first,second,third = m.start(1), m.start(2), m.start(3) results.append((a[first][0],a[second][0],a[third][0])) print results 
+1
source

Try this snippet.

 list_of_values = [ ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'), ('an', 'o'), ('an', 'o'), ('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig', 'p') ] pattern = ('k','h','p') # Important part matches = [ values for values, keys in ( zip(*list_of_values[i:i + len(pattern)]) for i in range(len(list_of_values) - len(pattern) + 1) ) if keys == pattern ] print(matches) >> [('jamy', 'Park', 'kick'), ('car', 'rock', 'pig')] 
+1
source
 a = [('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'), ('an', 'o'), ('an', 'o'), ('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig', 'p')] b = ['k', 'h', 'p'] filtered = [ele for ele in a if ele[1] in b] def split_list(_list, idx_range): _t = [] _temp = [] _d = {idx + 1: ele for idx, ele in enumerate(_list)} for k in _d: if k % idx_range == 0: _t.append(_d[k]) _temp.append(_t) _t = [] else: _t.append(_d[k]) return _temp _nested = split_list(filtered, len(b)) _l1 = [] for outer in _nested: _s = '' for inner in outer: _s += inner[1] _l1.append(_s) _l2 = [_nested[idx] for idx, ele in enumerate(_l1) if ''.join(b) == ele] final = [] for ele in _l2: tup = [e[0] for e in ele] final.append(tuple(tup)) print final 

Output:

 [('jamy', 'Park', 'kick'), ('car', 'rock', 'pig')] 
+1
source

Given:

 a = [ ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'), ('an', 'o'), ('an', 'o'), ('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig', 'p') ] b = ('k','h','p') 

If your goal is to assemble groups of tuples from list a based on the second element occurring in the same order as the sequence from b , you can do this:

 result=[] for sl in [a[i:i+len(b)] for i in range(0,len(a))]: if tuple([tp[1] for tp in sl])==b: result.append(tuple(tp[0] for tp in sl)) print result # [('jamy', 'Park', 'kick'), ('car', 'rock', 'pig')] 
0
source

Source: https://habr.com/ru/post/956801/


All Articles