鱼C论坛

 找回密码
 立即注册
查看: 3412|回复: 3

[已解决]如何删除list中指定的元素

[复制链接]
发表于 2017-2-4 06:00:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如何删除list中指定的元素?

word = ['+1\tThis book is such a life saver.  It has been so helpful to be able to go back to track trends, answer pediatrician questions, or communicate with each other when you are up at different times of the night with a newborn.  I think it is one of those things that everyone should be required to have before they leave the hospital.  We went through all the pages of the newborn version, then moved to the infant version, and will finish up the second infant book (third total) right as our baby turns 1.  See other things that are must haves for baby at [...]\n', '+1\tI bought this a few times for my older son and have bought it again for my newborn. This is super easy to use and helps me keep track of his daily routine. When he started going to the sitter when I went back to work, it helped me know how his day went to better prepare me for how the evening would most likely go. When he was sick, it help me keep track of how many diapers a day he was producing to make sure he was getting dehydrated. The note sections to the side and bottom are useful too because his sitter writes in small notes about whether or not he liked his lunch or if the playtime included going for a walk, etc.Excellent for moms who are wanting to keep track of their kids daily routine even though they are at work. Excellent for dads to keep track as my husband can quickly forget what time he fed our son. LOL\n', '+1\tThis is great for basics, but I wish the space to write things in was bigger. A lot times I need struggle trying to read what the caretaker wrote in because the spaces go together.\n']

如何删除  'is'

使word里面不再有'is' 这个单词?
最佳答案
2017-2-4 08:32:29
实际上列表中是3个长字符串,即3个元素,而这3个元素都不是'is','is'是在字符串中的子字符串,需要对每个字符串做替换操作,一条语句就OK了,用' is '而不是'is'是为了仅把单词去掉,而像this中的is就不去掉了。
  1. word = [i.replace(' is ', ' ') for i in word]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-2-4 08:32:29 | 显示全部楼层    本楼为最佳答案   
实际上列表中是3个长字符串,即3个元素,而这3个元素都不是'is','is'是在字符串中的子字符串,需要对每个字符串做替换操作,一条语句就OK了,用' is '而不是'is'是为了仅把单词去掉,而像this中的is就不去掉了。
  1. word = [i.replace(' is ', ' ') for i in word]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-2-4 18:07:18 | 显示全部楼层
表示我只知道语法
用     del
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-8 03:57:49 | 显示全部楼层
word[0].split()
b = []
for i in range(len(a)):
    if a[i] != 'is':
        b.append(a[i])
b




先把word里的单词split成一个个字符串,然后再建立一个新的列表,每个word里面的元素判断是否是'is'如果是的话就不放进新的列表,如果不是的话就放进新的列表

b
Out[92]:
['+1',
'This',
'book',
'such',
'a',
'life',
'saver.',
'It',
'has',
'been',
'so',
'helpful',
'to',
'be',
'able',
'to',
'go',
'back',
'to',
'track',
'trends,',
'answer',
'pediatrician',
'questions,',
'or',
'communicate',
'with',
'each',
'other',
'when',
'you',
'are',
'up',
'at',
'different',
'times',
'of',
'the',
'night',
'with',
'a',
'newborn.',
'I',
'think',
'it',
'one',
'of',
'those',
'things',
'that',
'everyone',
'should',
'be',
'required',
'to',
'have',
'before',
'they',
'leave',
'the',
'hospital.',
'We',
'went',
'through',
'all',
'the',
'pages',
'of',
'the',
'newborn',
'version,',
'then',
'moved',
'to',
'the',
'infant',
'version,',
'and',
'will',
'finish',
'up',
'the',
'second',
'infant',
'book',
'(third',
'total)',
'right',
'as',
'our',
'baby',
'turns',
'1.',
'See',
'other',
'things',
'that',
'are',
'must',
'haves',
'for',
'baby',
'at',
'[...]']
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-25 06:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表