An warware: ƙididdige bambancin duk haɗin haɗin tsararraki 2

A cikin duniyar yau, sarrafa bayanai da bincike suna da mahimmanci don magance matsaloli daban-daban da yanke shawara. Python, kasancewar ɗaya daga cikin yaren shirye-shirye da aka fi amfani da shi, yana ba da ɗakunan karatu da ayyuka daban-daban don sauƙaƙe waɗannan ayyuka. Ɗayan irin wannan matsalar ita ce ƙididdige bambanci tsakanin duk abubuwan da za su iya haɗuwa da tsararraki biyu. Wannan labarin zai ba da zurfin fahimtar yadda za a tunkari wannan matsala, mafita ta mataki-mataki, da kuma ba da haske ga ɗakunan karatu da ayyukan da ke ciki. Za mu kuma zurfafa cikin batutuwa masu alaƙa don fahimtar tsarin warware matsalar da kyau.

Domin lissafta bambance-bambancen ga duk haɗuwa na tsararraki biyu, za mu yi amfani da itertools library, musamman samfur aiki, wanda ke taimakawa wajen samar da samfuran Cartesian na abubuwan shigarwa. Bugu da ƙari, za mu yi amfani da numpy, sanannen ɗakin karatu don lissafin kimiyya, don sauƙaƙe ayyukan tsararru.

Maganin Matsala

Za mu fara da shigo da dakunan karatu masu mahimmanci, ƙaddamar da tsararraki biyu, sannan mu yi amfani da aikin itertools.product() don tantance duk yuwuwar haɗuwa tsakanin abubuwan waɗannan tsararrun. Na gaba, za mu ƙididdige bambanci tsakanin waɗannan haɗuwa kuma mu adana sakamakon a cikin jeri.

import itertools
import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([3, 4, 5])

combinations = list(itertools.product(array1, array2))
differences = [abs(combination[0] - combination[1]) for combination in combinations]

Bayanin mataki-mataki na Code

Bari mu bincika kowane bangare na lambar dalla-dalla don fahimtar yadda yake lissafta bambance-bambancen ga duk haɗuwa:

1. Da farko, muna shigo da dakunan karatu da ake buƙata - itertools da numpy:

import itertools
import numpy as np

2. Mun ƙirƙiri jerin ƙididdiga guda biyu tare da abubuwa masu zuwa:

array1 = np.array([1, 2, 3])
array2 = np.array([3, 4, 5])

3. Muna amfani da aikin itertools.product() don samun duk yuwuwar haɗuwa tsakanin abubuwa na duka tsararru:

combinations = list(itertools.product(array1, array2))

Fitowar za ta kasance jerin tuples masu ɗauke da haɗuwa kamar haka:

(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), ( 3, 5)]

4. A ƙarshe, muna ƙididdige lissafin haɗuwa kuma muna ƙididdige cikakken bambanci tsakanin kowane nau'i na abubuwa, adana sakamakon a cikin jerin da ake kira "bambance-bambance":

differences = [abs(combination[0] - combination[1]) for combination in combinations]

Sakamakon bambance-bambancen lissafin zai kasance:

[2, 3, 4, 1, 2, 3, 0, 1, 2]

Itertools Library

  • Laburaren itertools ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙaƙƙarfan ƙa'ida ne a cikin Python Standard Library wanda ke ba da tarin kayan aiki masu sauri, ingantaccen ƙwaƙwalwar ajiya don yin aiki tare da masu tada hankali.
  • Yana ba da ayyuka daban-daban, kamar samfuri, haɓakawa, haɗuwa, waɗanda zasu iya haifar da nau'ikan shirye-shirye iri-iri.
  • Waɗannan ayyuka na iya taimakawa wajen magance hadaddun matsaloli da inganci da haɓaka aikin lambar ku.

Numpy Library

  • Numpy sanannen ɗakin karatu ne na buɗe tushen don lissafin kimiyya a Python.
  • Yana ba da kayan aiki daban-daban don aiki tare da tsararraki, algebra na layi, canjin Fourier, da ƙari.
  • Yana ba da damar ƙididdige ƙididdiga cikin sauri kuma yana sauƙaƙe ayyukan tsararru, yana mai da shi zaɓi don sarrafa bayanai da ayyukan bincike a Python.

A yanzu, ya kamata ku sami fahintar fahimtar yadda ake ƙididdige bambance-bambancen duk yuwuwar haɗuwar tsararraki biyu ta amfani da Python, musamman itertools da ɗakunan karatu na lambobi. Tsarin tsari na tarwatsa matsaloli masu rikitarwa zuwa matakai masu sauƙi ta amfani da ɗakunan karatu na musamman da ayyuka ba wai kawai yana taimakawa wajen samun zurfin fahimtar matsalar ba har ma yana ƙara haɓaka ƙima.

Shafi posts:

Leave a Comment