如何根据索引组合嵌套列表?

时间:2016-11-18 13:03:20

标签: python python-2.7

考虑这些嵌套列表:

L1 = [[1,2,3],[4,5,6],[7,8,9]]
L2 = [[11,22,33],[44,55,66]]
L3 = [[111,222,333],[444,555,666],[777,888,999]]
L4 = [12,13,14] # Note: not a nested list

我要求:

output1 = [1,4,7,11,44,111,444,777,12]
output2 = [2,5,8,22,55,222,555,888,13]
output3 = [3,6,9,33,66,333,666,999,14]

4 个答案:

答案 0 :(得分:3)

oneliner:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<body>
  <div data-role="content">
    <fieldset>
      <label>x1</label>
      <input id="x1" , autofocus="autofocus" , class="inputTextBox" />
    </fieldset>
    <fieldset>
      <label>x2</label>
      <input id="x2" , class="inputTextBox" />
    </fieldset>
    <fieldset>
      <label>x3</label>
      <input id="x3" , class="inputTextBox" />
    </fieldset>
    <input type="submit" value="Log in" style="width:100%" />
  </div>
</body>

</html>

输出:

output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]

虽然你真的应该提供一个你在寻求解决方案时尝试的样本: - )

更新

由于我们显然沉迷于友好的竞争,所以我的机器(python3)上有不同答案的时间:

<强>代码:

>>> output1
[1, 4, 7, 11, 44, 111, 444, 777, 12]
>>> output2
[2, 5, 8, 22, 55, 222, 555, 888, 13]
>>> output3
[3, 6, 9, 33, 66, 333, 666, 999, 14]

结果(最快的一次运行):

import time

L1 = [[1,2,3],[4,5,6],[7,8,9]]
L2 = [[11,22,33],[44,55,66]]
L3 = [[111,222,333],[444,555,666],[777,888,999]]
L4 = [12,13,14]


t = time.process_time()

output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))


t = time.process_time()

output1, output2, output3 = map(list,(zip(*L1+L2+L3+[L4])))

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))


t = time.process_time()

from itertools import chain

output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))

@Chris_Rands答案似乎是最快的: - )

答案 1 :(得分:2)

zip有一个简单的解决方案:

output1, output2, output3 = map(list,(zip(*L1+L2+L3+[L4])))

请注意,如果您对元组而不是列表感兴趣,可以进一步简化:

output1, output2, output3 = zip(*L1+L2+L3+[L4])

我发现这比使用chain合并列表要快一些,至少对于这些数据而言。它似乎也比@ Gormador的解决方案更快。

>>> def f1():
...     output1, output2, output3 = zip(*L1+L2+L3+[L4])
... 
>>> def f2():
...     output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))
... 
>>> def f3():
...     output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]
... 
>>> timeit(lambda: f1(), number = 1000000)
1.5090796248987317
>>> timeit(lambda: f2(), number = 1000000)
1.7326991918962449
>>> timeit(lambda: f3(), number = 1000000)
5.100052359048277

答案 2 :(得分:1)

我很惊讶没有人提到itertools.chain()zip()的用法。最简单的解决方案是:

   # Function designed to retrieve first name only from fullname entry.
def get_first_name(name):
    i = 0 
    while i < len(name) and name[i] !=" ":
        i += 1
    return name[:i]

# Function designed to retrieve first initial of last name or first initial of first name if only one name input.
def get_last_initial(name):
    j = len(name) - 1 
    while j >= 0 and name[j] !=" ":
        j-=1
    return full_name[j+1]

# Function that generates username based upon user input.
def get_username(full_name):
    username = get_first_name(full_name) + get_last_initial(full_name) 
    return username.lower()

使变量保持值为:

from itertools import chain

output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))

答案 3 :(得分:0)

试试这个:

L = L1+L2+L3+[L4]
output1 = [l[0] for l in L]
output2 = [l[1] for l in L]
output3 = [l[2] for l in L]