Python: How to store string inputs on a array [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 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)

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.

Python appending empty 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 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.

Index into a string 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 8 years ago.
Improve this question
Suppose I have a list containing 9 fields, and the 9th field is a string.
Then print(line) will print the entire line, print(line[9]) will return something like:
1/0:.:PASS:90:204,90:201,88:48,39:-204,0,-90:-48,0,-39:14:9,5:5
but print(line[9[0:1]])
will return
File "FileParser.py", line 9, in ?
print(line[9[0:1]])
TypeError: unsubscriptable object
If I assign line[9] to a second var, then I can manipulate it like a string, but this seems like a silly extra step.
Is there a way to index directly into the string while still part of the list?
Thank you
You can index it like this
line[9][0:1]
line[9] will get the actual string and you get the range of characters from that string.
When you do line[9[0:1]] you are trying to get the range of values from the number 9, which is not possible. That is why your code fails.
Since line[9] is a string, then you have to do
line[9][0:1]
Let's say line[9] = "some string". Then you can manipulate it with
line[9][0:1]
which will be equivalent to
"some string"[0:1]

Categories