我想要创建一个程序来转换这个列表:
['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此文:
['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,它附加到一个新的列表,没有额外的信息,但我认为可能有一种不同的方式来做它。
到目前为止,这就是我所掌握的。
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 = ""发布于 2019-08-02 17:47:38
我认为最简单的方法是使用regex:
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+与以前相同:一个或多个单词字符,但在点之后发布于 2019-08-02 17:55:21
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行回答:
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)产出:
['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']发布于 2019-08-02 18:02:10
上述regex将不涵盖“and”的情况:我建议采用以下模式:
p = re.compile("([a-zA-Z]*[\.][a-zA-Z]+)\.*[and]*[\/]*")
https://stackoverflow.com/questions/57331069
复制相似问题