python - 从python中的csv中查找字符串长度并附加字典格式的另一列
问题描述
基本上-我想附加房屋和地区专栏
- 然后找到两列的字符串长度;在这种情况下 House 263 --> (0, 8), dhaka (10,14)
- 另外,附上它们的相关标签(label1 列和 label2)
- 为所有行执行此操作
我的预期输出格式是我的预期输出-[('House 263 dhaka', {'entities': [[(0, 8)], 'holding_number'], [(10,14), 'district']})
我该怎么做?
解决方案
尝试使用此列表理解:
>>> [(k, {'entities': [[[0, len(k.rpartition(' ')[0]) - 1], v['label1']], [(k.rfind(' ') + 1, len(k) - 1), v['label2']]]}) for k, v in df.set_index(['house', 'district']).set_axis(df[['house', 'district']].agg(' '.join, axis=1)).to_dict('index').items()]
[('House 163 dhaka', {'entities': [[[0, 8], 'holding_number'], [(10, 14), 'district']]}), ('House 31 comilla', {'entities': [[[0, 7], 'holding_number'], [(9, 15), 'district']]}), ('House 193/A chittagong', {'entities': [[[0, 10], 'holding_number'], [(12, 21), 'district']]})]
>>>
推荐阅读
- python - 如何在 Python 中以静态方法修补打开
- google-cloud-platform - 谷歌可能平台无法加载虚拟机实例
- amazon-web-services - 如何使用 CDK 授予 Lambda 对 RDS 的访问权限?
- mysql - MySQL:在过程中准备/执行(concat)
- java - Docker 抛出异常 - Html2Pdf 库
- python - 在 pandas 中读取 csv 时自动确定标题行
- c# - 使用 VB.NET 在 Excel 的下拉单元格中获取可用选项
- c++ - 没有内存分配的工厂方法 C++
- node.js - 可靠地验证 JWS 证书链和域
- spring-boot - 使用 spring cloud 流和函数的 route FROM 和 route TO