python从列表中删除元素

时间:2017-06-13 18:29:12

标签: python-3.x

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任何人都知道为什么?它可以修复吗?

2 个答案:

答案 0 :(得分:0)

特殊字符\ t用于“生成”标签。这些字符称为字符串文字。如果您想了解它们,请查看here

答案 1 :(得分: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']