需要将列表拆分为嵌套列表
Need to split list into a nested list
我正在尝试根据行尾和 # 创建子列表:
例如文件包含:
#
2.1,-3.1
-0.7,4.1
#
3.8,1.5
-1.2,1.1
并且输出需要是:
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
但编码后:
results = []
fileToProcess = open("numerical.txt", "r")
for line in fileToProcess:
results.append(line.strip().split(' '))
print(results)
我得到:
[['#'], ['2.1', '-3.1'], ['-0.7', '4.1'], ['#'], ['3.8', '1.5'], ['-1.2' , '1.1']]
假设 Python 作为一种编程语言,并且假设您希望输出完全像这样:
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
操作方法如下:
我对代码进行了注释以便更好地理解。如果有什么不清楚的地方请告诉我。
fileToProcess = open("numerical.txt", "r")
results = []
hashtag_results = []
# For each line, we have two cases: either the line contains hashtags or contains numbers.
for line in fileToProcess:
'''
If the line doesn't contain hashtags, then we want to:
1. Separate the text by "," and not spaces.
2. Parse the text as floats using list comprehension.
3. Append the parsed line to hashtag_results which contains
all lists between two hashtags.
'''
if not line.startswith("#"):
line_results = [ float(x) for x in line.strip().split(',')]
hashtag_results.append(line_results)
'''
If the line contains a hashtag AND the hastag_results ISN'T EMPTY:
then we want to append the whole hashtag_list to the final results list.
'''
if line.startswith("#") and hashtag_results:
results.append(hashtag_results)
hashtag_results = []
# For the final line, we append the last hashtag_results to the final results too.
results.append(hashtag_results)
print(results)
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
总体思路在您的 OP 中看起来不错,但您需要按 ","
(而不是 " "
)拆分,并将 list
附加到 results
,其中 list
是数值列表。
另一个问题是您在完成文件后没有关闭它。我建议使用 open()
支持的内置上下文管理器结构 (https://book.pythontips.com/en/latest/context_managers.html),一旦您离开上下文管理器范围,它将自动关闭文件。
从文件中解析数据是 Python 中的一项常见数据处理任务,因此 可以 通过列表理解以更“pythonic”的方式实现。
# use a context manager, so once you leave the `with` block,
# the file is closed(!)
with open("numerical.txt", "r") as fileToProcess:
results = [
# split the line on "," and interpret each element as a float
[float(val) for val in line.strip().split(",")]
# iterate through each line in the file
for line in fileToProcess
# ignore lines that just have '#'
if line.strip() != "#"
]
# here, the file would be closed, and `results` will contain the parsed data
# result = [[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
我正在尝试根据行尾和 # 创建子列表: 例如文件包含:
#
2.1,-3.1
-0.7,4.1
#
3.8,1.5
-1.2,1.1
并且输出需要是: [[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
但编码后:
results = []
fileToProcess = open("numerical.txt", "r")
for line in fileToProcess:
results.append(line.strip().split(' '))
print(results)
我得到: [['#'], ['2.1', '-3.1'], ['-0.7', '4.1'], ['#'], ['3.8', '1.5'], ['-1.2' , '1.1']]
假设 Python 作为一种编程语言,并且假设您希望输出完全像这样:
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
操作方法如下:
我对代码进行了注释以便更好地理解。如果有什么不清楚的地方请告诉我。
fileToProcess = open("numerical.txt", "r")
results = []
hashtag_results = []
# For each line, we have two cases: either the line contains hashtags or contains numbers.
for line in fileToProcess:
'''
If the line doesn't contain hashtags, then we want to:
1. Separate the text by "," and not spaces.
2. Parse the text as floats using list comprehension.
3. Append the parsed line to hashtag_results which contains
all lists between two hashtags.
'''
if not line.startswith("#"):
line_results = [ float(x) for x in line.strip().split(',')]
hashtag_results.append(line_results)
'''
If the line contains a hashtag AND the hastag_results ISN'T EMPTY:
then we want to append the whole hashtag_list to the final results list.
'''
if line.startswith("#") and hashtag_results:
results.append(hashtag_results)
hashtag_results = []
# For the final line, we append the last hashtag_results to the final results too.
results.append(hashtag_results)
print(results)
[[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]
总体思路在您的 OP 中看起来不错,但您需要按 ","
(而不是 " "
)拆分,并将 list
附加到 results
,其中 list
是数值列表。
另一个问题是您在完成文件后没有关闭它。我建议使用 open()
支持的内置上下文管理器结构 (https://book.pythontips.com/en/latest/context_managers.html),一旦您离开上下文管理器范围,它将自动关闭文件。
从文件中解析数据是 Python 中的一项常见数据处理任务,因此 可以 通过列表理解以更“pythonic”的方式实现。
# use a context manager, so once you leave the `with` block,
# the file is closed(!)
with open("numerical.txt", "r") as fileToProcess:
results = [
# split the line on "," and interpret each element as a float
[float(val) for val in line.strip().split(",")]
# iterate through each line in the file
for line in fileToProcess
# ignore lines that just have '#'
if line.strip() != "#"
]
# here, the file would be closed, and `results` will contain the parsed data
# result = [[[2.1, -3.1], [-0.7, 4.1]], [[3.8, 1.5], [-1.2, 1.1]]]