在每个/n处将一个列表条目拆分为多个列表Python 3
我试图通过分解每个列表条目来创建几个列表。格式如下: 列表条目格式在每个/n处将一个列表条目拆分为多个列表Python 3,python,list,python-3.x,Python,List,Python 3.x,我试图通过分解每个列表条目来创建几个列表。格式如下: 列表条目格式 ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter
['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']
基本上,我的列表中有许多条目遵循此格式,我需要将每个条目分成几个列表,在每个符号处开始一个新的列表。例如:
列表1=根本原因:硬件故障
列表2=操作已完成:电源/过滤器/电缆交换
如此类推,这将需要对数千个条目执行此操作,所以我有一个所有根本原因的列表,一个所有已完成操作的列表,等等
我已经设法在每个点上放置了/n符号,我想要一个新的列表开始,但不确定从哪里开始
非常感谢您的帮助这就是您所看到的吗
x = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']
y = x[0].split('\n')
这就是你看到的吗
x = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']
y = x[0].split('\n')
如果需要列表列表,请执行以下操作:
list(map(lambda s: [s], old_list[0].splitlines()))
如果需要列表列表,您可以阅读更多关于和的信息:
list(map(lambda s: [s], old_list[0].splitlines()))
您可以阅读更多信息,并使用列表comp和str.splitlines将列表中的每个字符串拆分为各行的子列表:
split_lines = [s.splitlines() for s in lst]
如果希望列表中的每个字符串:
from itertools import chain
split_lines = [[s] for s in chain(*map(str.splitlines, lst))]
使用列表comp和str.splitlines将列表中的每个字符串拆分为各行的子列表:
split_lines = [s.splitlines() for s in lst]
如果希望列表中的每个字符串:
from itertools import chain
split_lines = [[s] for s in chain(*map(str.splitlines, lst))]
您可以使用
split()
alist = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']
new_lists = []
for i in alist:
for j in i.split('\n'):
new_lists.append([j])
print(new_lists)
# [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap'], ...
循环的第一个是在初始列表包含多个必须拆分的条目的情况下。您可以使用split()
方法创建条目,然后将它们放置在主列表中,如下所示:
alist = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']
new_lists = []
for i in alist:
for j in i.split('\n'):
new_lists.append([j])
print(new_lists)
# [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap'], ...
循环的第一个,是在初始列表包含多个必须拆分的条目的情况下。这里是所有所需列表的列表
list = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap']
temp = L[0].split('\n')
newList = []
for n in temp:
newList.append([n])
您将获得:
newList = [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap']]
在这里,您可以获得所有所需列表的列表
list = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap']
temp = L[0].split('\n')
newList = []
for n in temp:
newList.append([n])
您将获得:
newList = [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap']]
lst[0].splitlines()
您能为我们显示预期的输出吗?lst[0].splitlines()
您能为我们显示预期的输出吗?这不会为每个项目创建列表,但会为每个项目创建列表。这不会为每个项目创建列表,但python 3map
中的项目列表不会创建列表。您需要将其传递给list()
函数,如list(map(…)
。请更新,因为Python 3已被OP标记。我正在更新您的答案。Python 3map
没有创建列表。您需要将其传递给list()
函数,如list(map(…)
。请更新,因为Python 3是由OP标记的。我对你的答案投赞成票