समस्या विवरण
Series में उन शब्दों की संख्या गिनें जिनमें एक से अधिक विशेष वर्ण (special characters) हों।
इनपुट Series
0 fruits!! 1 *cakes* 2 $nuts 3 #drinks dtype: object
अपेक्षित आउटपुट
2
(fruits!! में दो '!', *cakes* में दो '*')
समाधान 1: लूप और विशेष वर्ण सूची
import pandas as pd
import string
l = ["Fruits!!","*Cakes*","$Nuts","#Drinks"]
data = pd.Series(l)
special_char = list(string.punctuation) # सभी विराम चिह्न
total_count = 0
for word in data:
chars_count = 0
for ch in word:
if ch in special_char:
chars_count += 1
if chars_count > 1:
total_count += 1
print("एक से अधिक विशेष वर्ण वाले शब्दों की संख्या:", total_count)
समाधान 2: Regex \W (गैर-शब्द वर्ण) और lambda (संक्षिप्त)
import pandas as pd
import re
l = ["fruits!!","*cakes*","$nuts","#drinks"]
data = pd.Series(l)
# \W गैर-शब्द वर्ण से मिलान करता है (विराम चिह्न, स्पेस आदि)
filtered = pd.Series(filter(lambda x: len(re.findall(r"\W", x)) > 1, l))
count = len(filtered)
print("गिनती:", count)
आउटपुट
2
नोट: \W अंडरस्कोर (_) को छोड़कर सभी गैर-शब्द वर्णों से मिलान करता है। केवल विराम चिह्न के लिए string.punctuation या [^\w\s] उपयोग करें।