This question already has answers here:
Why does "x = x.append(...)" not work in a for loop?
(8 answers)
Closed 2 years ago.
I am trying to re-write this for loop into a function to make it applicable to different time series. It is pretty straightforward but for some reason there are errors with the function. The goal is to subtract the current value with its preceding one:
append doesn't return the modified list, it modifies it in place. This means that
diff = diff.append(value)
will be assigning None to diff, causing your problem.
You just need
diff.append(value)
as you had in the original loop.
Related
This question already has answers here:
How to get first AND last element of tuple at the same time
(4 answers)
Closed 1 year ago.
Is there a function in Python to do it?
I have tried printing out last item in the tuple, but each time I want to generate more strings, I have to change it manually.
Try tuple_name[-1]
replace tuple_name with your tuple name
finally, it will be like print(tuple_name[-1])
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 2 years ago.
Why is a string with a minus sign not recognised properly when passed to a function in python? The simple code
def f(s):
print(s)
if s is 'a-b':
print('I expect this to be printed.')
else:
print('Instead, this is printed.')
s = 'a-b'
if s is 'a-b':
print('Oustide everything is fine.')
f(s)
gives the output
Oustide everything is fine.
a-b
Instead, this is printed.
Can someone explain please why this happens? Without the minus sign everything is fine.
Python distinguishes between values that are equal and values that are identical. You should use the is operator rarely. In fact, you probably won't encounter too many cases where you use is other than is None.
Here, you want if s == 'a-b':.
This question already has answers here:
What does return mean in Python? [closed]
(2 answers)
Why is "None" printed after my function's output?
(7 answers)
Python: Why "return" won´t print out all list elements in a simple for loop and "print" will do it?
(4 answers)
Closed 5 years ago.
I have been practising python and have a small question. I am working with DNA sequences and so I simply wanted to make a small function that just returned the record.ids.
from Bio import AlignIO
my_alignment = Align.IO.read("multipleseqfile.fa","fasta")
def get_id_names(alignment):
for record in alignment:
return record.id
print get_id_names(my_alignment)
I had done a for loop before that prints the names nicely but I wanted to improve my script and make these exercises into functions. However, when I use this function, it only returns the first record id (and there is a list of 30-40). I switched the return record.id to print record.id, and it does print all the names but then I get a None at the end of the output. Not sure what is going on here?
This question already has answers here:
How do I prepend to a short python list?
(7 answers)
Closed 6 years ago.
Recently I was learning python,and I want to use the function that is opposite of append() function.How can I use the function insert an element in the first position of the list.Thank you!
Prepend doesn't exist, but you can simply use the insert method:
list.insert(0, element)
This question already has answers here:
Python objects confusion: a=b, modify b and a changes! [duplicate]
(3 answers)
Closed 8 years ago.
I'm new to python (using 2.7.6) and I'm sure this has been asked before, but I can't find an answer anywhere. I've looked at the python scoping rules and I don't understand what is happening in the following code (which converts three hex number strings to integers)
ls=['a','b','c']
d=ls
for i in range(0,len(d)):
d[i]=int(d[i],64)
print str(ls)
Why does the value of ls change along with the value of d?
I couldn't repeat this behavior with simple assignments. Thanks!
d is ls.
Not the value assigned to d equals the value assigned to ls. The two are one and the same. This is typical Python behavior, not just for lists.
A simple way to do what you want is
d = ls[:]