Python3.6:根据列表的给定元素将列表分为子列表

时间:2018-08-23 09:47:16

标签: python

我有一些数据,如下所示。 我现在尝试了很长时间,就是将这个列表分成多个子列表,这样每个子列表都代表第一行中给出的一个日期。在示例中,我们有5天的不同时间,我希望将原始列表分为5个相应的列表。

这个问题似乎不太复杂,但是我尝试了一段时间,由于某种原因,我无法解决这个问题。

希望您能提出任何解决方案。当然原始数据要大得多。

listofstrings=[
"17.02.2018 14:30:24    00000000    23,7    23,9    -2,0    1,1",
"17.02.2018 15:00:21    00000000    23,7    23,8    -4,0    1,1",
"19.02.2018 18:30:24    00000000    23,6    23,7    -3,0    1,1",
"19.02.2018 19:00:21    00000000    23,6    23,6    -7,0    1,1",
"19.02.2018 19:30:22    00000000    23,5    23,5    -5,0    1,1",
"20.02.2018 05:30:21    00000000    23,5    23,8    -3,0    1,1",
"20.02.2018 06:00:21    00000000    23,5    23,8    1,0 1,1",
"20.02.2018 16:00:22    00000000    23,6    23,8    -4,0    1,1",
"21.02.2018 05:00:22    00000000    23,6    23,7    0,0 1,1",
"21.02.2018 05:30:23    00000000    23,6    23,8    -6,0    1,1",
"22.02.2018 07:30:23    00000000    23,6    23,8    -6,0    1,1",
"22.02.2018 08:00:21    00000000    23,6    23,9    -3,0    1,1",
"22.02.2018 13:30:25    00000000    23,6    23,8    -3,0    1,1"]

listoflists=[]
locallist=[]

for i in range(0, len(listofstrings)):

    current_string=listofstrings[i]
    current_date=current_string.split()[0]
    if not i==0:
        recent_string=listofstrings[i-1] 
        recent_date=recent_string.split()[0]

        if current_date==recent_date:
            locallist.append(current_string)
            locallist.append(recent_string)

    listoflists.append(locallist)
    locallist.clear()

预期输出将是这样的:

list1=["17.02.2018 14:30:24    00000000    23,7    23,9    -2,0    1,1",
       "17.02.2018 15:00:21    00000000    23,7    23,8    -4,0    1,1"]
list2=["19.02.2018 18:30:24    00000000    23,6    23,7    -3,0    1,1",
       "19.02.2018 19:00:21    00000000    23,6    23,6    -7,0    1,1",
       "19.02.2018 19:30:22    00000000    23,5    23,5    -5,0    1,1",]
....

3 个答案:

答案 0 :(得分:5)

您似乎需要itertools.groupby

演示:

from itertools import groupby

listofstrings=[
"17.02.2018 14:30:24    00000000    23,7    23,9    -2,0    1,1",
"17.02.2018 15:00:21    00000000    23,7    23,8    -4,0    1,1",
"19.02.2018 18:30:24    00000000    23,6    23,7    -3,0    1,1",
"19.02.2018 19:00:21    00000000    23,6    23,6    -7,0    1,1",
"19.02.2018 19:30:22    00000000    23,5    23,5    -5,0    1,1",
"20.02.2018 05:30:21    00000000    23,5    23,8    -3,0    1,1",
"20.02.2018 06:00:21    00000000    23,5    23,8    1,0 1,1",
"20.02.2018 16:00:22    00000000    23,6    23,8    -4,0    1,1",
"21.02.2018 05:00:22    00000000    23,6    23,7    0,0 1,1",
"21.02.2018 05:30:23    00000000    23,6    23,8    -6,0    1,1",
"22.02.2018 07:30:23    00000000    23,6    23,8    -6,0    1,1",
"22.02.2018 08:00:21    00000000    23,6    23,9    -3,0    1,1",
"22.02.2018 13:30:25    00000000    23,6    23,8    -3,0    1,1"]


listofstrings = [i.split() for i in listofstrings]
result = dict((k, list(v)) for k, v in groupby(listofstrings, lambda x: x[0]))
print(result)

输出:

{'17.02.2018': [['17.02.2018', '14:30:24', '00000000', '23,7', '23,9', '-2,0', '1,1'], ['17.02.2018', '15:00:21', '00000000', '23,7', '23,8', '-4,0', '1,1']], 
 '19.02.2018': [['19.02.2018', '18:30:24', '00000000', '23,6', '23,7', '-3,0', '1,1'], ['19.02.2018', '19:00:21', '00000000', '23,6', '23,6', '-7,0', '1,1'], ['19.02.2018', '19:30:22', '00000000', '23,5', '23,5', '-5,0', '1,1']],
 '22.02.2018': [['22.02.2018', '07:30:23', '00000000', '23,6', '23,8', '-6,0', '1,1'], ['22.02.2018', '08:00:21', '00000000', '23,6', '23,9', '-3,0', '1,1'], ['22.02.2018', '13:30:25', '00000000', '23,6', '23,8', '-3,0', '1,1']],
 '21.02.2018': [['21.02.2018', '05:00:22', '00000000', '23,6', '23,7', '0,0', '1,1'], ['21.02.2018', '05:30:23', '00000000', '23,6', '23,8', '-6,0', '1,1']],
 '20.02.2018': [['20.02.2018', '05:30:21', '00000000', '23,5', '23,8', '-3,0', '1,1'], ['20.02.2018', '06:00:21', '00000000', '23,5', '23,8', '1,0', '1,1'], ['20.02.2018', '16:00:22', '00000000', '23,6', '23,8', '-4,0', '1,1']]}

或:

result = dict((k, list(v)) for k, v in groupby(listofstrings, lambda x: x[:10]))

输出:

{'17.02.2018': ['17.02.2018 14:30:24    00000000    23,7    23,9    -2,0    1,1', '17.02.2018 15:00:21    00000000    23,7    23,8    -4,0    1,1'],
 '19.02.2018': ['19.02.2018 18:30:24    00000000    23,6    23,7    -3,0    1,1', '19.02.2018 19:00:21    00000000    23,6    23,6    -7,0    1,1', '19.02.2018 19:30:22    00000000    23,5    23,5    -5,0    1,1'],
 '22.02.2018': ['22.02.2018 07:30:23    00000000    23,6    23,8    -6,0    1,1', '22.02.2018 08:00:21    00000000    23,6    23,9    -3,0    1,1', '22.02.2018 13:30:25    00000000    23,6    23,8    -3,0    1,1'],
 '21.02.2018': ['21.02.2018 05:00:22    00000000    23,6    23,7    0,0 1,1', '21.02.2018 05:30:23    00000000    23,6    23,8    -6,0    1,1'],
 '20.02.2018': ['20.02.2018 05:30:21    00000000    23,5    23,8    -3,0    1,1', '20.02.2018 06:00:21    00000000    23,5    23,8    1,0 1,1', '20.02.2018 16:00:22    00000000    23,6    23,8    -4,0    1,1']}

答案 1 :(得分:2)

这是不需要导入模块的解决方案。

l = listofstrings  # an alias for conciseness
d={st[:10]:[] for st in l}
for st in l:
    d[st[:10]] += [st]

说明:首先在字典d中创建一个空列表,其中key是每个输入字符串的前10个字符,即日期。 这利用了不能复制字典键的事实。实际上,您可以从输入中获得唯一日期的集合。

然后对于每个输入字符串,将“有效负载”添加到给定键下的列表中。同样,这些键将定义字符串附加到哪个列表。

完成后, d 是您所需的数据结构。

答案 2 :(得分:1)

这与上面的ilia解决方案非常相似。这是没有列表理解的,最后输出的是列表列表,而不是字典。

listofstrings = [
    "17.02.2018 14:30:24    00000000    23,7    23,9    -2,0    1,1",
    "17.02.2018 15:00:21    00000000    23,7    23,8    -4,0    1,1",
    "19.02.2018 18:30:24    00000000    23,6    23,7    -3,0    1,1",
    "19.02.2018 19:00:21    00000000    23,6    23,6    -7,0    1,1",
    "19.02.2018 19:30:22    00000000    23,5    23,5    -5,0    1,1",
    "20.02.2018 05:30:21    00000000    23,5    23,8    -3,0    1,1",
    "20.02.2018 06:00:21    00000000    23,5    23,8    1,0     1,1",
    "20.02.2018 16:00:22    00000000    23,6    23,8    -4,0    1,1",
    "21.02.2018 05:00:22    00000000    23,6    23,7    0,0     1,1",
    "21.02.2018 05:30:23    00000000    23,6    23,8    -6,0    1,1",
    "22.02.2018 07:30:23    00000000    23,6    23,8    -6,0    1,1",
    "22.02.2018 08:00:21    00000000    23,6    23,9    -3,0    1,1",
    "22.02.2018 13:30:25    00000000    23,6    23,8    -3,0    1,1"]
    _list = {}

for d in listofstrings:
    if d[:10] not in _list:
        _list[d[: 10]] = [d]
    else:
        _list[d[:10]].append(d)

_list_of_lists = []
for k, v in _list.items():
    _list_of_lists.append(v)

print(*_list_of_lists, sep="\n")

输出:

['17.02.2018 14:30:24    00000000    23,7    23,9    -2,0    1,1', '17.02.2018 15:00:21    00000000    23,7    23,8    -4,0    1,1']
['19.02.2018 18:30:24    00000000    23,6    23,7    -3,0    1,1', '19.02.2018 19:00:21    00000000    23,6    23,6    -7,0    1,1', '19.02.2018 19:30:22    00000000    23,5    23,5    -5,0    1,1']
['20.02.2018 05:30:21    00000000    23,5    23,8    -3,0    1,1', '20.02.2018 06:00:21    00000000    23,5    23,8    1,0     1,1', '20.02.2018 16:00:22    00000000    23,6    23,8    -4,0    1,1']
['21.02.2018 05:00:22    00000000    23,6    23,7    0,0     1,1', '21.02.2018 05:30:23    00000000    23,6    23,8    -6,0    1,1']
['22.02.2018 07:30:23    00000000    23,6    23,8    -6,0    1,1', '22.02.2018 08:00:21    00000000    23,6    23,9    -3,0    1,1', '22.02.2018 13:30:25    00000000    23,6    23,8    -3,0    1,1']