如何在python中将2d列表分成两个单独的列表?

问题描述 投票:2回答:6
temp = [['1979', '74.6.160.107'], ['1979', '216.115.100.123'], ['1979', '216.115.100.124'], ['1979', '74.6.160.106'], ['1979', '192.168.1.33'], ['1979', '74.6.160.106'], ['1979', '216.115.100.124'], ['1979', '216.115.100.123'], ['1979', '74.6.160.107'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979', '192.168.1.118'], ['1979', '8.8.8.8'], ['1979', '64.14.29.250'], ['1979', '64.14.29.252'], ['1979', '64.14.29.251'], ['1979', '64.14.29.50'], ['1979', '192.168.1.11'], ['1979', '8.8.8.8'], ['1979', '2404', '6800', '4007'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979']]

这是我的2d列表,我希望将它分成两个列表,例如:

id = ['1979', '1979', '1979', #and so on..]
ip = ['74.6.160.107', '216.115.100.123', '216.115.100.124' #and so on..]

如何将其拆分为两个单独的列表?

我还有另一个清单:

dates = ['Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:23', 'Jun 15 16:26:23', 'Jun 15 16:26:30', 'Jun 15 16:26:30', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:33', 'Jun 15 16:26:33', 'Jun 15 16:26:33', 'Jun 15 16:26:37', 'Jun 15 16:26:38', 'Jun 15 16:26:38']

我想将“dates”和“ip”加入另一个列表,如下所示:

finalResult = [['Jun 15 16:26:21', '74.6.160.107'], ['Jun 15 16:26:21' , '216.115.100.123'] #and so on.. ]

我如何加入他们如上所示?

python arrays list slice
6个回答
2
投票

为了您的目的,tuple可能与list一样好。因此,您可以使用zip将列表列表拆分为两个元组:

year, ip = zip(*temp)

然后通过zip组合您的日期和IP地址,以形成元组列表:

res = list(zip(dates, ip))

上述方法依赖于具有相同元素数量的tempdates;在temp的每个子列表中有两个元素。


0
投票

temp拆分为两个列表:

id = [i[0] for i in temp]
ip = [i[1] for i in temp]  

请注意,在您的临时示例中,您的最后一个索引是列表,其中只有一个变量将导致IndexError

合并datesipfinalResult

finalResult = [[i, j] for i, j in zip(dates, ip)]

0
投票

第一个答案是一个简单的转置。它可以在numpy的帮助下完成。可能有更多的pythonic方式来完成任务,但这就是我所使用的。

import numpy as np    
a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
np.array(a).T.tolist()

[[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

对于第二个,确保两个列表的长度相等。

a = [1, 2, 3, 4, 5, 6]
b = ['a', 'b', 'c', 'd', 'e', 'f']
c = [[ax, bx] for ax, bx in zip(a, b)]

[[1,'a'],[2,'b'],[3,'c'],[4,'d'],[5,'e'],[6,'f']]


0
投票

使用列表推导和生成器表达式迭代第一列,而不是第二列值:

id, ip = ([el[column] for el in temp] for column in range(2))

使用zip和list comprehension连接两个列表:

finalResult = [[date, ip_addr] for date, ip_addr in zip(dates, ip)]

如果您需要检查temp中的无效值,您可以执行类似的操作

id, ip = ([el[column] for el in temp if len(el) == 2] for column in range(2))

0
投票

创建两个列表:

year = [i[0] for i in temp]
ip = [i[1] for i in temp[:-1]]

对于第二个列表,我们剪切了temp的最后一个元素,因为它只有年份值,也许你错过了它,我建议你检查一下。

由于在temp的最后一个元素中没有ip值,lendateslenip是不同的。合并datesip只需使用简单的for loop

final = []
for i, j in zip(dates, ip):
        final.append([i, j])

然后像这样将dates的最后一个元素添加到finalfinal.append([dates[-1]])


0
投票

我建议在进行拆分和连接之前过滤标准结构和有效IP的输入列表,如下所示:

#testasplilist.py

def validate_ip(s):
    a = s.split('.')
    if len(a) != 4:
        return False
    for x in a:
        if not x.isdigit():
            return False
        i = int(x)
        if i < 0 or i > 255:
            return False
    return True

#input data
temp = [['1979', '74.6.160.107'], ['1979', '216.115.100.123'], ['1979', '216.115.100.124'], ['1979', '74.6.160.106'], ['1979', '192.168.1.33'], ['1979', '74.6.160.106'], ['1979', '216.115.100.124'], ['1979', '216.115.100.123'], ['1979', '74.6.160.107'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979', '192.168.1.118'], ['1979', '8.8.8.8'], ['1979', '64.14.29.250'], ['1979', '64.14.29.252'], ['1979', '64.14.29.251'], ['1979', '64.14.29.50'], ['1979', '192.168.1.11'], ['1979', '8.8.8.8'], ['1979', '2404', '6800', '4007'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979']]

dates = ['Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:23', 'Jun 15 16:26:23', 'Jun 15 16:26:30', 'Jun 15 16:26:30', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:33', 'Jun 15 16:26:33', 'Jun 15 16:26:33', 'Jun 15 16:26:37', 'Jun 15 16:26:38', 'Jun 15 16:26:38']

# filter temp for valid IP
c= []
for x in temp:
    if (len(x) == 2) and validate_ip(x[1]):
        c.append(x)

print(c)


#filter dates for valid IP
d=[]
for x,y  in zip(temp, dates):
    if (len(x) == 2) and validate_ip(x[1]):
         d.append(y)

print(d)


a = [x[0] for x in c]

b = [x[1] for x in c]

print(a)

print(b)

print(len(c))


print (len(d))

finalResult = [[x, y] for x, y in zip(d, b)]

print(finalResult)

结果:

[['1979', '74.6.160.107'], ['1979', '216.115.100.123'], ['1979', '216.115.100.124'], ['1979', '74.6.160.106'], ['1979', '192.168.1.33'], ['1979', '74.6.160.106'], ['1979', '216.115.100.124'], ['1979', '216.115.100.123'], ['1979', '74.6.160.107'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1'], ['1979', '192.168.1.118'], ['1979', '8.8.8.8'], ['1979', '64.14.29.250'], ['1979', '64.14.29.252'], ['1979', '64.14.29.251'], ['1979', '64.14.29.50'], ['1979', '192.168.1.11'], ['1979', '8.8.8.8'], ['1979', '192.168.1.24'], ['1979', '127.0.0.1']]
['Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:21', 'Jun 15 16:26:23', 'Jun 15 16:26:23', 'Jun 15 16:26:30', 'Jun 15 16:26:30', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:32', 'Jun 15 16:26:33', 'Jun 15 16:26:33', 'Jun 15 16:26:37', 'Jun 15 16:26:38']
['1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979', '1979']
['74.6.160.107', '216.115.100.123', '216.115.100.124', '74.6.160.106', '192.168.1.33', '74.6.160.106', '216.115.100.124', '216.115.100.123', '74.6.160.107', '192.168.1.24', '127.0.0.1', '192.168.1.24', '127.0.0.1', '192.168.1.118', '8.8.8.8', '64.14.29.250', '64.14.29.252', '64.14.29.251', '64.14.29.50', '192.168.1.11', '8.8.8.8', '192.168.1.24', '127.0.0.1']
23
23
[['Jun 15 16:26:21', '74.6.160.107'], ['Jun 15 16:26:21', '216.115.100.123'], ['Jun 15 16:26:21', '216.115.100.124'], ['Jun 15 16:26:21', '74.6.160.106'], ['Jun 15 16:26:21', '192.168.1.33'], ['Jun 15 16:26:21', '74.6.160.106'], ['Jun 15 16:26:21', '216.115.100.124'], ['Jun 15 16:26:21', '216.115.100.123'], ['Jun 15 16:26:21', '74.6.160.107'], ['Jun 15 16:26:23', '192.168.1.24'], ['Jun 15 16:26:23', '127.0.0.1'], ['Jun 15 16:26:30', '192.168.1.24'], ['Jun 15 16:26:30', '127.0.0.1'], ['Jun 15 16:26:32', '192.168.1.118'], ['Jun 15 16:26:32', '8.8.8.8'], ['Jun 15 16:26:32', '64.14.29.250'], ['Jun 15 16:26:32', '64.14.29.252'], ['Jun 15 16:26:32', '64.14.29.251'], ['Jun 15 16:26:32', '64.14.29.50'], ['Jun 15 16:26:33', '192.168.1.11'], ['Jun 15 16:26:33', '8.8.8.8'], ['Jun 15 16:26:37', '192.168.1.24'], ['Jun 15 16:26:38', '127.0.0.1']]
© www.soinside.com 2019 - 2024. All rights reserved.