首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python查找句子和段落

使用Python查找句子和段落
EN

Stack Overflow用户
提问于 2017-08-18 22:41:29
回答 1查看 72关注 0票数 0

我有以下格式的数据:

代码语言:javascript
复制
[1956, Jon's story, He sold his soul in 1987, 200]  
[1960, Mary's story, "She liked her soul, but decided to sold it anyway.", 250]  
[1963, "Alice and Peter story, with a twist", "Peter said "Your soul is mine!" and tried to sold it, but Alice had no soul and killed him.", 500]

我想把这个分成

代码语言:javascript
复制
[1956, 1960, 1963]  
['Jon's story', 'Mary's story','Alice and Peter story, with a twist']  
['He sold his soul in 1987','She liked her soul, but decided to sold it anyway.','Peter said "Your soul is mine!" and tried to sold it, but Alice had no soul and killed him.']  
[200,250,500]

到目前为止,我已经这样做了

代码语言:javascript
复制
import re
data = [[1956, "Jon's story", "He sold his soul in 1987", 200],
        [1960, "Mary's story", "She liked her soul, but decided to sold it anyway.", 250],
        [1963, "Alice and Peter story, with a twist", "Peter said 'Your soul is mine!' and tried to sold it, but Alice had no soul and killed him.", 500]]
for row in data:
    line = str(row)
    sentence = re.split(r',', line)

但这种方式会考虑到“”内部的逗号分隔。我怎么才能避免呢?

EN

回答 1

Stack Overflow用户

发布于 2017-08-18 23:08:43

所以这可以通过使用zip而不是re来解决,看看下面的代码,看看它是如何工作的。m4、m3、m2、m1将是包含所需值的列表

代码语言:javascript
复制
    data = [[1956, "Jon's story", "He sold his soul in 1987", 200],
    [1960, "Mary's story", "She liked her soul, but decided to sold it anyway.", 250],
    [1963, "Alice and Peter story, with a twist", "Peter said 'Your soul is mine!' and tried to sold it, but Alice had no soul and killed him.", 500]]
    m4, m3 ,m2,m1 = map(list, zip(*data))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45759345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档