This question already has answers here:
creating sum of odd indexes python
(3 answers)
Closed 6 years ago.
Say I call this function getSumOdds([1,2,3,4,5]). This should return 6 since 2 + 4 are located at odd indices. I have figured out how to get it to return what numbers are located at odd indices, but I am unsure how to add them.
getSumOdds(aList):
return aList[1::2]
All this does is return what numbers are at the odd locations. I also want it just to return aList. I have tried using sum() in various ways but nothing seems to work. Anything would help!!
getSumOdds(aList):
return sum(aList[1::2])
Are you looking for sum(aList[1::2]) ?
Related
This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]
This question already has answers here:
Logical indexing with lists
(3 answers)
Closed 3 years ago.
I have no idea how this code works and it returns an output of the first element in the list.
I have tried with different inputs since though I'm getting all the same output.
[1,2,3][bool("")]
Since bool("")==False. Also, False is 0. Therefore [1,2,3][bool("")] is same as [1,2,3][0] which return first element of list ultimately.
bool("")==0
This is because "" is regarded as false. This means your statement will be,
[1,2,3][0]
which returns the first element of the list. In this case
1
Source: https://www.programiz.com/python-programming/methods/built-in/bool
This question already has answers here:
Python array multiply
(3 answers)
Closed 5 years ago.
I saw this questions a couple other places but all the fixes dont work for me code. If someone could help that would be great.
import random
cvalues=[]
for i in range(50):
cvalues.append(random.randrange(0,16))
float_cvalues=[float(i) for i in cvalues]
print(float_cvalues)
nvalues=[((.4*(float_cvalues)-.8))]
print(nvalues)
Multiplying a sequence in Python is interpreted as an attempt to create multiple sequences, see this post.
You can instead use another list comprehension:
nvalues=[.4*i-.8 for i in float_cvalues]
Or for instance switch to numpy arrays.
This question already has answers here:
How do I create variable variables?
(17 answers)
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
Closed 5 years ago.
I'm a rookie at Python so I don't know much about the limits of the language, but it seems that you can't write a function or a program that creates a list on its own.
I'm working with binary lists. I'm working with 7 bits so, as you can guess, I need 128 different lists to store all possibilities.
Let me know if its possible, or if not please let me know why and what else could work for my needs.
def list_creator:
n=128
"create a new_list n times with 7 of length"
"Should display something like this"
list1=[None]*7
list2=[None]*7
list(n)=[None]*7
You should make a list containing 128 empty lists and add to those.
This question already has answers here:
How to create a range of numbers with a given increment
(6 answers)
Closed 8 years ago.
Does anybody know whether Python can do the same thing as for i= 1:2:5 in Matlab? So i=1,3,5.
I know I can use other approaches to do this, but I want to know the equivalent form in Python.
try:
for i in xrange(1,6,2):
print i
This print:
1
3
5
Use xrange instead of range if you are using python 2.x because it is more efficient as it generates an iterable object, and not the whole list.
Use the range function:
for i in range(1, 6, 2):
print(i)