简体   繁体   English

查找字符串列表中的至少一个字符串是否没有字符(python)

[英]Find if at least one string of a list of strings hasn't a character (python)

I want to check if, among the elements of a list of strings, at least one of the element has not a "-" in it. 我想检查一下,在一个字符串列表的元素中,至少一个元素中是否没有“-”。 I tried : 我试过了 :

print not any("-" in elem for elem in my_list)

With: 带有:

my_list = ['gta', '---', '---', 'gta']

it should return True and with : 它应该返回True并带有:

my_list = ['gt-', 'ac-', 'ca-', 'gt-']

It should return False . 它应该返回False It's always False for now, is there a one-line command to do that ? 目前始终为False ,是否有单行命令来执行此操作? I could choose the easy way and make my code on several lines, but I want to practice these kinds of commands :). 我可以选择简单的方法,并在多行代码中编写代码,但是我想练习这些命令:)。

如评论中所述,由于not any变成all not您不需要使用not all

You can use re.findall to check if the entire string in the list is composed of "-" : 您可以使用re.findall检查列表中的整个字符串是否由"-"组成:

import re
def check_string(s):
   return any(re.findall('^-+$', i) for i in s)

lists = [[['gta', '---', '---', 'gta'], True], [['gt-', 'ac-', 'ca-', 'gt-'], False]]
for a, b in lists:
   assert check_string(a) == b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查字符串是否包含列表中的至少一个字符串 - Check if a string contains at least one of the strings in a list Python:删除列表中至少由同一列表中的一个其他字符串包含的字符串 - Python: Remove Strings in a List that are contained by at least one other String in the same List Python在索引处将一个字符串切成两个字符串,且每个字符有一个字符重叠,并将这两个字符串作为列表中的两个元素返回 - Python slicing a string into two strings at the index with a one-character overlap and return the two strings as two elements in a list 用python中的字符串列表替换一个字符串列表 - Replace one string list with list of strings in python 一线:查找字符串是否在字符串列表中 - One liner: Find if string is in list of strings 查找至少包含Python中的点列表之一的面 - Find polygons that contain at least one of a list of Points in Python python regex查找至少包含一个字母的字母数字字符串 - python regex to find alphanumeric string with at least one letter 比较字符串和字符串列表以查找Python中的字谜 - Comparing a string with a list of strings to find anagrams in Python Python - 在字符串中查找字符串列表的出现 - Python - find occurrences of list of strings within string Python,在列表的字符串中找到特定字符 - Python, find a specific character in a string in a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM