+ 1
Is there a way to check if multiple entries in a list show up in a string? (Python)
I'm working on the Password Validator challenge, and one of the things I need to do is to check if any numbers appear in the inputted password. So, what I did was I made a list of all numbers from 0 - 9, and now I'm trying to count the number of times a number shows up in the password. I know that .count() works for individual numbers, but I was wondering if there was a way to shorten it to a single line. I already tried to input the name of the list into count, like .count(listname), but that didn't seem to work, so now I'm kind of stuck.
4 odpowiedzi
+ 2
you can use two ways regular expressions ask if it is in the list with "in"
import re
x=["1","3","4","5"]
print("1" in x)#if in list
#regex
passw="jsdfk8dfkk99"
passw2="jdfgjskdfg"
ans=re.search('(\d)+', passw)
print(True if ans else False)
ans2=re.search('(\d)+', passw2)
print(True if ans2 else False)
+ 1
Matt Dahl this is an interesting challenge. I just rewrote my own code that was using a for loop. Now it counts in one line by using functional programming, which is explained later in the Python course that you are in.
numCnt = sum(map(lambda c: '0'<=c<='9', pwd))
Maybe there are cleaner ways to count how many characters are numbers. Looking forward to seeing other answers!
0
Brian Yup, I used your code for the numbers, and I ended up just individually counting the special characters and adding them together. Thanks for the tip!
0
L.M.Paredes I'm looking into this, but I don't think I really understand the code yet. I'll keep working through my course and see if I can figure it out. Thanks for the answer!