{"id":896,"date":"2023-07-28T09:39:35","date_gmt":"2023-07-28T09:39:35","guid":{"rendered":"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/"},"modified":"2023-07-28T09:39:35","modified_gmt":"2023-07-28T09:39:35","slug":"zip-due-elenchi-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/","title":{"rendered":"Come comprimere due elenchi in python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Spesso potresti essere interessato a comprimere (o &#8220;unire&#8221;) due elenchi in Python. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione zip().<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo tutorial mostra diversi esempi di utilizzo pratico di questa funzione.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: comprimere due elenchi di uguale lunghezza in un unico elenco<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">La seguente sintassi mostra come comprimere due elenchi di uguale lunghezza in uno solo:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define list a and list b<\/span>\na = ['a', 'b', 'c']<\/strong>\n<strong>b = [1, 2, 3]\n\n<span style=\"color: #008080;\">#zip the two lists together into one list<\/span><\/strong>\n<strong>list( <span style=\"color: #3366ff;\">zip<\/span> (a,b))\n\n[('a', 1), ('b', 2), ('c', 3)]\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: comprimere due elenchi di uguale lunghezza in un dizionario<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">La seguente sintassi mostra come comprimere due elenchi di uguale lunghezza in un dizionario:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define list of keys and list of values<\/span>\n<\/strong><strong>keys = ['a', 'b', 'c']\nvalues = [1, 2, 3]\n\n<span style=\"color: #008080;\">#zip the two lists together into one dictionary<\/span><\/strong>\n<strong>dict( <span style=\"color: #3366ff;\">zip<\/span> (keys, values)) \n\n{'a': 1, 'b': 2, 'c': 3}\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 3: comprimere due elenchi di lunghezza diversa<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Se i tuoi due elenchi hanno lunghezze diverse, zip() verr\u00e0 troncato alla lunghezza dell&#8217;elenco pi\u00f9 breve:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define list a and list b<\/span>\na = ['a', 'b', 'c', 'd']<\/strong>\n<strong>b = [1, 2, 3]\n\n<span style=\"color: #008080;\">#zip the two lists together into one list<\/span><\/strong>\n<strong>list( <span style=\"color: #3366ff;\">zip<\/span> (a,b))\n\n[('a', 1), ('b', 2), ('c', 3)]<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Se vuoi evitare che zip() venga troncato alla lunghezza dell&#8217;elenco pi\u00f9 breve, puoi invece utilizzare la funzione <a href=\"https:\/\/docs.python.org\/3\/library\/itertools.html#itertools.zip_longest\" target=\"_blank\" rel=\"noopener\">zip_longest()<\/a> dalla libreria <strong>itertools<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per impostazione predefinita, questa funzione compila &#8220;Nessuno&#8221; per i valori mancanti:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">from<\/span> itertools <span style=\"color: #008000;\">import<\/span> zip_longest<\/span>\n\n#define list a and list b<\/span>\na = ['a', 'b', 'c', 'd']<\/strong>\n<strong>b = [1, 2, 3]\n\n<span style=\"color: #008080;\">#zip the two lists together without truncating to length of shortest list<\/span><\/strong>\n<strong>list( <span style=\"color: #3366ff;\">zip_longest<\/span> (a, b))\n\n[('a', 1), ('b', 2), ('c', 3), ('d', None)]\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tuttavia, puoi utilizzare l&#8217;argomento <strong>fillvalue<\/strong> per specificare un valore di riempimento diverso da utilizzare:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define list a and list b<\/span>\na = ['a', 'b', 'c', 'd']<\/strong>\n<strong>b = [1, 2, 3]\n\n<span style=\"color: #008080;\">#zip the two lists together, using fill value of '0'<\/span><\/strong>\n<strong>list( <span style=\"color: #3366ff;\">zip_longest<\/span> (a, b, fillvalue= <span style=\"color: #339966;\">0<\/span> ))\n\n[('a', 1), ('b', 2), ('c', 3), ('d', 0)]\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><em>Puoi trovare la documentazione completa per la funzione zip_longest() <a href=\"https:\/\/docs.python.org\/3\/library\/itertools.html#itertools.zip_longest\" target=\"_blank\" rel=\"noopener\">qui<\/a> .<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spesso potresti essere interessato a comprimere (o &#8220;unire&#8221;) due elenchi in Python. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione zip(). Questo tutorial mostra diversi esempi di utilizzo pratico di questa funzione. Esempio 1: comprimere due elenchi di uguale lunghezza in un unico elenco La seguente sintassi mostra come comprimere due elenchi di uguale [&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 comprimere due elenchi in Python - Stology<\/title>\n<meta name=\"description\" content=\"Una semplice spiegazione di come comprimere due elenchi insieme in Python, inclusi diversi esempi.\" \/>\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\/zip-due-elenchi-python\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come comprimere due elenchi in Python - Stology\" \/>\n<meta property=\"og:description\" content=\"Una semplice spiegazione di come comprimere due elenchi insieme in Python, inclusi diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T09:39:35+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=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/\",\"url\":\"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/\",\"name\":\"Come comprimere due elenchi in Python - Stology\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T09:39:35+00:00\",\"dateModified\":\"2023-07-28T09:39:35+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Una semplice spiegazione di come comprimere due elenchi insieme in Python, inclusi diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come comprimere due elenchi in python\"}]},{\"@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 comprimere due elenchi in Python - Stology","description":"Una semplice spiegazione di come comprimere due elenchi insieme in Python, inclusi diversi esempi.","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\/zip-due-elenchi-python\/","og_locale":"it_IT","og_type":"article","og_title":"Come comprimere due elenchi in Python - Stology","og_description":"Una semplice spiegazione di come comprimere due elenchi insieme in Python, inclusi diversi esempi.","og_url":"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/","og_site_name":"Statorials","article_published_time":"2023-07-28T09:39:35+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/","url":"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/","name":"Come comprimere due elenchi in Python - Stology","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T09:39:35+00:00","dateModified":"2023-07-28T09:39:35+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Una semplice spiegazione di come comprimere due elenchi insieme in Python, inclusi diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/zip-due-elenchi-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/zip-due-elenchi-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come comprimere due elenchi in python"}]},{"@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\/896"}],"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=896"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/896\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}