簡體   English   中英

查找長度大於(x)的字符串

[英]Find string greater than (x) in length

如何搜索變量並查找長度大於20的單詞/字符串?

var = '''this is my example and I want
no find the following
string jakjajsiwjsjwijaksjdfjaiwjalskdjfiajwlajfklajsdfwi 
from this variable'''

謝謝

使用列表理解:

print [x for x in var.split() if len(x) > 20]

使用split遍歷單詞列表,並檢查每個單詞的len

for x in var.split():
    if len(x) > 20:
        print(x)

或者當然可以使用列表推導

long_words = [x for x in var.split() if len(x) > 20]
print(long_words)
var_split = var.split(' ')
long_word = ''

for word in var_split:
    if len(word) > 20:
        long_word = word

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM