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 11 months ago.
Improve this question
I have a code which compares two lists
a=[10,20,30,40,50,60,40,45]
x=[10,50,5,100,15,60]
z=([i > j for i, j in zip(a[-len(x):], x)])
print(z)
I need to rewrite it in the following format but current wording
returns error. How to correct it?
a=[10,20,30,40,50,60,40,45]
x=[10,50,5,100,15,60]
for i in range(a[-len(x):],x):
if a[i]>x[i]:
print('yes')
else:
print('no')
The issue is you're using range incorrectly. range takes integer arguments while you're passing the array
....
start_range = -len(x)
for i in range(len(x)):
if a[start_range]>x[i]:
print('yes')
else:
print('no')
start_range+=1
...
Now I'm not sure what is it you expect from the output since the first snippet returns boolean in an array while the second snippet would print out yes and no in separate lines.
However, you can build the logic around the above since the only problem was the loop range.
Related
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 months ago.
Improve this question
If I have the following code
if "ERB-776-RAD" in my_data.any():
print("I found it!")
Where the my_data is something as follows:
my_data = ['ERA-776-TRF','FDS-342-FHS','EBR-776-RAD'...]
How would I print the actual value I am looking for (ERB-776-RAD) instead of printing "I found it"?
Use the in statement to test if the list contains the string. Then you can print anything you want if it's True. You can use string formatting to insert the value dynamically (in the code below, I am using f-strings).
my_data = ['ERA-776-TRF','FDS-342-FHS','EBR-776-RAD']
target = "ERB-776-RAD"
if target in my_data:
print(f"I found {target}")
As #chepner commented, lists do not have an any method. (If you saw .any(), it might have been with a numpy array or pandas dataframe.)
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 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
the expected output of this code is [x**2,x+2] but when i try to run it the system gives me an error saying that the variable 'x' is not defined.how to fix this issue. I would like to get the map the expression to a graph.
l=[1,2]
out=[]
for i in l:
if i==1:
y=x**2
out.append(y)
if i==2:
y=x+2
out.append(y)
print(out)
At y=x**2 you have used the variable x but you have not defined it, which is causing the error. I'm assuming you want it as a string, so just do y="x**2".
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 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 3 years ago.
Improve this question
I need to remove a certain element from a list of dictionaries. Unfortunately, the only reference to the object needing removal is a place in another list:
enemy_list[num]
I need to take out enemy_list[num] from everyone.
ii = 0
for i in enemy_list:
print(ii, ':', enemy_list[ii])
ii += 1
num = input("Which of your opponents would you like to eliminate? ")
num = int(num)
del everyone[enemy_list[num]]
I cannot remove it from only enemy_list because it is reset multiple times as everyone in list everyone takes a turn.
I tried this question, but it only worked from removing entire lists from eachother. Is there a way I could change it to work for this case?
Thanks for your time!
everyone.remove(enemy_list[num])