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 3 years ago.
Improve this question
I need your help here.
by use Python, I want to find a way to filter out element in list that has no double integer(at least 2 continuous number).
like in [314120,420423,432192,444689,112345], I want filter out 314120 and 420423,432192.
because 444689 has 444 and 112345 has 11. so they shall not be filter out as a expected result.
thank you
You could simply use something like this:
import re
result = [x for x in numbers if !re.search(r"(.)\1", str(x))]
This document represents a way to check the uniqueness of characters. Suppose isUniqueChars is the method and numbers is your list of integers, you can find the one which all its numbers are unique by following piece of code:
unique = [n for n in numbers if isUniqueChars(str(n))]
Related
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 11 months ago.
Improve this question
This is my first time posting to Stackoverflow.
I'm trying to solve this problem here: https://codingbat.com/prob/p270692?parent=/home/konstans#stuy.edu/all
When looking at all hailstone sequences from 1 to z, maxHail(z) will return the starting number that creates the longest sequence. In other words, maxHail(n) looks at hailLen(1), hailLen(2) ... hailLen(n) and returns the number from 1-n that had the largest hailstone sequence. You should look at the hailLen() problem before working on this. You should use your solution from the hailLen() problem. ( http://codingbat.com/author/p264289 ) since hailLen(3) is larger than the hailLen of 4 or 5, maxHail of 3,4,5 all return 3. Since 6 has a longer sequence, maxHail(6) gives us 6. remember: Use the hailLen function you already wrote!
Here's my code and the output:
However, I'm not sure where this goes wrong - I checked line-by-line and couldn't see anything wrong. Could anyone help me fix this? Thank you!
I see what is wrong - hailLen returns lenght of sequence and the question is about index for which the sequence is the longest. Just store it in variable
if (res := hailLen(i)) > counter: # it's python 3.8 syntax
counter = res
index = i
return index
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 1 year ago.
Improve this question
Below is my list,
['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
And I need to sort the list and filter it to a below format. Please suggest a quicker way to achieve it
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
When your format is fixed and always starts with "pending" you can use the normal sorted function and count the / in a list comprehension.
>>> values = ['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
>>> sorted(x for x in values if x.count('/') == 2)
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
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 2 years ago.
Improve this question
I'm trying to sort a list with the following values
My list
list_to_sort = ['1.1.0','1.2.3','1.3.10','1.3.9','x23','hi']
My expected output
['1.1.0','1.2.3','1.3.9','1.3.10','hi','x23']
What i have tried:
list_to_sort .sort()
I have never done something like this so I would love to know how I could solve it. Thanks!
This was, needless to say, an interesting one to solve
list_to_sort = ['1.1.0', '1.2.0','4.3.0' ,'1.3.10','1.3.9','1.31.0','x23','hi']
def weirdFloats(x):
try:
#trips an error when given something like "hi"
return [int(i) for i in x.split(".")]
except:
return [float("Inf"), x]
print(sorted(list_to_sort, key= weirdFloats, reverse= False))
['1.1.0', '1.2.0', '1.3.9', '1.3.10', '1.31.0', '4.3.0', 'hi', 'x23']
Note that im not sure if this properly accounts for things that cant be interpreted as what you call "floats" (they look more like version #'s to me) such as what seem to be the outliers here hi and x23
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 2 years ago.
Improve this question
Say i inputted "1+2x+3x^2" or "1-2x+3x^2"
How do i make a function that splits and makes a list of each term like [1, 2x, 3x^2] or [1, -2x, 3x^2].
I have been stumped at this for a while, for now the function im using currently, seperates only at the "+" so to get a list like [1, -2x, 3x^2] i have to input
"1+-2x+3x^2"
Note: I won't be using complex polynomials but simple looking ones that doesn't include parenthesis or fractions.
import re
s = '1+2x^2-3x^-3'
s = re.sub('\^-', 'neg', s) # trick to deal the negative powers
print(s)
s=s.replace('-','+-')
s=s.replace('neg','-')
sub = s.split('+')
print(sub)
This will split your negative power variables correctly.
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 9 years ago.
Improve this question
Is it possible to combine the two statements inside 'for' loop.
num_pro=raw_input("ENTER THE NUMBER OF PRODUCTIONS: ")
right=[];left=[];
for i in range(int(num_pro)):
l,r=raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->')
right.append(r);left.append(l)
sample input: E->abc
Append tuples to one list, then split out the lists using zip():
entries = []
for i in range(int(num_pro)):
entries.append(raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->'))
left, right = zip(*entries)
zip(*iterable) transposes the nested list; columns become rows. Because you have two 'columns' (pairs of values), you end up with two rows instead.
Not without making it more complex. Each method needs to be called individually, and the only way to do that is either explicitly, as you have done, or in a loop.
If you are willing to store the whole production (which isn't necessarily a bad idea, since it keeps both sides synchronized) then just append the split result instead.
productions = []
for ...
productions.append(....split('->'))