{"id":2574,"date":"2023-07-21T16:28:02","date_gmt":"2023-07-21T16:28:02","guid":{"rendered":"https:\/\/statorials.org\/it\/spostamento-insensato\/"},"modified":"2023-07-21T16:28:02","modified_gmt":"2023-07-21T16:28:02","slug":"spostamento-insensato","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/spostamento-insensato\/","title":{"rendered":"Come spostare gli elementi in un array numpy (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare uno dei seguenti metodi per compensare gli elementi di un array NumPy:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: sposta gli elementi (mantieni tutti gli elementi originali)<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#shift each element two positions to the right<\/span>\ndata_new = np. <span style=\"color: #3366ff;\">roll<\/span> (data, 2)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: Sposta elementi (consenti la sostituzione degli elementi)<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#define shifting function<\/span>\n<span style=\"color: #008000;\">def<\/span> <span style=\"color: #3366ff;\">shift_elements<\/span> (arr, num, fill_value):\n    result = np. <span style=\"color: #3366ff;\">empty_like<\/span> (arr)\n    <span style=\"color: #008000;\">if<\/span> num &gt; 0:\n        result[:num] = fill_value\n        result[num:] = arr[:-num]\n    <span style=\"color: #008000;\">elif<\/span> num &lt; 0:\n        result[num:] = fill_value\n        result[:num] = arr[-num:]\n    <span style=\"color: #008000;\">else<\/span> :\n        result[:] = arr\n    <span style=\"color: #008000;\">return<\/span> result\n\n<span style=\"color: #008080;\">#shift each element two positions to the right (replace shifted elements with zero)\n<\/span>data_new = shift_elements(data, 2, 0)\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare ciascun metodo nella pratica.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 1: sposta gli elementi (mantieni tutti gli elementi originali)<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.roll.html\" target=\"_blank\" rel=\"noopener\">np.roll()<\/a> per spostare ciascun elemento di un array NumPy di due posizioni a destra:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#create NumPy array\n<\/span>data = np. <span style=\"color: #3366ff;\">array<\/span> ([1, 2, 3, 4, 5, 6])\n\n<span style=\"color: #008080;\">#shift each element two positions to the right\n<\/span>data_new = np. <span style=\"color: #3366ff;\">roll<\/span> (data, 2)\n\n<span style=\"color: #008080;\">#view new NumPy array\n<\/span>data_new\n\narray([5, 6, 1, 2, 3, 4])\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Nota che ogni elemento \u00e8 stato spostato di due posizioni a destra e gli elementi alla fine dell&#8217;array sono stati semplicemente spostati in avanti.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Potremmo anche usare un numero negativo nella funzione <strong>np.roll()<\/strong> per spostare gli elementi a sinistra:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#create NumPy array\n<\/span>data = np. <span style=\"color: #3366ff;\">array<\/span> ([1, 2, 3, 4, 5, 6])\n\n<span style=\"color: #008080;\">#shift each element three positions to the left\n<\/span>data_new = np. <span style=\"color: #3366ff;\">roll<\/span> (data, -3)\n\n<span style=\"color: #008080;\">#view new NumPy array\n<\/span>data_new\n\narray([4, 5, 6, 1, 2, 3])<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 2: Sposta elementi (consenti la sostituzione degli elementi)<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo anche definire una funzione personalizzata per spostare gli elementi di un array NumPy e consentire la sostituzione degli elementi spostati con un determinato valore.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, possiamo definire la seguente funzione per spostare gli elementi e sostituire tutti gli elementi spostati con il valore 0:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#create NumPy array\n<\/span>data = np. <span style=\"color: #3366ff;\">array<\/span> ([1, 2, 3, 4, 5, 6])\n\n<span style=\"color: #008080;\">#define custom function to shift elements\n<\/span><span style=\"color: #008000;\">def<\/span> <span style=\"color: #3366ff;\">shift_elements<\/span> (arr, num, fill_value):\n    result = np. <span style=\"color: #3366ff;\">empty_like<\/span> (arr)\n    <span style=\"color: #008000;\">if<\/span> num &gt; 0:\n        result[:num] = fill_value\n        result[num:] = arr[:-num]\n    <span style=\"color: #008000;\">elif<\/span> num &lt; 0:\n        result[num:] = fill_value\n        result[:num] = arr[-num:]\n    <span style=\"color: #008000;\">else<\/span> :\n        result[:] = arr\n    <span style=\"color: #008000;\">return<\/span> result\n\n<span style=\"color: #008080;\">#shift each element two positions to the right and replace shifted values with zero\n<\/span>data_new = shift_elements(data, 2, 0)\n\n<span style=\"color: #008080;\">#view new NumPy array\n<\/span>data_new\n\narray([0, 0, 1, 2, 3, 4])<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Possiamo anche usare un numero negativo nella funzione per spostare gli elementi a sinistra:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#create NumPy array\n<\/span>data = np. <span style=\"color: #3366ff;\">array<\/span> ([1, 2, 3, 4, 5, 6])\n\n<span style=\"color: #008080;\">#define custom function to shift elements\n<\/span><span style=\"color: #008000;\">def<\/span> <span style=\"color: #3366ff;\">shift_elements<\/span> (arr, num, fill_value):\n    result = np. <span style=\"color: #3366ff;\">empty_like<\/span> (arr)\n    <span style=\"color: #008000;\">if<\/span> num &gt; 0:\n        result[:num] = fill_value\n        result[num:] = arr[:-num]\n    <span style=\"color: #008000;\">elif<\/span> num &lt; 0:\n        result[num:] = fill_value\n        result[:num] = arr[-num:]\n    <span style=\"color: #008000;\">else<\/span> :\n        result[:] = arr\n    <span style=\"color: #008000;\">return<\/span> result\n\n<span style=\"color: #008080;\">#shift each element three positions to the left and replace shifted values with 50\n<\/span>data_new = shift_elements(data, -3, 50)\n\n<span style=\"color: #008080;\">#view new NumPy array\n<\/span>data_new\n\narray([ 4, 5, 6, 50, 50, 50])<\/strong><\/span><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre operazioni comuni in NumPy:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/conto-digitale\/\" target=\"_blank\" rel=\"noopener\">Come contare le occorrenze degli elementi in NumPy<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/numpy-ordina-array-per-colonna\/\" target=\"_blank\" rel=\"noopener\">Come ordinare un array NumPy per colonna<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/modalita-digitale\/\" target=\"_blank\" rel=\"noopener\">Come calcolare la modalit\u00e0 dell&#8217;array NumPy<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare uno dei seguenti metodi per compensare gli elementi di un array NumPy: Metodo 1: sposta gli elementi (mantieni tutti gli elementi originali) #shift each element two positions to the right data_new = np. roll (data, 2) Metodo 2: Sposta elementi (consenti la sostituzione degli elementi) #define shifting function def shift_elements (arr, num, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come spostare gli elementi in un array NumPy (con esempi) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come correggere il seguente errore in Python: sono accettati solo 2 argomenti non basati su parole chiave.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/spostamento-insensato\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come spostare gli elementi in un array NumPy (con esempi) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come correggere il seguente errore in Python: sono accettati solo 2 argomenti non basati su parole chiave.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/spostamento-insensato\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-21T16:28:02+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/spostamento-insensato\/\",\"url\":\"https:\/\/statorials.org\/it\/spostamento-insensato\/\",\"name\":\"Come spostare gli elementi in un array NumPy (con esempi) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-21T16:28:02+00:00\",\"dateModified\":\"2023-07-21T16:28:02+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come correggere il seguente errore in Python: sono accettati solo 2 argomenti non basati su parole chiave.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/spostamento-insensato\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/spostamento-insensato\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/spostamento-insensato\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come spostare gli elementi in un array numpy (con esempi)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come spostare gli elementi in un array NumPy (con esempi) - Statorials","description":"Questo tutorial spiega come correggere il seguente errore in Python: sono accettati solo 2 argomenti non basati su parole chiave.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/spostamento-insensato\/","og_locale":"it_IT","og_type":"article","og_title":"Come spostare gli elementi in un array NumPy (con esempi) - Statorials","og_description":"Questo tutorial spiega come correggere il seguente errore in Python: sono accettati solo 2 argomenti non basati su parole chiave.","og_url":"https:\/\/statorials.org\/it\/spostamento-insensato\/","og_site_name":"Statorials","article_published_time":"2023-07-21T16:28:02+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/spostamento-insensato\/","url":"https:\/\/statorials.org\/it\/spostamento-insensato\/","name":"Come spostare gli elementi in un array NumPy (con esempi) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-21T16:28:02+00:00","dateModified":"2023-07-21T16:28:02+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come correggere il seguente errore in Python: sono accettati solo 2 argomenti non basati su parole chiave.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/spostamento-insensato\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/spostamento-insensato\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/spostamento-insensato\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come spostare gli elementi in un array numpy (con esempi)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2574"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=2574"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2574\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}