Python从列表中删除一个措辞字符串
发布时间:2020-12-20 11:49:37 所属栏目:Python 来源:网络整理
导读:words = [['hey','hey you'],['ok','ok no','boy','hey ma']] 我有一个包含字符串的列表列表.我理解如何从列表中删除特定元素,但不知道如何删除只有一个单词的元素.我想要的输出是: final = [['hey you'],['ok no','hey ma']] 我正在尝试但我认为这是完全错
words = [['hey','hey you'],['ok','ok no','boy','hey ma']] 我有一个包含字符串的列表列表.我理解如何从列表中删除特定元素,但不知道如何删除只有一个单词的元素.我想要的输出是: final = [['hey you'],['ok no','hey ma']] 我正在尝试但我认为这是完全错误的…. remove = [' '] check_list = [] for i in words: tmp = [] for v in i: a = v.split() j = ' '.join([i for i in a if i not in remove]) tmp.append(j) check_list.append(tmp) print check_list 解决方法
你可以做:
words = [['hey','hey ma']] final = [[x for x in sub if ' ' in x.strip()] for sub in words] # [['hey you'],'hey ma']] 我只是在所有字符串中搜索空格. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |