set

Explaining appearent idiosyncrasy in Python when using tuples as elements in a set

Explaining appearent idiosyncrasy in Python when using tuples as elements in a set Question: If we execute the two following lines in Python: a = (1,) {a} it won’t cause any problems. Instead, if we execute the two following lines: a = ([1],) {a} now what we get is "TypeError: unhashable type: ‘list’". In both …

Total answers: 1

Why set.discard doesn't throw an error when a set is passed to it in Python?

Why set.discard doesn't throw an error when a set is passed to it in Python? Question: My question is quite simple. When I run someSet = {1,2,3,4} someSet.discard([5]) It gives the error: Traceback (most recent call last): File "File.py", line 2, in <module> someSet.discard([5]) TypeError: unhashable type: ‘list’ Just like list, sets are also unhashable …

Total answers: 1

My sets is not being updated after I do operations in a fucntion

My sets is not being updated after I do operations in a fucntion Question: I have 2 defined sets they are set1 = {1, 2, 3} set2 = {1, 3, 4, 6} and I have 2 fucntions. One for add a value to sets and one for print it def printSet(): x = input("Enter name …

Total answers: 1

Find all words in binary buffer using Python

Find all words in binary buffer using Python Question: I want to find in binary buffer (bytes) all the "words" build from ascii lowercase and digits that only 5 chars length. For example: bytes(b’ax1109ertx01x03a54bbx05′) contains a54bb and 09ert . Note the string abcdef121212 is larger than 5 chars so I don’t want it I have …

Total answers: 1

Can someone explain why this function returns None?

Can someone explain why this function returns None? Question: I’ve been trying to figure out why this function is returning None every time I run it, I would appreciate a lot if someone could explain my why. x = set([1,2,3]) def inserta(multiconjunto, elemento): a = multiconjunto.add(elemento) return a mc1 = inserta(x, 2) print(mc1) Asked By: …

Total answers: 1

Python convert csv to json with specific format

Python convert csv to json with specific format Question: I am trying to write a python script that takes the following structured CSV file: "id","city","diacritice","county","auto","zip","populatie","lat","lng" "1","Buftea","Buftea","Ilfov","IF","70000","19202","44.5629744","25.9388214" "2","Buciumeni","Buciumeni","Ilfov","IF","70000","2976","44.5460939","25.9574846" "3","Otopeni","Otopeni","Ilfov","IF","75100","12540","44.5671874","26.0835113" "4","Odaile","Odăile","Ilfov","IF","75100","1321","44.5435944","26.0487028" and converts it to the following json file: { "locations": [ { "name": "New York", "slug": "", "description": "", "meta": [ {"image_id" : 45}, {"_icon" : …

Total answers: 1

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