我在数据框中有一个称为流派的列,其中每一行都由字典对象列表组成。例如,这是第一行:
[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}, {"id": 14, "name": "Fantasy"}, {"id": 878, "name": "Science Fiction"}]
我想做的是重新设置格式,以便每行仅包含在“名称”键中找到的信息,最好是转储到列表中。
因此,例如,第一行的输出类似于以下内容:
["Action", "Adventure", "Fantasy", "Science Fiction"]
任何帮助将不胜感激。
答案 0 :(得分:3)
使用列表理解功能,将列表中的每本词典取出并提取Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 16.0),
child: Center(
child: Text(
'Circles',
style: TextStyle(fontSize: 18),
),
),
),
Container(
height: 200,
margin: EdgeInsets.only(top: 8.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.25,
height: MediaQuery.of(context).size.height * 0.2,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
),
Spacer(),
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return GestureDetector(
onTap: () {
_animationController.forward();
},
child: Container(
width: MediaQuery.of(context).size.width * _animation.value,
height: MediaQuery.of(context).size.height * _animation.value,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
),
);
},
),
Spacer(),
Container(
width: MediaQuery.of(context).size.width * 0.25,
height: MediaQuery.of(context).size.height * 0.2,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
)
],
),
),
],
);
:
'name'
在代码中:
[x['name'] for x in lst]
答案 1 :(得分:3)
data = [{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}, {"id": 14, "name": "Fantasy"}, {"id": 878, "name": "Science Fiction"}]
out = [x['name'] for x in data]
print(out)
答案 2 :(得分:0)
您还可以使用'name'
作为密钥的itemgetter
import operator
lst = [{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"},
{"id": 14, "name": "Fantasy"}, {"id": 878, "name": "Science Fiction"}]
answer = list(map(operator.itemgetter('name'), lst))
# ["Action", "Adventure", "Fantasy", "Science Fiction"]