Python appending empty list [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am very new to Python and trying to learn by trial-and-error, so my question may sound naive for the community.
Let's say I have two empty lists with only the first element defined:
a = [[]]*20
a[0] = 0
b = [[]]*20
b[0] = 1
I want to use a for loop for creating the other elements of the lists:
x = 20
for i in range(1,x):
a[i] = b[i-1],
b[i] = a[i-1]+b[i-1]
What I obtain is the following error:TypeError: can only concatenate tuple (not "int") to tuple.
Basically I am trying to reproduce the fibonacci series (a famous starting point in Python tutorial), but I would like to experiment other ways of obtaining the same output.
Thank you!

The problem is on this line:
a[i] = b[i-1],
Notice the comma at the end? That makes python think you're dealing in tuples. Remove it and the error will be gone.

Related

Adding sentence breaks to the beginning and end of each element in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a list of strings:
mini_corpus = ['I am Sam','Sam I am','I am Sam','I do not like green eggs and Sam']
I need to add a sentence boundary at the beginning and end of each element (i.e. 'BOS I am Sam EOS', 'BOS Sam I am EOS', etc.)
I've tried using map : mini_corpv2 = list(map(lambda x: 'BOS{}EOS'.format(x), mini_corpus)) but it throws 'list' object is not callable
Can anyone tell me what I'm doing wrong or suggest another method to implement this?
I suppose the problem is somewhere else. Your code runs without problems, resulting in
['BOSI am SamEOS',
'BOSSam I amEOS',
'BOSI am SamEOS',
'BOSI do not like green eggs and SamEOS']
(so you will probably want to add spaces after BOS and before EOS).
An alternative solution using list comprehension:
mini_corpv2 = [f'BOS {x} EOS' for x in mini_corpus]

.remove function not working with an 'if' check in lists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm fairly new to programming and python itself- I'm trying to use the .remove function to delete an item from a list if it exists in that list (as to not get the nameError).
q = ["cat","dog","fish","hamster","horse"]
#Request element name to delete from queue
removeElement = input("Please type in the element name to remove from the queue: ")
#Remove the given element from the list
q.remove(removeElement) if 'removeElement' in q else None
print(q)
Unfortunately, if I try and use the 'if' checker it the item isn't removed from my list- why is this and how can I fix this issue?
You have to use the variable's name not a string:
q.remove(removeElement) if removeElement in q else None

Why am I getting two diferent outputs with my python code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
def kaka(name):
r=''
for ch in name:
r=r+ch*3
return r
Output:
>>> kaka('Mississippi')
>>> 'MMMiiissssssiiissssssiiippppppiii'
But for this code:
def kaka(name):
for ch in name:
r=''
r=r+ch*3
return r
I am getting output as: iii
That's because in your second code you're re-assigning r back to the empty string ''. Thus you only get the final character multiplied 3 times (which for Mississippi is i).
You are getting 2 different outputs because in the first code you are initialising the value of r i.e r = '' outside the for loop and in the second program you are initialising value of r inside the for loop.

TypeError: 'function' object is not subscriptable in sorting algorithm [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am creating a program in Python which is designed to organise a list, I have been using an insertion sort method, however when I execute the program, after I have input the list top organise, I am returned with the error "TypeError: 'function' object is not subscriptable"
My code is below:
def listsort(x):
for i in range(1,len(list)):
value = list[index]
i = index - 1
while i>=0:
if value < list[i]:
list[i+1] = list[i]
list[i] = value
i = i - 1
else:
break
Please help me understand where I have gone wrong here people, it's frying my brain....
list() is a python function.
You can't index functions.
Considering using a different variable name. (like x since that seems to be the list you are trying to sort)
It's worth noting that sort() and sorted() are also functions that you may use, but learning sorting methods is good practice.

Python: How to store string inputs on a array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I would like to store strings on a array. This strings are produced in this loop:
while (count < ts ):
dt=tb
t1=count+180
t2=t1+360
dt1=dt+t1
dt2=dt+t2
slice=stream.slice(dt1, dt2)
B=str(dt1)
E=str(dt2)
slice.write(station+'_'+comp[i]+'_'+B+'_'+E, format="MSEED")
count = count + 360
bb=[]
name=station+B+'_'+E
a=[str(name)]
bb.append(a)
But it doesn't work. The variable name is from type:
name=2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z
And I would like to have an array like that:
bb=[2011-05-22T23:42:00.000000Z_2011-05-22T23:48:00.000000Z, 2011-05-22T23:48:00.000000Z_2011-05-22T23:54:00.000000Z, 2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
But what bb returns me is an array with the last element called:
bb=[2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
If I do it manually:
bb.append('2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z')
It works perfectly because I put the ''. But I need to it in a automatic way.
Any suggestion?
Thanks in advance!
Declare bb outside the loop and a will be a list. You will get a list of lists(not in the way you asked for)

Categories