How to slice a 2d list into two separate lists in python?

时间:2018-06-19 11:08:42

标签: python arrays list slice

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']]

This is my 2d list, I want it to be split into two lists for example:

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

how do I split it into two separate lists?

further I have another list:

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']

I want to join "dates" and "ip" into another list as follows:

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

how do I join them as shown above?

6 个答案:

答案 0 :(得分:2)

出于您的目的,tuplelist一样好。因此,您可以使用zip将列表列表分成两个元组:

year, ip = zip(*temp)

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

res = list(zip(dates, ip))

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

答案 1 :(得分:0)

Iterating through first, than second column values using list comprehension and generator expression:

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

Joining two lists using zip and list comprehension:

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

If you need to check for invalid values in temp you can do something like

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

答案 2 :(得分:0)

Split temp to two lists:

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

Note that in you temp example your last index is list with only one variable which will cause IndexError.

Merge dates and ip to finalResult:

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

答案 3 :(得分:0)

The first answer is a simple transpose. It can be done with the help of numpy. There might be a more pythonic way of way doing the task but this is what I've used.

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

gives

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

For the second, make sure both the lists are of equal length.

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

gives

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

答案 4 :(得分:0)

创建两个列表:

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

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

由于temp的最后一个元素中没有ip值,因此len的{​​{1}}和dates的{​​{1}}有所不同。 要合并lenip,只需使用简单的dates

ip

然后将for loop的最后一个元素添加到final = [] for i, j in zip(dates, ip): final.append([i, j]) ,如下所示:dates

答案 5 :(得分: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']]