Home > other >  To help solve an array
To help solve an array

Time:05-11

List=[{' level: 1, 'num' : 'a'},
{' level: 1, 'num' : 'ab'},
{' level: 1, 'num' : 'aasdf},
{' level: 2, 'num' : 'as'},
{' level: 2, 'num' : 'sda},
{' level: 3, 'num' : 'asd'},
{' level: 2, 'num' : 'SDWQW},
{' level: 1, 'num' : 'dqwee},
{' level: 1, 'num' : 'sre'},
{' level: 2, 'num' : 'SXZXC},
{' level: 2, 'num' : 'sxzasdxc},
{' level: 3, 'num' : 'SXZSXC},
{' level: 1, 'num' : 'sxasdzxc}]

Level 1 and begin with 'aa' elements, to the next level of 1 elements between delete all


The result is that
[{' level: 1, 'num' : 'a'},
{' level: 1, 'num' : 'ab'},
{' level: 1, 'num' : 'aasdf},
{' level: 1, 'num' : 'dqwee},
{' level: 1, 'num' : 'sre'},
{' level: 2, 'num' : 'SXZXC},
{' level: 2, 'num' : 'sxzasdxc},
{' level: 3, 'num' : 'SXZSXC},
{' level: 1, 'num' : 'sxasdzxc}]

CodePudding user response:

 LST=[{' level: 1, 'num' : 'a'}, 
{' level: 1, 'num' : 'ab'},
{' level: 1, 'num' : 'aasdf},
{' level: 2, 'num' : 'as'},
{' level: 2, 'num' : 'sda},
{' level: 3, 'num' : 'asd'},
{' level: 2, 'num' : 'SDWQW},
{' level: 1, 'num' : 'dqwee},
{' level: 1, 'num' : 'sre'},
{' level: 2, 'num' : 'SXZXC},
{' level: 2, 'num' : 'sxzasdxc},
{' level: 3, 'num' : 'SXZSXC},
{' level: 1, 'num' : 'sxasdzxc}]


S=None
For I, j in enumerate (LST) :
L=list (j.v alues ())
If l [0]==1 and l [1] [: 2]=='aa' :
S=I
Elif l [0]==1 and s!=None:
Del LST [I] s + 1:
Break

Print (LST)
  • Related