python - python:拆分每个子列表中的项目
问题描述
我有以下子列表:
lst=[['a,b,c,d,e'],['f,g,h'] ]
我想获得以下结果:
lst=[['a','b','c','d','e'],['f','g','h']]
但以下行
lst1=[y for x in lst for y in x.split(',')]
print(lst1)
产生此错误: AttributeError: 'list' object has no attribute 'split'
我应该如何解决这个问题?
解决方案
试试这个简单的方法:
lst = [['a,b,c,d,e'],['f,g,h']]
list1 = [x[0].split(',') for x in lst]
print(list1)
输出:
[['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h']]
推荐阅读
- java - 如何在Java上确定字符串是否为双精度
- fingerprint - 我可以比较从不同指纹扫描仪扫描的两个指纹吗?
- postgresql - 如何在 postgresql 中使用 age() 函数
- eloquent - 在函数中调用 db
- java - 为什么 hbase get 函数不获取记录?
- ios - 以编程方式将 CollectionViewCell 到达 Xib
- javascript - rxjs ReplaySubject 句柄
- javascript - 量角器:你能延迟 WebElement.sendKeys() 全局 onPrepare 吗?
- json - Cloud Speech-to-Text 错误采样率赫兹
- spring - 如何读取tomcat错误日志