Python Forum
Why am I getting list elements < 0 ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why am I getting list elements < 0 ?
#1
Hi all!

Here's some code:

price_chg = np.arange(-10,11,2)

stock_prices = [100]

for i in range(10001):
    if (stock_prices[-1] + np.random.choice(price_chg)) < 0:
        stock_prices.append(0)
        continue
    else:
        stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
Why is the if block failing to keep negative integers out of the list?
Reply
#2
(Aug-23-2021, 02:10 PM)Mark17 Wrote: Why is the if block failing to keep negative integers out of the list?
Because you are generating a new random price in the "else" block which can again be negative.
Apparently you mean to do this:
price_chg = np.arange(-10,11,2)
 
stock_prices = [100]
 
for i in range(10001):
    random_price = stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
    if random_price < 0:
        stock_prices.append(0)
    else:
        stock_prices.append(random_price)
Reply
#3
(Aug-23-2021, 02:44 PM)ibreeden Wrote:
(Aug-23-2021, 02:10 PM)Mark17 Wrote: Why is the if block failing to keep negative integers out of the list?
Because you are generating a new random price in the "else" block which can again be negative.
Apparently you mean to do this:
price_chg = np.arange(-10,11,2)
 
stock_prices = [100]
 
for i in range(10001):
    random_price = stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
    if random_price < 0:
        stock_prices.append(0)
    else:
        stock_prices.append(random_price)

When will the "stupid mistakes" end? Rhetorical question. :)

Thank you!
Reply
#4
max() may be a better choice.
import random
price = 10
stock_prices = [price]
for _ in range(11):
    price = max(0, price + random.randrange(-10, 11, 2))
    stock_prices.append(price)

print(stock_prices)
Reply
#5
One line code
import random
stock_prices = [10] + [max(0, 10+random.randrange(-10, 11, 2)) for _ in range(11)]
Reply
#6
Not the same result. +/-10 is the range of daily fluctuation, not the range of stock_prices.
Reply
#7
(Aug-24-2021, 08:31 AM)deanhystad Wrote: Not the same result. +/-10 is the range of daily fluctuation, not the range of stock_prices.

two line
import random
price = 10
stock_prices = [price] + [max(0, price+random.randrange(-10, 11, 2)) for _ in range(11)]
Reply
#8
Still not right. The price fluctuates from the previous day, not the first day. I don't think this can be done with a comprehension because there is no mechanism for remembering what the price was yesterday.
Reply
#9
(Aug-24-2021, 03:32 PM)deanhystad Wrote: Still not right. The price fluctuates from the previous day, not the first day. I don't think this can be done with a comprehension because there is no mechanism for remembering what the price was yesterday.

ya, you are right, I am learned, thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Looping through nested elements and updating the original list Alex_James 3 350 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 462 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 477 Jul-11-2021, 09:58 AM
Last Post: perfringo
  I cannot delete and the elements from the list quest 4 920 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 968 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Find Common Elements in 2 list quest 4 864 Apr-14-2021, 03:57 PM
Last Post: quest
  Sorting Elements via parameters pointing to those elements. rpalmer 3 877 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Group List Elements according to the Input with the order of binary combination quest_ 19 1,930 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  Adding List Element if Second part of the List Elements are the Same quest_ 3 843 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Remove specific elements from list with a pattern Xalagy 3 980 Oct-11-2020, 07:18 AM
Last Post: Xalagy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020