The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
For loops question.
Discuss For loops question. in the Python Programming forum on Dev Shed. For loops question. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 22nd, 2013, 07:15 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
|
For loops question.
Thank you for the hint!
|

February 22nd, 2013, 07:42 PM
|
 |
Contributing User
|
|
|
|
Combine a clever application of these codes:
Code:
import random
list_of_20_random_numbers = [random.random() for i in range(20)]
for i in range(8):
print('this event: {}'.format(i))
__________________
[code] Code tags[/code] are essential for python code!
|

February 25th, 2013, 01:30 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
|
Ok I can create five hundred random numbers but I cannot generate them a thousand times, I try it but it just loops the same thing thousand times. Would we have to use nested loops?
|

February 25th, 2013, 06:00 AM
|
 |
Contributing User
|
|
|
|
|
Are you trying to get a matrix of random numbers?
(2 dimensional, like a populated spreadsheet page)
|

February 25th, 2013, 01:28 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg Are you trying to get a matrix of random numbers?
(2 dimensional, like a populated spreadsheet page) |
2 dimensional yes but I won't be going to print it out because it will be a giant mess. I'm going to take the hundredth value and five hundredth value in those thousand lists and add it to some list separate for hundredth list and thousandth list.
|

February 25th, 2013, 07:39 PM
|
 |
Contributing User
|
|
|
|
|
[[prng()
for c in range(cols)]
for r in range(rows)]
where prng is the random number generator.
|

February 25th, 2013, 08:29 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg [[prng()
for c in range(cols)]
for r in range(rows)]
where prng is the random number generator. |
Thanks I seem to have got this part! Thank you sir b49P23TIvg!
|

February 25th, 2013, 09:34 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
|
Okay I have gotten the list of values for hundred and five hundred terms. Is there a way to find the times a position is revisited/or similar values(0.001 considered equal).
|

February 25th, 2013, 10:10 PM
|
 |
Contributing User
|
|
|
|
I don't understand "I'm going to take the hundredth value and five hundredth value in those thousand lists and add it to some list separate for hundredth list and thousandth list." Nor do I understand why you'd need to generate all that data to get 2 random numbers.
Sort the data then use your tolerant comparisons across neighbors. You'll have to do this carefully, as Ken Iverson said, Quote: | Originally Posted by http://keiapl.org/anec/ In an early talk Ken was explaining the advantages of tolerant comparison. A member of the audience asked incredulously, “Surely you don’t mean that when A=B and B=C, A may not equal C?” Without skipping a beat, Ken replied, “Any carpenter knows that!” and went on to the next question. |
|

February 25th, 2013, 10:53 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
|
Is there a way to find numbers equal to each other in a list and how many times they occur in a list?
|

February 26th, 2013, 07:19 AM
|
 |
Contributing User
|
|
|
|
|
>>> dir(list)
.
.
.
>>> help(list.extend)
.
.
.
|

February 26th, 2013, 11:03 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
|
I have used list.count(x) function but the code is taking a lot of time to generate the results lol, it still hasn't printed the results.
|

February 26th, 2013, 11:33 AM
|
 |
Contributing User
|
|
|
|
|
If you first sort the data, then look through it with a histogram sort of approach you'll get O(n log(n)) performance rather than O(n*n) which you get by scanning the entire list n times.
if n is a million, n squared is 72000 times the value of n log(n)
|

March 1st, 2013, 12:54 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 9
Time spent in forums: 2 h 24 m 25 sec
Reputation Power: 0
|
|
|
thanks!
|

March 1st, 2013, 01:39 PM
|
 |
Contributing User
|
|
|
|
find_three_double works when presented with a list of words.
Code:
>>> find_three_double('bookkeeper axbcccc'.split())
bookkeeper
axbcccc
2 found
>>> find_three_double('bookkeeper axbccc'.split())
bookkeeper
1 found
>>>
Regarding your other question, write some clever logic to avoid whatever it is you mean to avoid.
Note that
Code:
>>> '%%f' % ()
'%f'
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|