首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中修改列表中的字符串

在Python中修改列表中的字符串
EN

Stack Overflow用户
提问于 2019-08-02 17:26:33
回答 3查看 75关注 0票数 0

我想要创建一个程序来转换这个列表:

代码语言:javascript
复制
['H.Geffner/AI:FromProgramstoSolvers7', 'R.DeSousa.', 'R.Dechter.', 'H.Dreyfus.', 'J.Elster.', 'J.Evans.Dual-processingaccountsofreasoning,judgment,', 'H.Geffner.Heuristics,planning,cognition.InR.Dechter,', 'H.Geffner,andJ.Y.Halpern,editors,', 'H.Geffner.Computationalmodelsofplanning.', 'M.Genesereth,N.Love,andB.Pell.Generalgameplay-', 'G.Gigerenzer.', 'G.GigerenzerandP.Todd.', 'A.Gopnik,C.Glymour,D.Sobel,L.Schulz,T.Kushnir,and']

to此文:

代码语言:javascript
复制
['H.Geffner', 'R.DeSousa', 'R.Dechter', 'H.Dreyfus', 'J.Elster', 'J.Evans', 'H.Geffner', 'H.Geffner', 'H.Geffner', 'M.Genesereth', 'G.Gigerenzer', 'G.Gigerenzer', 'A.Gopnik', 'C.Glymour', 'D.Sobel', 'L.Schulz', 'T.Kushnir']

所有的名字都由“.”、“和”或“”分隔开。

我们试着用数“数”来把它们分开。它有,当它达到2,它附加到一个新的列表,没有额外的信息,但我认为可能有一种不同的方式来做它。

到目前为止,这就是我所掌握的。

代码语言:javascript
复制
names = (the huge list I showed above)
just_names = []
current_name = ""
number_of_periods = 0
for item in names:
    index = 0
    while index < 8:
        if item[index] != ".":
            current_name = current_name + item[index]
           # print(current_name)
            index = index + 1
        else:
            number_of_periods= number_of_periods+ 1
            index = index + 1
            if ponto >= 2:
                just_names.append(current_name)
                current_name = ""
EN

回答 3

Stack Overflow用户

发布于 2019-08-02 17:47:38

我认为最简单的方法是使用regex:

代码语言:javascript
复制
import re

data = ['H.Geffner/AI:FromProgramstoSolvers7', 'R.DeSousa.', 'R.Dechter.', 'H.Dreyfus.', 'J.Elster.', 'J.Evans.Dual-processingaccountsofreasoning,judgment,', 'H.Geffner.Heuristics,planning,cognition.InR.Dechter,', 'H.Geffner,andJ.Y.Halpern,editors,', 'H.Geffner.Computationalmodelsofplanning.', 'M.Genesereth,N.Love,andB.Pell.Generalgameplay-', 'G.Gigerenzer.', 'G.GigerenzerandP.Todd.', 'A.Gopnik,C.Glymour,D.Sobel,L.Schulz,T.Kushnir,and']

regex = r"^(\w+\.?\w+)"
matches = [re.search(regex, item) for item in data]
names = [match.group(0) if match else None for match in matches]

print names

既然你在问这个问题,我猜你不知道。语法有点难看,但在某些情况下非常有用。

查看本网站,了解^(\w+\.?\w+)的含义。以下是基本知识:

  • ^我们要寻找的必须在字符串的开头
  • (...) --我们将想要提取的位封装在括号中
  • \w查找单词字符(字母和数字)
    • +量词用于\w,匹配其中一个或多个

  • \.查找点字符
    • 用于?\.量词允许其中的一个或零

  • \w+与以前相同:一个或多个单词字符,但在点之后
票数 0
EN

Stack Overflow用户

发布于 2019-08-02 17:55:21

代码语言:javascript
复制
import re

names=['H.Geffner/AI:FromProgramstoSolvers7', 'R.DeSousa.', 'R.Dechter.', 'H.Dreyfus.', 'J.Elster.', 'J.Evans.Dual-processingaccountsofreasoning,judgment,', 'H.Geffner.Heuristics,planning,cognition.InR.Dechter,', 'H.Geffner,andJ.Y.Halpern,editors,', 'H.Geffner.Computationalmodelsofplanning.', 'M.Genesereth,N.Love,andB.Pell.Generalgameplay-', 'G.Gigerenzer.', 'G.GigerenzerandP.Todd.', 'A.Gopnik,C.Glymour,D.Sobel,L.Schulz,T.Kushnir,and']
just_names=[]
for name in names:
    found=re.findall('[A-Z]\.[A-Za-z]+',name)
    for n in found:
        just_names.append(n)
print(just_names)

或1行回答:

代码语言:javascript
复制
import re

names=['H.Geffner/AI:FromProgramstoSolvers7', 'R.DeSousa.', 'R.Dechter.', 'H.Dreyfus.', 'J.Elster.', 'J.Evans.Dual-processingaccountsofreasoning,judgment,', 'H.Geffner.Heuristics,planning,cognition.InR.Dechter,', 'H.Geffner,andJ.Y.Halpern,editors,', 'H.Geffner.Computationalmodelsofplanning.', 'M.Genesereth,N.Love,andB.Pell.Generalgameplay-', 'G.Gigerenzer.', 'G.GigerenzerandP.Todd.', 'A.Gopnik,C.Glymour,D.Sobel,L.Schulz,T.Kushnir,and']
just_names=[n for l in [re.findall('[A-Z]\.[A-Za-z]+',name) for name in names] for n in l]
print(just_names)

产出:

代码语言:javascript
复制
['H.Geffner', 'R.DeSousa', 'R.Dechter', 'H.Dreyfus', 'J.Elster', 'J.Evans', 'H.Geffner', 'R.Dechter', 'H.Geffner', 'J.Y', 'H.Geffner', 'M.Genesereth', 'N.Love', 'B.Pell', 'G.Gigerenzer', 'G.GigerenzerandP', 'A.Gopnik', 'C.Glymour', 'D.Sobel', 'L.Schulz', 'T.Kushnir']
票数 0
EN

Stack Overflow用户

发布于 2019-08-02 18:02:10

上述regex将不涵盖“and”的情况:我建议采用以下模式:

代码语言:javascript
复制
p = re.compile("([a-zA-Z]*[\.][a-zA-Z]+)\.*[and]*[\/]*")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57331069

复制
相关文章

相似问题

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