This question already has answers here:
Squaring all elements in a list
(9 answers)
How to add an integer to each element in a list?
(12 answers)
How to multiply individual elements of a list with a number?
(4 answers)
Closed 4 years ago.
Given a list x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10] use a for loop to create a new list, y, that contains the value aSin(10a) for each value, a, in list x. Plot the results using plot(x,y).
I have...
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
for a in x:
print sin(a)*(10*a)
The code returns the correct sin values but I'm not sure how to get the values into a new list y..
Any help would be greatly appreciated.
Use list comprehensions here
>>> y = [sin(10*i)*(i) for i in x]
try this
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y=list()
for a in x:
y.append(sin(a)*(10*a))
the following code works
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y = [i*sin(10*i) for i in x]
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y=[]
for a in x:
y.append(sin(a)*(10*a))
Should work but do try to learn python!!
Try list comprehension:
y = [a * sin(10*a) for a in x]
Hope that helps.
Related
This question already has answers here:
Is there a simple way to delete a list element by value?
(25 answers)
Closed 3 months ago.
For example:
list = [1,7,2,8,9,2,7,9,2,8,2]
How can I remove two of the "2" without removing the others?
You can find the index of the one(s) you want to delete and use the function pop(index) to delete it/them.
Exemple:
your_list.pop(5) # Delete the 5th element of your list
Here's a little doc: https://www.programiz.com/python-programming/methods/list/pop
This should work by removing an i element n times from a list arr.
arr = [1,7,2,8,9,2,7,9,2,8,2]
def remove(arr, i, n = 1):
for _ in range(n):
arr.remove(i)
remove(arr, 2, 3)
print(arr)
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 3 years ago.
I am reading a list of integers separated by space and sorting them.
S = [int(x) for x in input().split(" ")]
S.sort()
Works fine, but when I do,
S = [int(x) for x in input().split(" ")].sort(" ")
Or,
S = list(int(x) for x in input().split(" ")).sort(" ")
S gives a NoneType element, why is that? Aren't they both he same thing? I am taking input a list and sorting it.
.sort() returns None, so the variable assignment ends up giving None. You're looking for this:
s = sorted(s)
sorted() returns the sorted list. .sort() sorts it in place.
This question already has answers here:
Convert list of ints to one number?
(19 answers)
Closed 4 years ago.
In Python, I want to convert list into int.
so if i have input like this:
a=[1,2,3,4]
i want this output:
1234
so How it is possible?
You can use the join function in conjunction with a generator, like this:
a=[1,2,3,4]
int(''.join(str(i) for i in a))
Output:
1234
With recursion:
a=[1,2,3,4]
def f(l):
if not l: return 0
return l[-1] + f(l[:-1]) * 10
print(f(a))
This outputs:
1234
You can use generator comprehension in the following way:
result = int(''.join((str(i) for i in a)))
This turns every item of the list to a string, joins the list together and then turns the whole thing back to an integer
This question already has answers here:
Removing from a list while iterating over it [duplicate]
(5 answers)
How to remove items from a list while iterating?
(25 answers)
Closed 6 years ago.
I am trying to clear a python list by removing every element in loop by code
x=list(range(10000))
for i in x:
x.remove(i)
I thought that after this code x must be [], but instead only every second element of list is removed. len(x)=5000 instead of 0.
Why is it so? What am I doing wrong.
Thanks
The a.remove(i) messed up the indexing is my guess.
instead use
a.clear()
Its a good way to clear a list.
If you want to clear a python list like you're doing, the right way is just using x.clear, docs about that method here, now, if you want to remove elements using some fancy conditions, just use filter, example clearing the whole x list:
x = list(range(10000))
x = filter(lambda x: False, x)
print x
If you want to implement a list object that erases itself while iterating over it that would be fairly easy:
class ErasingList(list):
"a list implemented as an iterator, iterating over it will .pop() items off"
def __iter__(self):
return self
def __next__(self):
try:
return self.pop(0)
#or self.pop() to take from the end of the list which is less intuitive but more efficient
except IndexError:
raise StopIteration
next = __next__ #vs2 compatibility.
x = ErasingList(range(100))
for i in x:
print(i)
print(x)
original_list = list(range(1000))
remove_list_elements = []
for i in range(0, len(original_list), 2):
remove_list_elements.append(original_list[i])
[original_list.remove(i) for i in remove_list_elements]
print(len(original_list))
This question already has answers here:
Python one-line "for" expression [duplicate]
(5 answers)
Closed 6 years ago.
I have different variables of an unknown type (in ABAQUS, it says "Sequence") and want to combine them through a loop:
a = [[unknown type], [unknown type], ...]
x = []
for i in a:
x.append(i)
The problem now is that when I initialize x with = [] I get the error message
TypeError: Can only concatenate list (not "Sequence") to list.
Is there another (simple/efficient) way, e.g. to automatically create x in the first loop?
Use a list comprehension:
x = [v for v in a]