Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以如果我有这个:
a = "hi" b = ["hello there", "goodbye", "nice to meet you"]
所以如果 len(a) 是 2 那么是否有可能在 b 中找到一个长度相同的字符串?在这种情况下是“你好”
对于“hi”,我在数字符,但对于“hello there”,我在数单词。
我尝试拆分字符串: b = [["hello", "there"], "goodbye", "nice to meet you"] 但我只能找到一种方法将它们一一拆分
b = [["hello", "there"], "goodbye", "nice to meet you"]
如果您的意思是元素中的单词数b应与中的字符数相同a:
b
a
a = "hi" b = ["hello there", "goodbye", "nice to meet you"] next(w for w in b if len(w.split()) == len(a)) # returns 'hello there' [w for w in b if len(w.split()) == len(a)] # returns ['hello there']