[Python] Insiemi

Yuri yurj a alfa.it
Mer 26 Maggio 2021 09:22:36 CEST


I set di python servono per operazioni comuni, ma non sono una 
implementazione matematica degli insiemi. Forse SimPy è quello che cerchi?

https://docs.sympy.org/latest/modules/sets.html#compound-sets

Il 25/05/21 21:30, Bruno Firmani ha scritto:
> Grazie Federico,
> ma a me sembra una lacuna. Forse uso abitualmente software CAS.
>
> Ciao
> Bruno
>
>> Il giorno 25 mag 2021, alle ore 19:45, Federico Cerchiari 
>> <federicocerchiari a gmail.com> ha scritto:
>>
>> 
>> Ciao Bruno,
>>
>> questo comportamento dell'interprete deriva dal fattto che in ss1 ed 
>> ss3, nel momentto in cui True viene inserito nel set, è già presente 
>> un oggetto con "valore" 1 nello stesso set.
>> Questo perchè i set (come le chiavi dei dizionari) sono collezioni di 
>> "hashabili", gli hashabili sono oggetti di cui può essere calcolatto 
>> l'hash, e l'hash è un numero intero che identtifica il "valore" 
>> dell'oggetto.
>>
>> In questo caso, sia "True" che "1" hanno hash di valore 1:
>> >>> hash(True)
>> 1
>> >>> hash(1)
>> 1
>> >>> hash(True) == hash(1)
>> True
>> >>> True == 1
>> True
>>
>> Entrambi True e 1 "valgono" 1, al momento dell'inserimento in un 
>> insieme, Python fa questo ragionamento: "esiste già un elemento che 
>> "vale" come questo?", e in ss1 ed ss3, la risposa è sì. Lo stesso 
>> vale in entrambi i versi:
>> >>> { 1 , True }
>> {1}
>> >>> { True, 1 }
>> {True}
>>
>> Il comportamento è un false friend, ma deriva da come è implemenato 
>> il metodo __hash__ alla base dell'interprete. Lo setsso vale per 
>> False e 0:
>> >>> { False, 0 }
>> {False}
>>
>> I due bool, ed 1 e 0, hanno lo stesso "valore" per l'interprete, pur 
>> essendo "cose" diverse:
>> >>> True is 1
>> False
>>
>> Ciao
>> Federico
>>
>> Il giorno mar 25 mag 2021 alle ore 19:27 Bruno Firmani 
>> <firmanibruno a gmail.com <mailto:firmanibruno a gmail.com>> ha scritto:
>>
>>     Buonasera,
>>     sto incontrando questa difficoltà.
>>
>>     Sto imparando.
>>
>>     Alle istruzioni:
>>
>>     ss0 = { 2<=2 , 5 >= 3 }
>>     ss1 = { 1 , 2 , 3 , 4 , '4' , '5' , 'a' , 'b' , 'c' , 3 , '3' , 4
>>     , '4' , False , 2<=2 , 5 >= 3 }
>>     ss2 = { 1 ,11 , 'a' , 'ab' , 'abc' }
>>     ss3 = { 1 , 2 , 3 , 2 , 3 , 3 , 'a' , 'b' , 'c' , 'b' , 'c' , 'c'
>>     , False , 2<=2 , 5 >= 3 }
>>
>>     print('ss0 = ' , ss0)
>>     print('ss1 = ' , ss1)
>>     print('ss2 = ' , ss2)
>>     print('ss3 = ' , ss3)
>>
>>     ottengo come risposta:
>>
>>     ss0 =  {True}
>>     ss1 =  {False, 1, 2, 3, 4, '5', '3', 'b', 'c', '4', 'a'}
>>     ss2 =  {1, 'abc', 11, 'ab', 'a'}
>>     ss3 =  {False, 1, 2, 3, 'b', 'c', 'a'}
>>
>>
>>     In "ss0" compare "True" che invece non viene evidenziato in "ss1"
>>     e in "ss3".
>>     Non so spiegarmi questo fenomeno.
>>
>>     Cordiali saluti
>>     Bruno Firmani
>>
>>
>>
>>
>>
>>            Bruno Firmani
>>            Via Giannantonio Manci 5 - 39100 BOLZANO
>>     e.mail firmanibruno a gmail.com <mailto:firmanibruno a gmail.com>
>>     mlbf1984 a gmail.com <mailto:mlbf1984 a gmail.com>
>>     tel.   +39 338 5721755 (cellulare)
>>     fax    +39 02 70039544
>>     _______________________________________________
>>     Python mailing list
>>     Python a lists.python.it <mailto:Python a lists.python.it>
>>     https://lists.python.it/mailman/listinfo/python
>>     <https://lists.python.it/mailman/listinfo/python>
>>
>> _______________________________________________
>> Python mailing list
>> Python a lists.python.it
>> https://lists.python.it/mailman/listinfo/python
>
> _______________________________________________
> Python mailing list
> Python a lists.python.it
> https://lists.python.it/mailman/listinfo/python


Maggiori informazioni sulla lista Python