Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have some code that gets the lecture times from online courses I take. The resulting answer is in the form of [['02:29']]. I think I'd use the following code to get the number out:
time1=timeList[0]
time2=time1[0]
time3=(time2.contents[0])
(parts) = time3.split(":")
Is there a better way to do this in fewer steps? And what resources are there to help me in the future? Or is this one of those things you just learn by coding and then asking for help?
Is it a list in a list with just one item?
Then you could achieve the same with:
test = [['02:29']]
parts = test[0][0].split(':')
print parts # result: ['02', '29']
But I'am not sure if I got you right
Itertool is always available:
Itertools
And you can chain your list considering it's always in the same format:
import itertools
time1 = list(itertools.chain(*timeList))[0].split(":")[0]
This solution is very fast and it works very smooth. A bit complicated to understand but it makes the work very efficiently.
Note: it also works with more levels of subarrays, no matter if you have 1, 2 or 3 levels
Update
Due to you indicated you want to convert it to raw seconds the answer is:
time1 = list(itertools.chain(*timeList))[0].split(":")
seconds_total = (time1[0] * 60) + time1[1]
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I used dictionaries the first time and I can't figure out how to get all first elements of a dictionary. The picture shows an example of my problem. I want to get the brand names, not "brand0, brand1" etc.
thisdict = {
"brand0": ("Ford", "green_car"),
"brand1": ("Audi", "yellow_car"),
"brand2": ("Porsche", "red_car")
}
You can use several aproaches to this problem but the easiest is probably this
firstItems = [value[0] for value in thisdict.values()]
this works the same as
firstItems = []
for value in thisdict.values():
firstItems.append(value[0])
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How do I search and replace using built-in Python methods?
For instance, with a string of appleorangegrapes (yes all of them joined),
Replace "apple" with "mango".
The .replace method only works if the words are evenly spaced out but not if they are combined as one. Is there a way around this?
I searched the web but again the .replace method only gives me an example if they are spaced out.
Thank you for looking at the problem!
This works exactly as expected and advertised. Have a look:
s = 'appleorangegrapes'
print(s) # -> appleorangegrapes
s = s.replace('apple', 'mango')
print(s) # -> mangoorangegrapes
The only thing that you have to be careful of is that replace is not an in-place operator and as such it does not update s automatically; it only creates a new string that you have to assign to something.
s = 'appleorangegrapes'
s.replace('apple', 'mango') # the change is made but not saved
print(s) # -> appleorangegrapes
replace can work for any string, why you think that it doesn't, here is the test:
>>> s='appleorangegrapes'
>>> s.replace('apple','mango')
'mangoorangegrapes'
>>>
Don't you see that you received your expected result?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
i know this might be a bit beginner level type of code but i am getting a bit confused by this code below. i don't know what is happening. may someone be kind enough to explain it to me? And what is the output of the code?
lest = [3, 1, -2]
print(lest[lest[-1]])
3
1
-1
-2
What really is happening here, especially with lst[lst[-1]]? i do not understand the concept being shown here. are we looking for a value in a multi-dimensional array or what?
lest = [3, 1, -2]
print(lest[lest[-1]])
Okay easier if you break it up.
First:
lest[-1] will give us the last value of list lest which is -2
Second:
lest[lest[-1]]
Since we know lest[-1] = -2 we know that this is equivalent to:
lest[-2]
Now your code is going to return the second to last digit of lest which is 1
Output
(xenial)vash#localhost:~/python$ python3.7 helpin.py
1
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've written a program calculating pi based on basel problem but this program includes a very big while repeat.
See:
import math
import decimal
sqrt=1.
sumn=0.
outerrepeat=1000000
while outerrepat>0:
repeat=10000
while repeat>0:
sumn+=1/(sqrt**2)
sqrt+=1
repeat-=1
outerrepeat-=1
print math.sqrt(sumn*6)
When I run this outputs an empty line like this without ">>>":
========================== RESTART: C:/Python/pi.py ==========================
What's the reason?
Edit: Sorry for different variables, I wrote this program in my own language and then translated the variables for you. I forgot some of them.
Second edit: I tried Luc's suggestion, also changed the place of the print math.sqrt(sumn*6) to the outer while so I could see if it was proccesing.
It did but after the 7th number of pi it stopped working and stuck at 3.14159264498
The short answer to your question, is that your while loop are too big. So when you execute your program, the time to go through the while loop is too long.
There are some typo in the code that you copy/pasted, but I will assume that there is none. You are trying to make 10 000 000 000 iterations. Do you really need that many?
I would start by simply going for 100 iterations, maximum. And then augment it if you have a need for more precision.
You algorithm would be easier to understand/debug, if you simply use a for-loop.
You can try this one for example:
import math
max_iteration = 100
sumn=0.
for n in range(1,max_iteration+1):
sumn+=1./(n**2)
print math.sqrt(sumn*6)
With max_iteration = 100 you should find 3.13207653181, and the precision is getting better if you increase your max_iteration. With 10 000 iterations your are already at 3.14149716395.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
inputs = []
iterations = int(input())
for x in inputs:
currentInput = input()
inputs.append(currentInput)
print(x)
That code isn't working. It is supposed to make more "currentInput" variables based on "iterations". Thank you soooo, much, as this has been bugging me.
Your code isn't working because it's not right.
Your for loop is going through each element in the list inputs. But inputs is an empty list; you haven't added anything to it so the for loop won't work. You must have meant
for x in range(iterations):
currentInput=input()
inputs.append(currentInput)
print(currentInput)