2017-06-13 241 views
-2
names = """ 
Emma  
Removed from list 
Noah  
Removed from list 

names = " ".join(names.splitlines()) 
nameslist = names.split(" ") 
newnames = list() 
for n in range(0, len(nameslist)): 
# print(n) 
    if not(nameslist[n] == "Removed" or nameslist[n] == "from" or nameslist[n] == "list"): 
     newnames.append(nameslist[n]) 
     list.remove:"Removed" 

print(newnames) 

這就是返回蟒蛇從列表

['', 'Emma\t', 'Noah\t', 'Liam\t', 'Olivia\t', 'Ava\t', 'Ethan\t', 'Mason\t', 'Sophia\t', 'Lucas\t', 'Mia\t', 'Isabella\t', 'Oliver\t', 'Aiden\t', 'Charlotte\t', 'Amelia\t', 'Elijah\t', 'Harper\t', 'James\t', 'Benjamin\t', 'Emily\t', 'Abigail\t', 'Logan\t', 'Jacob\t', 'Madison\t', 'Jackson\t', 'Sofia\t', 'Lily\t', 'Michael\t', 'Avery\t', 'Daniel\t', 'Carter\t', 'Ella\t', 'Aria\t', 'Gabriel\t', 'Evelyn\t', 'Luke\t', 'Alexander\t', 'Chloe\t', 'Riley\t', 'William', '', 'Lillian\t', 'Samuel\t', 'Dylan\t', 'Savannah\t', 'Hunter\t', 'Violet\t', 'Adam\t', 'Eva\t', 'Claire\t', 'Joseph\t', 'Leo\t', 'Maya\t', 'Anthony\t', 'Leah\t', 'Camila\t', 'John\t', 'Aaliyah\t', 'Isaiah\t', 'Maria\t', 'Landon\t', 'Connor\t', 'Sarah\t', 'Aurora\t', 'Cameron\t', 'Anna\t', 'Max\t', 'Alice\t', 'Christian\t', 'Paisley\t', 'Josiah\t', 'Bella\t', 'Thomas\t', 'Luna\t', 'Nolan\t', 'Gabriella\t', 'Hudson\t', 'Brayden\t', 'Hazel\t', 'Kennedy\t', 'Muhammad\t', '', 'Evan\t', 'Hailey\t', 'Asher\t', 'Sophie\t', 'Christopher\t', 'Dominic\t', 'Eleanor\t', 'Caroline\t', 'Jeremiah\t', 'Samantha\t', 'Adrian\t', 'Sadie\t', 'Autumn\t', 'Colton\t', 'Cooper\t', 'Julia\t', 'Adeline\t', 'Ezra\t', 'Charlie\t', 'Quinn\t', 'Mateo\t', 'Piper\t', 'Austin\t', 'Peyton\t', 'Aaron\t', 'Elena\t', 'Madelyn\t', 'Jordan\t', 'Nicholas\t', 'Ruby\t', 'Gianna\t', 'Jace\t', 'Easton\t', 'Emilia\t', 'Ariana\t', 'Gavin\t', 'Arianna\t', 'Parker\t', 'Jonathan\t', 'Mackenzie\t', '', 'Ian\t', 'Naomi\t', 'Aubree\t', 'Carson\t', 'Isabelle\t', 'Chase\t', 'Alex\t', 'Kaylee\t', 'Eliana\t', 'Jaxson\t', 'Alexa\t', 'Ayden\t', 'Clara\t', 'Xavier\t', 'Serenity\t', 'Tyler\t', 'Cora\t', 'Zachary\t', 'Greyson\t', 'Taylor\t', 'Annabelle\t', 'Elias\t', 'Harrison\t', 'Kinsley\t', 'Lydia\t', 'Tristan\t', 'Kylie\t', 'Roman\t', 'Valentina\t', 'Bryson\t', 'Jason\t', 'Reagan\t', 'Lilly\t', 'Madeline\t', 'Sawyer\t', 'Kayden\t', 'Allison\t', 'Bentley\t', 'Cole\t', 'Declan\t'] 

這是名稱的列表,而無需從列表後綴刪除,但它增加了一個\ t人知道爲什麼刪除元素?它可以修復嗎?

+1

1.此代碼有語法錯誤(什麼呢'list.remove: 「刪除」'意思) 2.本代碼沒有什麼補充說: 「」'\ t'。如果您從文件中讀取數據,則需要在列表中的每個元素上調用'strip'來分割''返回。 – DeepSpace

+0

你的代碼應該是一個巨大的三引號字符串嗎? – khelwood

+0

'\ t'不是來自您的代碼,而是來自您正在閱讀的文件。 – rassar

回答

0

假設這是你的意思......

names = """ 
Emma 
Removed from list 
Noah 
Removed from list 
""" 

names = " ".join(names.splitlines()) 
nameslist = names.split(" ") 
newnames = list() 
for n in range(0, len(nameslist)): 
# print(n) 
    if not(nameslist[n] == "Removed" or nameslist[n] == "from" or nameslist[n] == "list"): 
     newnames.append(nameslist[n]) 
     list.remove:"Removed" 

print(newnames) 

這是你應該做的......(這是用Python 2.7的格式,但應該仍然工作)。

names = # Same as string above 
# One-liner for creating a list and removing any potential newlines or tabs 
nList = [ x.replace("\t", "") for x in names.split("\n") ] 
newList = [] # or list(), however you wish to do this 
for n in range(0, len(nList)): 
    # Filter out any line with "Removed from list" 
    # Also filter out blank lines 
    if ("Removed from list" not in nList[n] and 
     nList[n] is not ""): 
     newList.append(nList[n]) 
print(newList) 
# Returns ['Emma', 'Noah'] 
0

特殊字符\ t用於「生成」選項卡。這些字符被稱爲字符串文字。如果您想了解它們,請查看here

+0

好的,但你可以擺脫\ t?從輸出,因爲它不是所需的應用程序所需 –

+0

您可以使用列表[索引] =列表[索引] .replace(「\ t」,「」) –