set

Make set keep order of a tuple

Make set keep order of a tuple Question: In the code below, how can I use keep using set to have unique values, but also make set keep the order of how the filters are typed in? >>> filters = (‘f1’, ‘f2’) >>> set((*filters, ‘f3’)) {‘f1’, ‘f3’, ‘f2’} # expected: {‘f1’, ‘f2’, ‘f3’} Asked By: …

Total answers: 2

remove an element from a set and return the set

remove an element from a set and return the set Question: I need to iterate over a copy of a set that doesn’t contain a certain element. Until now I’m doing this: for element in myset: if element != myelement: … But I want something like this, in one line: for element in myset.copy().remove(myelement): … …

Total answers: 3

Python set iteration time complexity

Python set iteration time complexity Question: What is the time complexity of the following python code? create set x add n items in x remove n items in x add 1 item in x iterate x m times import time def test(n, m): answer = 0 start = time.time() x = set([i for i in …

Total answers: 1

How to create sets for a column, separated by data types?

How to create sets for a column, separated by data types? Question: I want to create a set in which only the string values of a column appear. The numerical values of the column are to be stored in another set. Asked By: di1a || Source Answers: Try this: my_dataset = [ {‘id’: 1, ‘my_column’: …

Total answers: 2

problem with itertools permutations in combination with english_words_set in a for loop

problem with itertools permutations in combination with english_words_set in a for loop Question: I want to get all possible words with 5,6,7 characters from a random string: word="ualargd" I do the following: A for loop to change the length of my permutations. for i in range(5,len(word)+1): mywords=permutations(word, i) The items created by the permutation get …

Total answers: 1

set() in Python

set() in Python Question: I have used the set() function but I am confused. x = set("car") print(x) Why does this code output: "a", "c", "r" and not "car"? Thanks for your answers. Asked By: Michal Dostal || Source Answers: It’s because of the main properties of set in Python. They can’t contain duplicates They’re …

Total answers: 3

None to Empty List

None to Empty List Question: I have to find the sum of two that are equivalent to value s, can’t use dictionaries, only sets, arrays, and lists. If none, then I have to return an empty list. This is my code, I tried to remove None using if statement, but it didn’t work, not sure …

Total answers: 2

How to discard multiple elements from a set?

How to discard multiple elements from a set? Question: I am trying to discard elements with length less than 10, but it doesn’t work. a = {‘ab’, ‘z x c v b n m k l j h g f f d s a a’, ‘q w e r t y u i o p’} …

Total answers: 2

Make tuple of sets in Python

Make tuple of sets in Python Question: I want to put two sets in a tuple in Python. set1 = set(1, 2, 3, 4) set2 = set(5, 6, 7) What I’ve tried: result = tuple(set1, set2) # got error "TypeError: tuple expected at most 1 argument, got 2" Desired output: ({1, 2, 3, 4}, {5, …

Total answers: 2

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