在python中创建组和列表

时间:2017-11-18 13:02:38

标签: python list loops for-loop

我有一个这样的清单。 last_1=[['3', '3', '2', 'F', '2', 'C', '2', 'D', '2', 'A', '2', '8', '7', 'C', '3', 'B', '2', 'E', '2', 'E', '3', '3', '3', '4', '3', '3', '3', '0', '3', 'B', '2', '8', '3', '3', '2', 'D', '2', 'E'], ['2', 'C', '2', 'A', '3', '3', '2', 'E', '2', '8', '7', '4', '7', 'A', '5', '3', '7', 'C', '3', '9', '2', 'D', '2', 'F', '2', 'F', '3', 'B', '2', 'E', '3', '8', '7', 'C', '2', '3', '2', 'D', '2', '7', '7', 'C', '2', '8', '2', 'D', '7', 'C', '2', '8', '3', '7', '2', 'A', '2', 'F', '3', '3', '2', 'E', '3', 'B', '2', '8', '3', '7', '7', 'C', '2', '3', '2', 'D', '2', '7', '2', 'A', '7', 'C', '3', '4', '2', '7', '2', 'F', '3', 'B', '2', 'E', '7', 'A', '7', '3'], ['3', '8', '3', '7', '3', '6', '7', 'C', '3', '1', '3', '3', '3', '0', '3', '0', '7', '4', '3', 'B', '2', 'A', '3', '5', '7', '3', '6', '2'], ['7', 'C', '7', 'C', '7', 'C', '7', 'C', '2', 'A', '3', '7', '2', '8', '2', '7', '2', 'A', '2', 'E', '7', 'C', '3', 'B', '2', 'A', '3', '5', '7', 'C', '7', '1', '7', 'C', '7', 'A', '7', 'C', '5', '1', '3', '3', '3', '0', '3', '0', '7', 'C', '3', '7', '2', '4', '3', '7', '3', '9', '2', '7', '2', '8', '3', '7', '3', '8', '7', 'A'], ['3', '1', '3', '3', '3', '0', '3', '0', '7', '4', '7', 'A', '3', '4', '3', '7', '3', '0', '3', '0', '2', 'D', '7', 'A', '7', '3']]如果您认为last_1是一个包含5个元素的列表。我只是想用5个元素保护它们。我的意思是我想获得这样的输出:

> output_hexa=[['33','2F','2C',...,'2E'],['2C','2A','33','2E',...,'73'],[....],[...],[...]]

由于它的长度,我保持短输出。顺便说一下,这个output_hexa列表可以改变。所以,它的长度可能超过5或小于5.而且我已尝试过以上的东西。我错了什么?你能说我吗?

output_hexa=[]
hexa_output=[]
b=0
indis_one=0
indis_two=1
for i in range(len(last_1)):
    for x in last_1:
        for j in range((len(x))//2):
            if len(output_hexa)-1*(2)==j:
                indis_one=0
                indis_two=1
                hexa_output.extend(output_hexa)
                output_hexa=[]
                break
            else:
                pass
            output_hexa.insert(j,last_1[i][indis_one]+last_1[i][indis_two])
            indis_one+=2
            indis_two+=2


print(output_hexa)

它会出现IndexError: list index out of range错误。

2 个答案:

答案 0 :(得分:0)

只需使用简单的one-line list-comprehension

[[l[i]+l[i+1] for i in range(0,len(l)-1,2)] for l in last_1]

给出:

[['33', '2F', '2C', '2D', '2A', '28', '7C', '3B', '2E', '2E', '33', '34', '33', '30', '3B', '28', '33', '2D', '2E'], ['2C', '2A', '33', '2E', '28', '74', '7A', '53', '7C', '39', '2D', '2F', '2F', '3B', '2E', '38', '7C', '23', '2D', '27', '7C', '28', '2D', '7C', '28', '37', '2A', '2F', '33', '2E', '3B', '28', '37', '7C', '23', '2D', '27', '2A', '7C', '34', '27', '2F', '3B', '2E', '7A', '73'], ['38', '37', '36', '7C', '31', '33', '30', '30', '74', '3B', '2A', '35', '73', '62'], ['7C', '7C', '7C', '7C', '2A', '37', '28', '27', '2A', '2E', '7C', '3B', '2A', '35', '7C', '71', '7C', '7A', '7C', '51', '33', '30', '30', '7C', '37', '24', '37', '39', '27', '28', '37', '38', '7A'], ['31', '33', '30', '30', '74', '7A', '34', '37', '30', '30', '2D', '7A', '73']]

如果你需要这个解释发表评论,我很乐意在这个答案中详细解释每个部分,但我会假设你可以自己解决...

答案 1 :(得分:-1)

Credits should go this answer

不要重新发明轮子,使用itertools库 - ))

from itertools import izip_longest
def grouper(n, iterable):
    args = [iter(iterable)] * n
    return izip_longest(*args)

如果您将上述功能应用于列表,

# let's assume a is your list 
print map(lambda e: list(grouper(2, e)), a)

你会得到

# =>[[('3', '3'), ('2', 'F'), ('2', 'C'), ('2', 'D'), ('2', 'A'), ('2', '8'), ('7', 'C'), ('3', 'B'), ('2', 'E'), ('2', 'E'), ('3', '3'), ('3', '4'), ('3', '3'), ('3', '0'), ('3', 'B'), ('2', '8'), ('3', '3'), ('2', 'D'), ('2', 'E')], [('2', 'C'), ('2', 'A'), ('3', '3'), ('2', 'E'), ('2', '8'), ('7', '4'), ('7', 'A'), ('5', '3'), ('7', 'C'), ('3', '9'), ('2', 'D'), ('2', 'F'), ('2', 'F'), ('3', 'B'), ('2', 'E'), ('3', '8'), ('7', 'C'), ('2', '3'), ('2', 'D'), ('2', '7'), ('7', 'C'), ('2', '8'), ('2', 'D'), ('7', 'C'), ('2', '8'), ('3', '7'), ('2', 'A'), ('2', 'F'), ('3', '3'), ('2', 'E'), ('3', 'B'), ('2', '8'), ('3', '7'), ('7', 'C'), ('2', '3'), ('2', 'D'), ('2', '7'), ('2', 'A'), ('7', 'C'), ('3', '4'), ('2', '7'), ('2', 'F'), ('3', 'B'), ('2', 'E'), ('7', 'A'), ('7', '3')], [('3', '8'), ('3', '7'), ('3', '6'), ('7', 'C'), ('3', '1'), ('3', '3'), ('3', '0'), ('3', '0'), ('7', '4'), ('3', 'B'), ('2', 'A'), ('3', '5'), ('7', '3'), ('6', '2')], [('7', 'C'), ('7', 'C'), ('7', 'C'), ('7', 'C'), ('2', 'A'), ('3', '7'), ('2', '8'), ('2', '7'), ('2', 'A'), ('2', 'E'), ('7', 'C'), ('3', 'B'), ('2', 'A'), ('3', '5'), ('7', 'C'), ('7', '1'), ('7', 'C'), ('7', 'A'), ('7', 'C'), ('5', '1'), ('3', '3'), ('3', '0'), ('3', '0'), ('7', 'C'), ('3', '7'), ('2', '4'), ('3', '7'), ('3', '9'), ('2', '7'), ('2', '8'), ('3', '7'), ('3', '8'), ('7', 'A')], [('3', '1'), ('3', '3'), ('3', '0'), ('3', '0'), ('7', '4'), ('7', 'A'), ('3', '4'), ('3', '7'), ('3', '0'), ('3', '0'), ('2', 'D'), ('7', 'A'), ('7', '3')]]

您还可以应用另一个lambda将这些元组作为字符串加入

print map(lambda e: map(lambda f: "".join(f), list(grouper(2, e))), a)

结果

# =>[['33', '2F', '2C', '2D', '2A', '28', '7C', '3B', '2E', '2E', '33', '34', '33', '30', '3B', '28', '33', '2D', '2E'], ['2C', '2A', '33', '2E', '28', '74', '7A', '53', '7C', '39', '2D', '2F', '2F', '3B', '2E', '38', '7C', '23', '2D', '27', '7C', '28', '2D', '7C', '28', '37', '2A', '2F', '33', '2E', '3B', '28', '37', '7C', '23', '2D', '27', '2A', '7C', '34', '27', '2F', '3B', '2E', '7A', '73'], ['38', '37', '36', '7C', '31', '33', '30', '30', '74', '3B', '2A', '35', '73', '62'], ['7C', '7C', '7C', '7C', '2A', '37', '28', '27', '2A', '2E', '7C', '3B', '2A', '35', '7C', '71', '7C', '7A', '7C', '51', '33', '30', '30', '7C', '37', '24', '37', '39', '27', '28', '37', '38', '7A'], ['31', '33', '30', '30', '74', '7A', '34', '37', '30', '30', '2D', '7A', '73']]