[英]How to write my code in python way of coding?
I have written a raw code in python, How can I make that look more of a python structure,我用 python 编写了一个原始代码,我怎样才能让它看起来更像一个 python 结构,
I haven't created class, objects for each functions and I want to return instead of printing我还没有为每个函数创建类和对象,我想返回而不是打印
How can I separate function wise and return at the end?如何明智地分离功能并在最后返回?
with open(r'features.csv', 'r') as f:
checker = lambda i: bool(i and i.strip())
reader = csv.reader(f)
header = next(reader)
folders = next(
{
header[0]: [row[0]],
'Feature Name': list(filter(checker, row[:1])),
'Child folder': list(filter(checker, row[1:]))
} for row in reader
)
foldersinlist = list(folders.values())
lists = sum(foldersinlist, [])
print(lists)
Any thoughts?有什么想法吗?
It would be helpful if we knew what you were trying to do with the returned data, we could help you a bit more, but this should get you moving in the right direction.如果我们知道您想用返回的数据做什么会很有帮助,我们可以为您提供更多帮助,但这应该会让您朝着正确的方向前进。
def my_function():
with open(r'features.csv', 'r') as f:
checker = lambda i: bool(i and i.strip())
reader = csv.reader(f)
header = next(reader)
folders = next(
{
header[0]: [row[0]],
'Feature Name': list(filter(checker, row[:1])),
'Child folder': list(filter(checker, row[1:]))
} for row in reader
)
foldersinlist = list(folders.values())
lists = sum(foldersinlist, [])
# print(lists) #Instead of this, let's return the value:
return lists
my_data = my_function() #we're setting my_data to the returned-value of my_function
print (my_data) #Now you can us my_data wherever you need the result of my_function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.