python-3.x - How can I condence this function into returning a value from a list comprehension?
问题描述
I understand it may not be best practices or conventional but this is more of a personal challenge.
def initialize_dataset(source):
all_features = []
targets = []
for (sent, label) in source:
feature_list=[]
feature_list.append(avg_number_chars(sent))
feature_list.append(number_words(sent))
all_features.append(feature_list)
targets.append(0) if label=="austen" else targets.append(1)
return all_features, targets
Example of what I'm looking for. I understand that it might not be possible to get it down to one single list and or value. But something close to it. I'd like to kind of expand my thinking on writing list comprehensions.
def sample_function(data):
return [i for i in data ]
解决方案
成功!我的 gaaaaaaaaaaaaaaawd 太丑了!
def initialize_dataset(source):
all_features, targets = [],[]; [(all_features.append([avg_number_chars(sent), number_words(sent)]), targets.append(0) if label == "austen" else targets.append(1)) for (sent, label) in source]; return all_features, targets
推荐阅读
- sql-server - 如何在 MSSQL 查询中为具有 INNER JOIN 的一列求和()适当的行?
- javascript - Postcss-loader 没有缩小 CSS 输出
- javascript - 强制 Javascript 函数调用调用 Function.prototype.call 方法
- server - 我的餐厅评论项目需要离线工作,但我的本地简单服务器显示 404 错误
- wpf - Blend 2015 中缺少状态转换效果
- sql - 这不是一个有效的月份
- python - Python:查找图形窗口何时移动
- javascript - 使用 Javascript 插入具有零宽度连接符的表情符号
- git - 重置后 --hard 我所有未跟踪的文件都消失了
- linux - 将软件包移植到 powerpc 需要哪些工具?