[英]Python3.6: Separation of a list into sublists depending on given elements of the list
我有一些數據,如下所示。 我現在嘗試了很長一段時間是將這個列表分成子列表,這樣每個子列表代表第一行中給出的一個日期。 我們在示例中有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",]
....
看起來你需要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']}
這是一個不需要導入模塊的解決方案。
l = listofstrings # an alias for conciseness
d={st[:10]:[] for st in l}
for st in l:
d[st[:10]] += [st]
解釋:首先在字典d中創建一個空列表,其中key是每個輸入字符串的前10個字符,即日期。 這利用了dict鍵不能重復的事實。 實際上,您可以從輸入中獲得一組唯一日期。
然后,對於每個輸入字符串,將“有效負載”添加到給定密鑰下的列表中。 同樣,鍵將定義附加字符串的列表。
完成后, d是您想要的數據結構。
這與上面的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']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.