i=5
while bool(controller[i]):
controller_jsn['Transition']=controller[i]
i+=1
I write a code above. And try to stop the loop when i is out of range. bool() is not the way. what methode should I use?
The more common way to write this code would be to iterate over the elements directly, instead of using indices. Use a slice if you need to start at the 5th element instead of looping over all elements.
for c in controller[5:]:
controller_jsn['Transition'] = c
but I hope you have more logic in there, because each iteration of the loop just overwrites the same value.
Related
I have the following code for pygame but it applies generally to python
expl_sounds = []
for snd in ['expl3.wav', 'expl6.wav']:
expl_sounds.append(pygame.mixer.Sound(path.join(snd_dir, snd)))
i would like to mute all items in the list individually instead of muting the entire application or mixer. I tried the following:
for i in expl_sounds:
expl_sounds[i].set_volume(Sound_Volume)
TypeError: list indices must be integers or slices, not Sound
I dont get the error message. The i in the loop is an integer and the mixer.set_Volume is a valid operation for the elements
What would be the correct way to iterate and apply the volume to each element ?
Your error comes from your misunderstanding of the syntax for i in expl_sounds. The i in the loop is an integer isn't true, i is one element of the expl_sounds, a pygame.mixer.Sound instance that you just add before
So use the object directly
for sound in expl_sounds:
sound.set_volume(Sound_Volume)
To do it by indices, do
for i in range(len(expl_sounds)):
expl_sounds[i].set_volume(Sound_Volume)
When you write for i in expl_sounds:, you are iterating on the elements in expl_sounds, so you can just do:
for i in expl_sounds:
i.set_volume(Sound_Volume)
The "i" in "for i in expl_sounds" is the sound object itself, not the array index.
Try the following:
for i in expl_sounds:
i.set_volume(Sound_Volume)
I have a creative problem that I want to solve.
Let's say if I have two list as below. I want to compare if all elements in the req_param list are also in the full_list. I know it is easy to do the same using a for loop and getting the answer. But I am trying to figure out if there is a python3 in-built fxn to do the same..
req_param = ['ele1','ele2','ele3','ele4]
full_param = [['ele1','ele2','ele3','ele4','ele6']
During the comparison, I don't care if there are additional elements in full_param list. I just care that if full_param has all the elements of the req_param, then somehow I want to return it true else, I want to return it false.
Currently, this works with the for loop. But really think there should be an inbuilt fxn like compare. The most important part is that each element may not come in the same order, so I am ok to sort my list before passing it to a fxn...
As was mentioned there are several ways:
Use all(): if all(item in full_list for item in req_param):
Use set(): if set(req_param).issubset(set(full_param)):
I figured out a different way you can solve the problem.
You could just use set() and len() to solve the problem instead of for loop
Here's how:
r = ['ele1','ele2','ele3','ele4']
f = ['ele1','ele2','ele3','ele4','ele6']
print(len(set(r)-set(f))==0)
use all keyword , it returns True if all the conditions are satisfied else it returns False
How can I change value of a variable which iterating over?
for i in range(10,30):
print('jdvcjhc')
I want to change I value like i do in while loop
i=1`enter code here`
while i<=10
print('dfhs')
You can't set conditional statements like (i<=10) in For loops. you just can set the range that "i" can change. Actually, if you need a condition for your loop you need to use While or For and If together.
That's my opinion.
Regards,
Shend
Edit: if you need to change the steps of adding or subtracting "i" you can use step in range function. ( range(start,stop,step)).
The range function returns a pre-calculated list of values, created from the input parameters, in this case - (10, 30). Unlike while loop, i here is a 'dummy' variable used as a placeholder for the values read from the list. Within a particular iteration, one can manipulate i as much as they like, but it will inevitably become overwritten with the subsequent list value upon calling the next iteration.
Hi I am trying to create a new array from a previous array. such that in new array the first element is mean of first 20 elements from existing array. Here is my code. I am not sure why its not working.
#Averages
RPMA=[]
for i in range(9580):
for j in range (0,191600):
a=RPM.iloc[j:j+20]
RPMA(i)= a.mean()
Looks to me like you're using the wrong kind of brackets. This line:
RPMA(i)= a.mean()
...should probably be this:
RPMA[i]= a.mean()
But I'm no Python expert. I guessing that it thinks RPMA(i) is a function because you use parentheses, in which case you would be trying to assign a value to a function call, like the error says.
However trying to assign a new value past the end of the array will result in an IndexError because the array element doesn't exist, and will need to be added. What you can do instead is this:
RPMA.append(a.mean())
...which will append the data to the end of the array (i.e. adding a new element).
Thanks everyone. As suggested by most of you, I made below changes in code and now its working just fine!
RPMA=[]
for j in range (0,191600, 20):
a=RPM.iloc[j:j+19]
RPMA.append(a.mean())
I've defined a function as below to try to interpolate between two sets of data. When I run it, I get the message:
for i, j in range(0, len(wavelength)):
TypeError: 'int' object is not iterable
I'm not sure what I'm doing wrong. Admittedly, I'm not very good at this.
def accountforfilter(wavelength, flux, filterwavelength, throughput):
filteredwavelength=[]
filteredflux=[]
for i in range(0, len(wavelength)):
if wavelength[i] in filterwavelength[j]:
j=filterwavelength.index(wavelength[i])
filteredwavelength.append(wavelength[i])
filteredflux.append(flux[i]*throughput[j])
elif wavelength[i]<filterwavelength[j]<wavelength[i+1]:
m=((throughput[j+1]-throughput[j])/(filterwavelength[j+1]-filterwavelength[j])
c=throughput[j]-(m*(wavelength[i]))
filteredwavelength.append(wavelength[i])
filteredflux.append(flux[i]*(m*wavelength[i]+c)
return filteredwavelength, filteredflux
range() returns a list of integers. By using for i,j in range() you are telling Python to unpack each item in range() to two values. But since those values are integers, which are a single piece of data, and thus not iterable, you get the error message.
Your code also looks a bit strange.
At first it seems like you want to loop over all combinations of wavelength/filterwavelength, which would be the same as
for i in range(len(wavelength)):
for j in range(len(filterwavelength)):
do_stuff()
but then you are modifying the j parameter inside the loop body, which I don't understand.
Regardless, there is probably a lot easier, and more clear, way to write the code you want. But from the current code it is hard to know what is expected (and should probably go in a separate question).
The problem is that the range only works with one variable like so:
for i in range(0, len(wavelength))
You try to use two variables at once so python tries to unpack an integer which is impossible. You should use the above. If you need two independent indices, use
for i in range(0, len(...))
for j in range(0, len(...))
Btw ranges always start with zero so you can save yourself some typing and use range(len(...)) instead.
You can use zip if you want.
check this: Better way to iterate over two or multiple lists at once
if you have two sets of data.
for i,j in zip(set1,set2):
print i,j