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)
Related
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.
I would like to get the indexes of a given list (images) and put it into a list.
So far I've tried:
list(images)
But this only returns itself.
list(images.keys())
But this gives me an error that images has no attribute keys
Is there a way to get all the indexes into its own list?
You could simply create a range as long as the list:
range(len(images))
This returns Range object, which is usable in most of the same contexts as a list. If you really wanted it as a list however, you could just put the range in a list:
list(range(len(images)))
I am trying to get a list of elements using a for loop to index my previous list. Here's the code:
for index in range(1,810):
list.extend(t[index])
list.extend(t[index+1])
list.extend(t[index])
I already have a list named "list" and t is an other list.
To be more specific I want to make my list like this.
t1,t2,t1,t2,t3,t2,t3,t4 etc.
I get this error TypeError: 'float' object is not iterable.
You should be using list.append since list.extend works on iterables; i.e. you can do stuff like lst=[1,2,3];lst.extend([4,5]) which would give you [1,2,3,4,5]
see this link if you want to read more
The other answers suggesting you use append fix your issue, but appending is very inefficient. You should use a list comprehension. Also, "list" is a keyword, so you should use another name.
my_list = [item for index in range(1,810) for item in [t[index],t[index+1],t[index]] ]
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
Below are snippets of code that is giving me some problems. what i am trying to do is find every occurrence of a 356 day high. To do this i am trying code similar to the one below, but getting an exception on the "for i" line: 'builtin_function_or_method' object has no attribute 'getitem'
Quote = namedtuple("Quote", "Date Close Volume")
quotes = GetData() # arrray
newHighs = []
for i,q in range[365, len(quotes)]: #<--Exception
max = max(xrange[i-365, i].Close) #<--i know this won't work, will fix when i get here
if (q.Close > max):
newHighs.append(i,q)
Any help on fixing this would be appreciated. Also any tips on implementing this in an efficient manner (since quotes array currently has 17K elements) would also be nice.
range is a function that returns a generator (or list in python2). Thus, it must be called as a function range(365, len(quotes)), which will return all the numbers from 365 to len(quotes).
Square brackets imply indexing, like accessing items in a list. Since range is a function, not a list, it throws an exception when you try to access it.
"Range" is a function. This means you use circle brackets, not square ones. This is the same deal with "xrange" in the line below. I understand why you'd think to use the square brackets, but what "range" does is create the list using those arguments. So it's not the same as when you want elements m to n of a list.