intersection of 2 objects of different types

2024/10/8 20:34:49

i want to take the intersection of:

(('e', 13.02338360095244), ('a', 11.820318700775383), ('o', 9.20172171683253), ('s', 7.635081506807498), ('n', 7.547469320471335), ('i', 7.219915745772025), ('r', 6.704927040722877), ('l', 5.650833384211491), ('d', 5.098296599303987), ('t', 4.7109103119848585))

and:

[('e', 1636), ('n', 991), ('a', 930), ('t', 678), ('o', 677), ('r', 612), ('d', 581), ('i', 507), ('l', 405), ('s', 399)]

but these 2 have different types, how do i go about taking the intersection and displaying the amount of elements in the intersection?

Answer

You can you the intersection of set. For example:

a = (('e', 13.02338360095244), ('a', 11.820318700775383), ('o', 9.20172171683253), ('s', 7.635081506807498), ('n', 7.547469320471335), ('i', 7.219915745772025), ('r', 6.704927040722877), ('l', 5.650833384211491), ('d', 5.098296599303987), ('t', 4.7109103119848585))
b = [('e', 1636), ('n', 991), ('a', 930), ('t', 678), ('o', 677), ('r', 612), ('d', 581), ('i', 507), ('l', 405), ('s', 399)]set(k for k,_ in a).intersection(k for k,_ in b)

Output will be {'i', 'a', 's', 'n', 'r', 'l', 't', 'o', 'd', 'e'}.

https://en.xdnf.cn/q/118674.html

Related Q&A

Enemy Projectiles Attack Way To Fast Problem

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class# enemys bulletsksud = pygame.image.load("heart.png")class Boo…

How to find the maximum digit of an integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.This p…

How to use python to generate a magazine cover? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why do I get this error? NoneType object has no attribute shape in opencv

Im working on real-time clothing detection. so i borrowed the code from GitHub like this:https://github.com/rajkbharali/Real-time-clothes-detection but (H, W) = frame.shape[:2]:following error in last …

Efficiently concatenate two strings from tuples in a list?

I want to concatenate the two string elements in a list of tuplesI have this:mylist = [(a, b), (c, d), (e, f), (g, h)] myanswer = []for tup1 in mylist:myanswer.append(tup1[0] + tup[1])Its working but i…

How to assert that a function call does not return an error with unittest?

Is there anyway with unittest to just assert that a function call does not result in an error, whether it is a TypeError, IOError, etc.example:assert function(a,b) is not errororif not assertRaises fun…

How to calculate Python float-number-th root of float number

I found the following answer here on Stackoverflow:https://stackoverflow.com/a/356187/1829329But it only works for integers as n in nth root:import gmpy2 as gmpyresult = gmpy.root((1/0.213), 31.5).real…

Need help making a Hilbert Curve using numbers in Python

I want to make a function that will create a Hilbert Curve in python using numbers. The parameters for the function would be a number and that will tell the function how many times it should repeat. To…

How do I separate each list [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why python doesnt see the members of quantumCircuit class qiskit

I`m trying to learn the programming on quantum computers. I have installed qiskit in VS Code (all qiskit extentions available in VS Code market) , python compilator (from Vs Code market "Python&qu…