我有一个关于返回以下功能 我正在给一个食物列表中的文件,看起来像这样的问题:返回一个列表
'''
bread
bun
milk
soya milk
'''
,我一定要退回的食品名单列表例如[['bread','bun'], ['milk','soya milk']]
我对Python和编程非常新,因此我被困在for循环中以创建我的列表。任何输入将不胜感激 - kev
我有一个关于返回以下功能 我正在给一个食物列表中的文件,看起来像这样的问题:返回一个列表
'''
bread
bun
milk
soya milk
'''
,我一定要退回的食品名单列表例如[['bread','bun'], ['milk','soya milk']]
我对Python和编程非常新,因此我被困在for循环中以创建我的列表。任何输入将不胜感激 - kev
它是非常接近。而不是使用while len(next_food) > 0:
,您应该使用if和next_food为空且非空白时处理这两种情况。就像你评论表明的那样,在返回之前你应该包含最后一个子列表。
另一个需要检查的问题是next_food是否在最后包含换行符。如果它在那里,你应该去掉换行符。最后有一个替代if len(next_food):
检查的快捷方式。只要写if next_food:
就可以了。
的作品......
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")]
你想,当你达到新类别追加的子表,然后开始一个新的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)
这实际上不起作用,因为它会跳过最后一个类别组。核实! ;) – pawroman
好的电话。我不确定sub_list是否是for循环的本地对象。因为它是在外面宣布的,我不认为它会。 – emschorsch
这不是一个很好的解决方案......但它的一些有趣的小窍门..
>>> 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']]
正则表达式之美... – joaoricardo000
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模块 - 非常方便!
最简单且相当可读的方式是:
>>> [el.strip().split('\n') for el in text.split('\n\n')]
[['bread', 'bun'], ['milk', 'soya milk']]
的开裂\n\n
导致了线,紧跟着一个空行
的.strip()
消除了前端和后端换行符所以只有 元素之间的换行符
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']]
如果输入文件是足够小,可以完全读入内存,我会做这样的:
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']]
你是如何分组这些食物的? – NullUserException
如果两组食物之间有空白空白,则它们在文件中按行间空格分组,这意味着上面的集合是它自己的集合 – bigk