簡體   English   中英

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

[英]Modifying strings inside a list in Python

我想創建一個程序來轉換列表:

['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']

對此:

['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']

所有名稱都與其他信息用“。”,“ and”或“,”分開

Iv'嘗試通過計算'。'的數量來分離它們。 它具有並且當它達到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 = ""

我認為最簡單的方法是使用正則表達式:

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+與之前相同:一個或多個文字字符,但在點后
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']

上面的正則表達式將不涉及“ and”的情況:我建議采用以下模式:

 p = re.compile("([a-zA-Z]*[\\.][a-zA-Z]+)\\.*[and]*[\\/]*") 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM