What does i for i in s mean? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def CodelandUsernameValidation(s):
if len(s)>4 and len(s)<25 and s[0].isalpha() and [i for i in s if i.isalnum() or i=="_"]!=[] and s[-1]!="_":
return True
else:
return False
# keep this function call here
print(CodelandUsernameValidation(input()))

It's a list comprehension if you expand, it'll look like this ->
result = []
for i in s: # will fetch element one by one from iterable
if i.isalnum() or i=="_": # checking condition
result.append(i) # if true, then append it to the list
This above code can be rewritten as -
result = [i for i in s if i.isalnum() or i=="_"]

That produces a list of those characters in s that are either alphanumeric or underlines. The code is actually incorrect, because it will pass if ANY of the characters are alphanumeric or underline, whereas the intent surely was that ALL of the characters must be alphanumeric or underline. Here's a better way to write it:
def CodelandUsernameValidation(s):
return 4 < len(s) < 25 and s[0].isalpha() and all(i.isalnum() or i=='_' for i in s) and s[-1] != '_'
print(CodelandUsernameValidation(input()))

Related

change the order of numbers from the back [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
they need to order with a problem
so I have the text in a certain order of numbers, something like gematria
input [12345] is what we call gematria and what do they need?
they need to line up the digits backwards
[54321]
have a different count and I would need help with that rather than doing twenty different if
def shiftall(s, n):
n %= len(s)
return s[n:] + s[:n]
it didn't help me much it only moves the simple text
For strings:
return s[::-1]
For integers:
return str(s)[::-1]
Note: This would go inside def shiftall(s, n):
Additional note: Now you don't even need the parameter n
If you want to reverse a number, then you can convert it to a string, reverse the string, and then convert it back to a number.
num = 12345
str_num = str(num)
# reverse and convert
num = int(str_num[::-1])
input=[12345,43545436,88888,843546]
def shiftall(s):
d=[]
for i in s:
res=''.join(reversed(str(i)))
d.append(int(res))
return d
print(shiftall(input))

how to remove a string element from the list if it is already there in another string element? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I want to keep a term which is longest in list element.
Input:
List1=['this','is','to','ask','this question']
Expected output:
List1=['is','to','ask','this question']
I assume you want to remove "this" because it's a substring of "this question". You're choosing to remove "this" and not "is" because "this" is longer than "is" right?
I would first sorted this list by length of the strings, then double for loop (i and j) to see if i is in j, then remove the last occurrence, because it is the longest.
If you confirm, that my assumption was right I'll try to code it.
EDIT :
Since you confirmed my assumption, here's the code :
List1=['this','is','to','ask','this question']
def removeSubString(myList):
sortedList = sorted(myList, key=lambda x: len(x))
for i in range(len(sortedList), -1, -1):
for j in range(i + 1, len(sortedList)):
if sortedList[i] in sortedList[j]:
myList.remove(sortedList[i])
return myList
return myList
print(removeSubString(List1))

python: list of tuples search [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Need some help here
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
if the last 6 digits are located to return the first digit.
example if finds 10,11,29,30,36,47 return 2
You can use next similair to user's approach:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
to_find = [10,11,29,30,36,47]
print(next(n for n, *nums in num if nums == to_find))
2
You can use next with a conditional generator expression:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
search = 11
next(first for first, *rest in num if search in rest)
# 2

Why are repeated letters skipped in some strings? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
this code is meant to 'explode' a given string s
def string_splosions(s):
"""erp9yoiruyfoduifgyoweruyfgouiweryg"""
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
For some reason this code can return the correct 'explosion' for most words however words which have a repeated letter they do not print correctly.
examples.
Correct outputs is would be:
Code --> CCoCodCode
abc --> aababc
pie --> ppipie
incorrect outputs when s is
Hello --> HHeHelHelHello (should be HHeHelHellHello)
(Note: in the incorrect output there should be 1 more l in the second to last repeat.)
You should transcribe the code instead of posting a picture:
def string_splosion(s):
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
The problem is that index(i) returns the index of the first instance of that character, which is 2 for both l's in "Hello". The fix is to just use the index directly, which is also simpler:
def string_splosion(s):
new = ''
for i in range(len(s)):
new += s[:i+1]
return new
Or even:
def string_splosion(s):
return ''.join(s[:i+1] for i in range(len(s)))

How to change iterations in a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
file = ['Tool','Cool','Pool']
word = 'Cool'
guesses = ['T','P']
for i in range(len(file)):
if len(file[i]) == len(word):
for j in range(len(file[i])):
print(file[i][j])
for k in range(len(guesses)):
print(guesses[k])
if file[i][j] == guesses[k]:
i + 1
In my piece of code, I am having issue moving to the next item in the list file. When file[0][0] == 'T' and guesses[0] == 'T' occurs I would like to move on to the next item in the file list. I figured i + 1 was suitable but it doesn't seem to work.
What you want to do here is to use continue, but since you have a few nested loops it won't work correctly so we can use a helper method to reduce it to one for loop and then use continue:
def find_any(what, where):
for that in what:
if that in where:
return True
return False
file = ['Tool','Cool','Pool']
word = 'Cool'
guesses = ['T','P']
for i in range(len(file)):
if len(file[i]) == len(word) and find_any(guesses, file[i]):
continue # This will skip to the next file
# Do things with file[i] here

Categories