For loop into numpyAccessing the index in 'for' loops?Iterating over dictionaries using 'for' loopsDump a NumPy array into a csv fileNumpy - Getting index positions by testing adjacent indexesRemoving nan values from an array“Large data” work flows using pandasPython pptx custom color for each categoryPython Rubiks Cube How to tell if 2 states are equalPython - creating a building map with a dictionaryPYTHON INSERT MYSql query

Why are electrically insulating heatsinks so rare? Is it just cost?

Is this a crack on the carbon frame?

Is it legal for company to use my work email to pretend I still work there?

Why do falling prices hurt debtors?

What's the point of deactivating Num Lock on login screens?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Why, historically, did Gödel think CH was false?

Mathematical cryptic clues

Font hinting is lost in Chrome-like browsers (for some languages )

Is a tag line useful on a cover?

How can I make my BBEG immortal short of making them a Lich or Vampire?

Risk of getting Chronic Wasting Disease (CWD) in the United States?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Can I make popcorn with any corn?

Why can't I see bouncing of a switch on an oscilloscope?

"You are your self first supporter", a more proper way to say it

strToHex ( string to it's hex representation as string)

In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?

Why not use SQL instead of GraphQL?

How can bays and straits be determined in a procedurally generated map?

What is the offset in a seaplane's hull?

Problem of parity - Can we draw a closed path made up of 20 line segments...

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

can i play a electric guitar through a bass amp?



For loop into numpy


Accessing the index in 'for' loops?Iterating over dictionaries using 'for' loopsDump a NumPy array into a csv fileNumpy - Getting index positions by testing adjacent indexesRemoving nan values from an array“Large data” work flows using pandasPython pptx custom color for each categoryPython Rubiks Cube How to tell if 2 states are equalPython - creating a building map with a dictionaryPYTHON INSERT MYSql query






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I'm just beginning to learn how to use numpy. I have this block of code and am looking to use numpy arrays to represent it. Any tips on how to go about doing so?



import numpy as np
for i in range(np):
direction = random.randint(1, 4)
if direction == NORTH:
ypositions[i] += 1
elif direction == SOUTH:
ypositions[i] -= 1
elif direction == EAST:
xpositions[i] += 1
elif direction == WEST:
xpositions[i] -= 1









share|improve this question
























  • Represent what exactly?

    – gmds
    Mar 21 at 23:24











  • for i in range(np): is well off the mark. That's the module name. What are you expecting to iterate through here?

    – roganjosh
    Mar 21 at 23:24

















-1















I'm just beginning to learn how to use numpy. I have this block of code and am looking to use numpy arrays to represent it. Any tips on how to go about doing so?



import numpy as np
for i in range(np):
direction = random.randint(1, 4)
if direction == NORTH:
ypositions[i] += 1
elif direction == SOUTH:
ypositions[i] -= 1
elif direction == EAST:
xpositions[i] += 1
elif direction == WEST:
xpositions[i] -= 1









share|improve this question
























  • Represent what exactly?

    – gmds
    Mar 21 at 23:24











  • for i in range(np): is well off the mark. That's the module name. What are you expecting to iterate through here?

    – roganjosh
    Mar 21 at 23:24













-1












-1








-1








I'm just beginning to learn how to use numpy. I have this block of code and am looking to use numpy arrays to represent it. Any tips on how to go about doing so?



import numpy as np
for i in range(np):
direction = random.randint(1, 4)
if direction == NORTH:
ypositions[i] += 1
elif direction == SOUTH:
ypositions[i] -= 1
elif direction == EAST:
xpositions[i] += 1
elif direction == WEST:
xpositions[i] -= 1









share|improve this question
















I'm just beginning to learn how to use numpy. I have this block of code and am looking to use numpy arrays to represent it. Any tips on how to go about doing so?



import numpy as np
for i in range(np):
direction = random.randint(1, 4)
if direction == NORTH:
ypositions[i] += 1
elif direction == SOUTH:
ypositions[i] -= 1
elif direction == EAST:
xpositions[i] += 1
elif direction == WEST:
xpositions[i] -= 1






python numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 23:27







Dyland

















asked Mar 21 at 23:23









DylandDyland

134




134












  • Represent what exactly?

    – gmds
    Mar 21 at 23:24











  • for i in range(np): is well off the mark. That's the module name. What are you expecting to iterate through here?

    – roganjosh
    Mar 21 at 23:24

















  • Represent what exactly?

    – gmds
    Mar 21 at 23:24











  • for i in range(np): is well off the mark. That's the module name. What are you expecting to iterate through here?

    – roganjosh
    Mar 21 at 23:24
















Represent what exactly?

– gmds
Mar 21 at 23:24





Represent what exactly?

– gmds
Mar 21 at 23:24













for i in range(np): is well off the mark. That's the module name. What are you expecting to iterate through here?

– roganjosh
Mar 21 at 23:24





for i in range(np): is well off the mark. That's the module name. What are you expecting to iterate through here?

– roganjosh
Mar 21 at 23:24












1 Answer
1






active

oldest

votes


















1














With numpy you need to think in terms of whole arrays. So, applying directional deltas based on some random number would need to operate on all coordinates at once.



for example (assuming xpositions and ypositions are numpy arrays):



directions = np.random.randint(1,4,25)
ypositions += directions == NORTH
ypositions -= directions == SOUTH
xpositions += directions == EAST
xpositions -= directions == WEST





share|improve this answer























  • And of for i in range(np):? It's completely impossible to know what the OP intends to iterate.

    – roganjosh
    Mar 22 at 0:03












  • ... I'm not the OP. But this can't answer the question if that isn't addressed.

    – roganjosh
    Mar 22 at 0:05











  • I had to assume that it was meant to be the range of entries in the xpositions and ypositions arrays. But I agree range(np) makes no sense otherwise

    – Alain T.
    Mar 22 at 0:05











  • Alain T is correct. I meant it to be the range of entries in the xpositions and positions arrays. I've worked with arrays before in java but not in python (with numpy). I appreciate the help and patience you all have showed me about this question!

    – Dyland
    Mar 22 at 0:11











  • Well, if I let myself be stumped by every little mistake, I'd be asking questions not answering them. You're welcome.

    – Alain T.
    Mar 22 at 0:17











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290684%2ffor-loop-into-numpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














With numpy you need to think in terms of whole arrays. So, applying directional deltas based on some random number would need to operate on all coordinates at once.



for example (assuming xpositions and ypositions are numpy arrays):



directions = np.random.randint(1,4,25)
ypositions += directions == NORTH
ypositions -= directions == SOUTH
xpositions += directions == EAST
xpositions -= directions == WEST





share|improve this answer























  • And of for i in range(np):? It's completely impossible to know what the OP intends to iterate.

    – roganjosh
    Mar 22 at 0:03












  • ... I'm not the OP. But this can't answer the question if that isn't addressed.

    – roganjosh
    Mar 22 at 0:05











  • I had to assume that it was meant to be the range of entries in the xpositions and ypositions arrays. But I agree range(np) makes no sense otherwise

    – Alain T.
    Mar 22 at 0:05











  • Alain T is correct. I meant it to be the range of entries in the xpositions and positions arrays. I've worked with arrays before in java but not in python (with numpy). I appreciate the help and patience you all have showed me about this question!

    – Dyland
    Mar 22 at 0:11











  • Well, if I let myself be stumped by every little mistake, I'd be asking questions not answering them. You're welcome.

    – Alain T.
    Mar 22 at 0:17















1














With numpy you need to think in terms of whole arrays. So, applying directional deltas based on some random number would need to operate on all coordinates at once.



for example (assuming xpositions and ypositions are numpy arrays):



directions = np.random.randint(1,4,25)
ypositions += directions == NORTH
ypositions -= directions == SOUTH
xpositions += directions == EAST
xpositions -= directions == WEST





share|improve this answer























  • And of for i in range(np):? It's completely impossible to know what the OP intends to iterate.

    – roganjosh
    Mar 22 at 0:03












  • ... I'm not the OP. But this can't answer the question if that isn't addressed.

    – roganjosh
    Mar 22 at 0:05











  • I had to assume that it was meant to be the range of entries in the xpositions and ypositions arrays. But I agree range(np) makes no sense otherwise

    – Alain T.
    Mar 22 at 0:05











  • Alain T is correct. I meant it to be the range of entries in the xpositions and positions arrays. I've worked with arrays before in java but not in python (with numpy). I appreciate the help and patience you all have showed me about this question!

    – Dyland
    Mar 22 at 0:11











  • Well, if I let myself be stumped by every little mistake, I'd be asking questions not answering them. You're welcome.

    – Alain T.
    Mar 22 at 0:17













1












1








1







With numpy you need to think in terms of whole arrays. So, applying directional deltas based on some random number would need to operate on all coordinates at once.



for example (assuming xpositions and ypositions are numpy arrays):



directions = np.random.randint(1,4,25)
ypositions += directions == NORTH
ypositions -= directions == SOUTH
xpositions += directions == EAST
xpositions -= directions == WEST





share|improve this answer













With numpy you need to think in terms of whole arrays. So, applying directional deltas based on some random number would need to operate on all coordinates at once.



for example (assuming xpositions and ypositions are numpy arrays):



directions = np.random.randint(1,4,25)
ypositions += directions == NORTH
ypositions -= directions == SOUTH
xpositions += directions == EAST
xpositions -= directions == WEST






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 23:56









Alain T.Alain T.

8,54711329




8,54711329












  • And of for i in range(np):? It's completely impossible to know what the OP intends to iterate.

    – roganjosh
    Mar 22 at 0:03












  • ... I'm not the OP. But this can't answer the question if that isn't addressed.

    – roganjosh
    Mar 22 at 0:05











  • I had to assume that it was meant to be the range of entries in the xpositions and ypositions arrays. But I agree range(np) makes no sense otherwise

    – Alain T.
    Mar 22 at 0:05











  • Alain T is correct. I meant it to be the range of entries in the xpositions and positions arrays. I've worked with arrays before in java but not in python (with numpy). I appreciate the help and patience you all have showed me about this question!

    – Dyland
    Mar 22 at 0:11











  • Well, if I let myself be stumped by every little mistake, I'd be asking questions not answering them. You're welcome.

    – Alain T.
    Mar 22 at 0:17

















  • And of for i in range(np):? It's completely impossible to know what the OP intends to iterate.

    – roganjosh
    Mar 22 at 0:03












  • ... I'm not the OP. But this can't answer the question if that isn't addressed.

    – roganjosh
    Mar 22 at 0:05











  • I had to assume that it was meant to be the range of entries in the xpositions and ypositions arrays. But I agree range(np) makes no sense otherwise

    – Alain T.
    Mar 22 at 0:05











  • Alain T is correct. I meant it to be the range of entries in the xpositions and positions arrays. I've worked with arrays before in java but not in python (with numpy). I appreciate the help and patience you all have showed me about this question!

    – Dyland
    Mar 22 at 0:11











  • Well, if I let myself be stumped by every little mistake, I'd be asking questions not answering them. You're welcome.

    – Alain T.
    Mar 22 at 0:17
















And of for i in range(np):? It's completely impossible to know what the OP intends to iterate.

– roganjosh
Mar 22 at 0:03






And of for i in range(np):? It's completely impossible to know what the OP intends to iterate.

– roganjosh
Mar 22 at 0:03














... I'm not the OP. But this can't answer the question if that isn't addressed.

– roganjosh
Mar 22 at 0:05





... I'm not the OP. But this can't answer the question if that isn't addressed.

– roganjosh
Mar 22 at 0:05













I had to assume that it was meant to be the range of entries in the xpositions and ypositions arrays. But I agree range(np) makes no sense otherwise

– Alain T.
Mar 22 at 0:05





I had to assume that it was meant to be the range of entries in the xpositions and ypositions arrays. But I agree range(np) makes no sense otherwise

– Alain T.
Mar 22 at 0:05













Alain T is correct. I meant it to be the range of entries in the xpositions and positions arrays. I've worked with arrays before in java but not in python (with numpy). I appreciate the help and patience you all have showed me about this question!

– Dyland
Mar 22 at 0:11





Alain T is correct. I meant it to be the range of entries in the xpositions and positions arrays. I've worked with arrays before in java but not in python (with numpy). I appreciate the help and patience you all have showed me about this question!

– Dyland
Mar 22 at 0:11













Well, if I let myself be stumped by every little mistake, I'd be asking questions not answering them. You're welcome.

– Alain T.
Mar 22 at 0:17





Well, if I let myself be stumped by every little mistake, I'd be asking questions not answering them. You're welcome.

– Alain T.
Mar 22 at 0:17



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290684%2ffor-loop-into-numpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript