Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a loop like that;
for i in range(0,500):
But the rest of them takes more time. I want to split my loop for instance 5 steps. In the first step i want to run the first 100, at last, 401 to 500. But i don't want to write this loop five times.
Is there any short-way this kind of progressive run?
Just create a loop inside a loop:
for s in range(0, 500, 100):
for i in range(s, s+100)):
...
Since in python indices start and 0 and the range is not inclusive of the last number this does, 0-99, 100-199, ..., 400-499.
If time is what you are trying to trim, use xrange(), it is MUCH faster, especially when dealing with large numbers:
for i in xrange(500):
Edit: This is for Python 2.x, not 3.x!
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
Iam new to programming and started learning python.
Iam having a list if numbers
a = [1,2,3,4,5]
Logic should be 1-2-3-4-5 to get the result as -13.
Someone help me with the python code for this problem.
The simplest solution would be to use a for loop and a temporary variable, subtracting the number each time.
a = [1,2,3,4,5] #initiate array
temp = a[0] #set the value to the first number
for i in a[1:]:
temp -= i #update value
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a list to iterate through, the first iteration took 3 minutes 40 seconds, result was a bunch of generated images being saves on hard disk. Would it make sense to split the list into 2 or 3 and apply multithreading in this case?
You can't write to a hard disk in parallel, so using Threading/Multiprocessing wouldn't show any time improvements and would most likely add overhead.
If it's python that's slowing you down and not your disk writing speed, then it might be worth looking into the Map function, if you're using Python3.
https://docs.python.org/3/library/functions.html#map
Otherwise you'd need to look at using a faster language like C
https://docs.python.org/2/c-api/index.html
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i want to create bash script.
that skip 1000 on each loop untill 2M,
i'm stuck here:
for i in {1..2000000} ; do
done;
for exmple:
the first loop:
offset=0
second loop:
offset=100
3rd loop
offset=2000
until 2M
i try few ways but with no success.
python will be welcome also
how can i do that?
Use while loop :
i=0
while [ $i -lt 2000000 ]
do
echo offset=$i
i=$(($i+1000))
done
What you want is the C-style for loop:
for ((i=0; i <= 2000000; i+=1000)); do
bash does support a brace expansion operator that lets you generate sequences with strides greater than one (support appears to have been added in 4.0, although there is no mention in the release notes):
for i in {0..200000..1000}
However, the C-style loop is preferable because it generates the values of i lazily, rather than creating the entire sequence in memory before starting the iteration. Unless you are generating absolutely enormous sequences, this will not usually be an issue, but you might notice a short delay while the sequence is generated.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have been given a set of 20,000 entries in Excel. Each entry is a string, and they are all names of events such as: Daytona 500, NASCAR, 3x1 Brand Rep, etc.
Many of the event names are repeated, and I would like to make a list and sort them and find the most common items in the list, and how many times each one is entered. I am half way through my first semester of Python and have just learned about lists, and would like to use Python 2.7 to do this task, but I am also open to using Excel or R if it makes more sense to use one of these.
I'm not sure where to start or how to input such a large list into a program.
In Excel I would use a PivotTable, about 15 seconds to set up:
your_list = ['Daytona 500', 'NASCAR'] # more values of course
Now use a dictionary comprehension to count items for each unique key.
your_dict = {i:your_list.count(i) for i in set(your_list)}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'd like to create a function that will print the max value within a list of numbers.
I dont know python but the pseudocode would look something like this
max = –2147483648
for each integer n in collection
if n > max
max = n
It loops through every number in a collection. If the number (n) is bigger than the previous maximum, the maximum is set to be equal to n.