0

我有一个关于返回以下函数的问题,我得到了一个文件,其中包含一个看起来像这样的食物列表:

'''
bread
bun

milk
soya milk
'''

我必须返回一份食物清单,例如,[['bread','bun'], ['milk','soya milk']]

我对 python 和编程非常陌生,因此我被困在 for 循环中以创建我的列表。任何输入将不胜感激 - kev

4

7 回答 7

2

这样可行...

grocery_list_file = open('foods.txt','r').read()
foods = grocery_list_file.split("\n\n") #split on blank lines

result = []
for food in foods:
   newFood = food.split("\n") # split the lines, creating the output...
   result += [newFood]
return result

在一行中:

print [f.strip().split("\n") for f in open('foods.txt','r').read().split("\n\n")]
于 2012-11-14T18:21:48.283 回答
0

您想在到达新类别时附加子列表,然后开始一个新的 sub_list。当您到达文件末尾时,将剩余的 sub_list 附加到末尾很重要。

new_list.append("\n")   #to make sure it appends the last category
for next_food in new_list:
        if next_food = "\n":
            result.append(sub_list)
            sub_list = []
        else:
            sub_list.append(next_food)
于 2012-11-14T18:21:58.033 回答
0

这不是一个很好的解决方案……但它有一些有趣的技巧……

>>> s = '''
... bread
... bun
...
... milk
... soya milk
... '''
>>> import re
>>> parts = re.sub("[\[\]']","",str(s.strip().splitlines())).split(", ,")
>>> import string
>>> print [map(string.strip,p.split(",")) for p in parts]
[['bread', 'bun'], ['milk', 'soya milk']]
于 2012-11-14T18:26:18.547 回答
0

如果输入文件小到可以完全读入内存,我会这样做:

with open('grocery_list.txt', 'rt') as grocery_list_file:
    data = grocery_list_file.read()

sublist = [item.strip().split('\n') for item in data.split('\n\n')]

输出:

sublist: [['bread', 'bun'], ['milk', 'soya milk']]
于 2012-11-14T19:52:19.733 回答
0

最简单且可读性强的方法是:

>>> [el.strip().split('\n') for el in text.split('\n\n')]
[['bread', 'bun'], ['milk', 'soya milk']]
  1. 拆分\n\n为一行,紧接着是一个空行

  2. .strip()删除前导和尾随换行符,因此只有元素之间的换行符 存在

  3. 然后split将这些元素分解为一个列表,从而生成您的列表列表

或者,您可以使用itertools.groupby

>>> [groups for groups in (list(g) for k, g in groupby(text.splitlines(), bool)) if groups[0]]
[['bread', 'bun'], ['milk', 'soya milk']]
于 2012-11-14T19:36:46.390 回答
0

它非常接近。while len(next_food) > 0:当 next_food 为空白和非空白时,您应该使用 if 并处理这两种情况,而不是使用。就像您评论指出的那样,在返回之前,您应该包含最后一个子列表。

要检查的另一件事是 next_food 是否在末尾包含换行符。如果有换行符,您应该去掉它。最后,还有一个替代检查的快捷方式if len(next_food):。简单地写if next_food:就行了。

于 2012-11-14T18:21:28.153 回答
0

使用itertools.groupby

from itertools import groupby


def build_grocery_list():
    # using "with" to open the file - recommended way
    with open("foods.txt") as f:
        # lines will contain all the lines in the file, without "\n" characters
        lines = f.read().splitlines()

        # initialize result as an empty list
        result = []

        # Now for the fun part: group file lines basing on whether they are empty
        # (bool(string) is analogous to as using "if string:" -- will be True if
        #  the string is not empty)
        #
        # groupby works in such a way that it appends stuff to the group as long
        # as "key" condition is the same, returning (key, group) pairs.
        #
        # So, we get pairs: (bool(string), string-group) where:
        # - bool(string) is the group "key", delimiting empty and non-empty
        #   strings
        # - string-group is a lazy *generator*, hence the "list(group)"
        for nonblank, group in groupby(lines, bool):
            if nonblank:
                result.append(list(group))

    return result

如果你正在学习 Python,我真的建议你熟悉一下优秀的itertools模块——它非常方便!

于 2012-11-14T18:41:30.500 回答