我已经看过了,但我只是想确认一下,考虑一下这个MWE:

data = ( ( "x1", ( (3, "a"), (1, "b"),  (5, "c") )  ), ( "x2", ( (2, "a"), (4, "b") )  ) )

outputA = []

for ix in data:
  print ix[0] # x1, x2
  for isnip in ix[1]:
    outputA.append(isnip)

print outputA
# [(3, 'a'), (1, 'b'), (5, 'c'), (2, 'a'), (4, 'b')]

outputB = []

for ix in data:
  print ix[0] # x1, x2
  for isnip in ix[1]:
    outputB.insert(isnip[0], isnip)

print outputB
# [(3, 'a'), (1, 'b'), (2, 'a'), (5, 'c'), (4, 'b')]

outputC = [None] * (5+1) #[]
for ix in data:
  print ix[0] # x1, x2
  for isnip in ix[1]:
    outputC[isnip[0]] = isnip

print outputC
# [None, (1, 'b'), (2, 'a'), (3, 'a'), (4, 'b'), (5, 'c')]

我有二维元组的数据(实际上,在我的实际例子中,是dict,但不必说),它们的第一个元素是排序索引;它们是未排序的,我需要对它们进行排序。然而,它们处于所有可能的嵌套级别(为了更简单的例子,我已经简化了上面的data;在我的实际情况下,它们可以嵌套得更远),因此我不能轻易地发出“排序”命令。
所以我考虑了插入元素-如您所见,我不能让.insert()保持顺序。于是我想到了明确的赋值——这是有效的,但是只有在列表被预先确定大小的情况下,为了找到这个大小,我仍然需要通过一个额外的递归来发现最大的索引是什么。
因此,我想在列表的确切位置插入(而不是像.insert()does那样的“before”),但事先不显式地调整列表的大小-是否有任何方法可以实现这一点?
编辑:这里有一些更像我的实际数据,显示(希望)为什么很难排序:
data = ( ( "x1", ( (3, "a"), (1, "b"),  (5, "c") )  ), ( "x2", ( "x3", ( (2, "a"), (4, "b") ) )  ), ("x100", 1 ) )

outputA = []

for ix in data:
  #print "[0]", ix[0], "[1]", ix[1] # x1, x2, x100
  try:
    for isnip in ix[1]:
      #print "isnip", isnip[0], "-", isnip[1]
      if int(isnip[0]) == isnip[0]:
        outputA.append(isnip)
      else:
        raise Exception("not good")
  except:
    try:
      for isnip in ix[1][1]:
        #print "isnip", isnip[0], "-", isnip[1]
        if int(isnip[0]) == isnip[0]:
          outputA.append(isnip)
    except:
      #print "skipping this"
      pass

print outputA
# [(3, 'a'), (1, 'b'), (5, 'c'), (2, 'a'), (4, 'b')]

outputB = []

for ix in data:
  try:
    for isnip in ix[1]:
      if int(isnip[0]) == isnip[0]:
        outputB.insert(isnip[0]+1, isnip)
      else:
        raise Exception("not good")
  except:
    try:
      for isnip in ix[1][1]:
        #print "isnip", isnip[0], "-", isnip[1]
        if int(isnip[0]) == isnip[0]:
          outputB.insert(isnip[0]+1, isnip)
    except:
      #print "skipping this"
      pass

print outputB
# [(3, 'a'), (1, 'b'), (5, 'c'), (2, 'a'), (4, 'b')]

最佳答案

将数据看作一棵树:

data = ( "x", (
          ( "x1", (
             (3, "a"),
             (1, "b"),
             (5, "c"))),
          ( "x2", (
             (2, "a"),
             (4, "b")))))

我添加了一个根节点,使其成为一致的格式。那棵树的叶子是由什么构成的?
def isleaf(x):
    return not isinstance(x[1], tuple)

现在您只需运行一个简单的depth-first search就可以按顺序排列叶子:
def dfs(x):
    if isleaf(x):
        yield x
        return
    for y in x[1]:
        yield from dfs(y)

例子:
>>> list(dfs(data))
[(3, 'a'), (1, 'b'), (5, 'c'), (2, 'a'), (4, 'b')]
>>> sorted(dfs(data), key=lambda x: x[0])
[(1, 'b'), (2, 'a'), (3, 'a'), (4, 'b'), (5, 'c')]

这可以扩展到任何其他类似树的数据。
更新:如果出于某种原因您必须避免排序步骤,那么您可以将结果收集到dict中,然后构造数组。
d = {}

def dfs(x):
    if isleaf(x):
        d[x[0]] = x
        return
    for y in x[1]:
        dfs(y)
dfs(data)

res = [None] * (max(d) + 1)
for i, v in d.items():
    res[i] = v

关于python - 将元素插入列表中的确切位置,而无需在Python中调整数组大小?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22364410/

10-12 00:24
查看更多