如何在每次出现特定元素之间按字母顺序排列列表元素?

时间:2018-12-20 21:09:57

标签: python python-2.7 list

我已经在这个特定问题上工作了几个小时,但并没有提出太多建议。

假设我有

AbsoluteLayout

如何在每个<AbsoluteLayout> <Image Source="Background.jpg" Aspect="AspectFill" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" /> <StackLayout AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All"> <Label Text="Timetable" TextColor="Silver" HorizontalOptions="EndAndExpand" Margin="0, 10, 20, 0"> </Label> <Image Margin="15, 20" HorizontalOptions="Center" WidthRequest="350" Source="subtle-logo.png"></Image> <Image HorizontalOptions="Center" x:Name="PlayPauseButton" Source="play.png" WidthRequest="75"> </Image> <Image HorizontalOptions="Center" x:Name="shareButton" Source="share-button.png" WidthRequest="50" VerticalOptions="EndAndExpand" Margin="0, 0, 0, 20"> </Image> </StackLayout> </AbsoluteLayout> 之间按字母顺序对a = ['ot=apple', 'zz=top', 'par=four', 'xyz_test=wff', 'sesh=232d23f23f33ff', '\n', 'xt=cal', 'poss=33', '\n', 'val=fifty', 'dvx=32f23f2', '\n','dsfad=www', 'orr=dsav'] b = '\n' 进行排序?

即,我该如何退货:

a

我已经成功使用:

b

对其他列表进行排序,但是在这种情况下,它取决于a = ['ot=apple', 'par=four', 'sesh=232d23f23f33ff','xyz_test=wff', 'zz=top', '\n', 'poss=33','xt=cal', '\n', 'dvx=32f23f2','val=fifty', '\n','dsfad=www', 'orr=dsav'] 的出现,因此我不确定该怎么做。

2 个答案:

答案 0 :(得分:3)

您可以将一维列表分成列表列表-每次'\n'出现时,您都会启动一个新的内部列表-然后对内部列表进行排序并重新组合它们:

a = ['ot=apple', 'zz=top', 'par=four', 'xyz_test=wff', 'sesh=232d23f23f33ff', 
     '\n', 'xt=cal', 'poss=33', '\n', 'val=fifty', 'dvx=32f23f2', 
     '\n','dsfad=www', 'orr=dsav']

b = '\n'

# partition your data into sublists
stacked = [[]]
for k in a:
    if k == b:
        stacked.append([])
    else:
        stacked[-1].append(k)

# remove empty list at end if present
if not stacked[-1]:
    stacked = stacked[:-1]

# sort each inner list
for sublist in stacked:
    sublist.sort()

# unstack again
retval = []
for k in stacked:
    retval.append(b) # add a \n
    retval.extend(k) # extend with the sublist

# remove the first \n
retval = retval[1:]

print a 
print stacked 
print retval 

输出:

# a
['ot=apple', 'zz=top', 'par=four', 'xyz_test=wff', 'sesh=232d23f23f33ff', '\n', 'xt=cal', 
 'poss=33', '\n', 'val=fifty', 'dvx=32f23f2', '\n', 'dsfad=www', 'orr=dsav']

# stacked 
[['ot=apple', 'par=four', 'sesh=232d23f23f33ff', 'xyz_test=wff', 'zz=top'], ['poss=33', 'xt=cal'], 
 ['dvx=32f23f2', 'val=fifty'], ['dsfad=www', 'orr=dsav']]

# retval
['ot=apple', 'par=four', 'sesh=232d23f23f33ff', 'xyz_test=wff', 'zz=top', '\n', 
 'poss=33', 'xt=cal', '\n', 'dvx=32f23f2', 'val=fifty', '\n', 'dsfad=www', 'orr=dsav']

答案 1 :(得分:1)

# create sublist
c = []
temp = []

for aa in a:
    if aa != b:
        temp += [aa]
    else:
        c += [temp]
        temp = []
        c += [b]
#sort and unravel
c = [sorted(i) for i in c]
d = [j for i in c for j in i]

print(d)
['ot=apple',
 'par=four',
 'sesh=232d23f23f33ff',
 'xyz_test=wff',
 'zz=top',
 '\n',
 'poss=33',
 'xt=cal',
 '\n',
 'dvx=32f23f2',
 'val=fifty',
 '\n']