set

How are small sets stored in memory?

How are small sets stored in memory? Question: If we look at the resize behavior for sets under 50k elements: >>> import sys >>> s = set() >>> seen = {} >>> for i in range(50_000): … size = sys.getsizeof(s) … if size not in seen: … seen[size] = len(s) … print(f"{size=} {len(s)=}") … s.add(i) …

Total answers: 1

Find the difference between two columns in a dataframe but keeping the row index avaiable

Find the difference between two columns in a dataframe but keeping the row index avaiable Question: I have two dataframes: df1 = pd.DataFrame({"product":[‘apples’, ‘bananas’, ‘oranges’, ‘kiwi’]}) df2 = pd.Dataframe({"product":[‘apples’, ‘aples’, ‘appples’, ‘banans’, ‘oranges’, ‘kiwki’], "key": [1, 2, 3, 4, 5, 6]}) I want to use something like a set(df2).difference(df1) to find the difference between the …

Total answers: 2

Behaviour of set-intersection of objects

Behaviour of set-intersection of objects Question: I stumbled upon this uncertainty in one of my programs: Suppose we have a Class deriving from int with a custom attribute. class A(int): def __new__(cls, value, *args, **kwargs): return super(cls, cls).__new__(cls, value) def __init__(self, _, a): self.a = a Objects of this class are now used in a …

Total answers: 1

Creating sets of specific extracted values from a .txt file (Python)

Creating sets of specific extracted values from a .txt file (Python) Question: I have a .txt file that says "NAMES," "POINTS" and "SUMMARY" in capital letters, each followed by lines containing data. Each of these three groups is separated by an empty line: NAMES John Cena Sam Smith Selena Gomez POINTS sixteen forty thirty SUMMARY …

Total answers: 2

Search in set of sets

Search in set of sets Question: I would like to search in a set of sets in a specific way: Example (Pseudocode): search = {{1}, {3}} search_base = {{1, 2}, {3, 4}} # search results in True because the 1 can be found in the first set and the 3 in the second. Order doesn’t …

Total answers: 2

It sometimes reverse when I turn a string into a set

It sometimes reverse when I turn a string into a set Question: The string is: x = ‘ABBA’ whenever I use this code: x = ‘ABBA’ x = ”.join(set(x)) print(x) It results in: BA but I want it to be the first letters instead of the second letters: AB Is there any way that I …

Total answers: 2

Get item from set python

Get item from set python Question: I have a set which contains objects which I have the __eq__ and __hash__ functions defined for. I would like to be able to check if an object with the same hash is in the set and if it is in the set to return the object from the …

Total answers: 2

Python set.add() is triggering outside a conditional statement

Python set.add() is triggering outside a conditional statement Question: I’m tokenizing some document and I want to find out which tokens are shared between one or more tokenizations. To do this, for each tokenization, I am looping through the set of all tokens in all tokenizations, called all_tokens and checking if a given token exists …

Total answers: 2

multidimensional jax.isin()

multidimensional jax.isin() Question: i am trying to filter an array of triples. The criterion by which I want to filter is whether another array of triples contains at least one element with the same first and third element. E.g import jax.numpy as jnp array1 = jnp.array( [ [0,1,2], [1,0,2], [0,3,3], [3,0,1], [0,1,1], [1,0,3], ] ) …

Total answers: 1