如何基于列表中的某些值在Python中创建2D列表

时间:2014-10-15 03:03:21

标签: python list

我有以下值的列表:

numbers = [127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127]

这些都是指"能力"一个微控制器引脚,基本上是"模式"可用。结构的工作方式是第一个数字是模式,第二个数字是分辨率,当返回127时,意味着该引脚的所有模式都已耗尽。例如,

此列表中的第三个图钉支持分辨率为0, 1, 4的模式1, 1, 14。类似地,前两个引脚不支持模式(这就是为什么有两个连续127 s。)

我的问题是,如何为所有引脚及其支持的模式制作2D列表?如何遍历该数字列表,每次达到127时添加新列表?以下是我到目前为止的情况:

    pins = []
    supported_modes = [] # Temporary list to hold values
    for i in range(0, len(numbers)):
            if numbers[i] == 127:
                pins.append(supported_modes) # Append to pin list
                del supported_modes[:]          
                return
            supported_modes.append(numbers[i])

然而,这有各种各样的问题。主要认为每次都删除supported_modes。理想情况下,此列表(如果仅填充前三个引脚)将如下所示:

pins = [ [], [], [0, 1, 1, 1, 4, 14] etc. ]

由于

3 个答案:

答案 0 :(得分:2)

pins = []
supported_modes = []

for number in numbers:
    if number == 127:
        pins.append(supported_modes)
        supported_modes = []
    else:
        supported_modes.append(number)

您遇到了一些问题:return应该是continue但我更喜欢else,而del在那里没用,因为正如您所说的那样消失了<{1}}即使在插入之后也是如此,因为在Python中一切都是通过引用。

答案 1 :(得分:1)

numbers = [127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127]

answer = []
temp = []
for num in numbers:
    if num==127:
        answer.append(temp)
        temp = []
    else:
        temp.append(num)
if temp:
    answer.append(temp)

输出:

In [51]: answer
Out[51]: 
[[],
 [],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10, 6, 1],
 [0, 1, 1, 1, 2, 10, 6, 1]]

还有itertools的解决方案:

import itertools

answer = []
while numbers:
    temp = list(itertools.takewhile(lambda n: n!=127, numbers))
    numbers = numbers[len(temp)+1 :]
    answer.append(temp)

输出:

In [79]: answer
Out[79]: 
[[],
 [],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10, 6, 1],
 [0, 1, 1, 1, 2, 10, 6, 1]]  # note the missing empty list at the end

答案 2 :(得分:0)

repl.it

上的演示

代码:

numbers = [127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127]

pins = []
supported_modes = []
for number in numbers:
    if number == 127:
        if supported_modes:
            pins.append(supported_modes)
        supported_modes = []
    else:
        supported_modes.append(number)

print pins

输出:

[[0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 3, 8, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 4, 14],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10],
 [0, 1, 1, 1, 2, 10, 6, 1],
 [0, 1, 1, 1, 2, 10, 6, 1]]