tuples - python list reverse and merge -


i have these 2 lists

a = [(0,1), (1,2), (3,4)] b = [[(0,1), (6,7), (8,9)], [(4,5), (7,15), (20,25)], [(18,17), (3,4)]] 

what need check if first tuple in exists in b merge reverse expected output is

ab = [[(3,4),(1,2),(0,1),(6,7), (8,9)], [(4,5), (7,15), (20,25)], [(18,17),(3,4)]] 

my sample code is:

import copy ab = copy.deepcopy(b)  coord in b:     if a[0] in coord:         #print "yes", coord.index(a[0])         ab.insert(coord.index(a[0]), a[::-1])  print ab 

but doesn't give me output want. out there can help? thanks

use list comprehension rebuild b:

ab = [a[:0:-1] + sub if a[0] in sub else sub sub in b] 

the a[:0:-1] slice not reverses a, excludes first element of a prevent duplicates in output:

>>> = [(0,1), (1,2), (3,4)] >>> b = [[(0,1), (6,7), (8,9)], [(4,5), (7,15), (20,25)], [(18,17), (3,4)]] >>> [a[:0:-1] + sub if a[0] in sub else sub sub in b] [[(3, 4), (1, 2), (0, 1), (6, 7), (8, 9)], [(4, 5), (7, 15), (20, 25)], [(18, 17), (3, 4)]] 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -