This question already has answers here:
Does Python have an ordered set?
(15 answers)
Closed 3 years ago.
Is there a way of ordering a set in base python?
For example:
A = {1, 2, 3}
B = {3, -6, 2, 0}
print union(A, B)
Expected Output:
({-6, 0, 1, 2, 3}, 5)
My attempt:
x = A | B
y = len(x)
print((set(x), y))
My output:
({0, 1, 2, 3, -6}, 5)
I have read some of the answers for other questions and there are ways of doing it with various packages, but for this exercise, I am NOT meant to import any packages, just doing it in base python (if that is what it is called) if possible.
No, sets are unordered by definition and implementation. To create a sorted list, convert your set to a list and sort it:
List = sorted(your_set)
Related
This question already has answers here:
How can I create a Set of Sets in Python?
(4 answers)
How do I add two sets?
(5 answers)
Closed 2 months ago.
i can't add set to python
you can add tuple to python set
a={1,2,3,4,5}
a.add((10,11,12))
when i try same with set
a={1,2,3,4,5}
a.add({10,11,12})
TypeError: unhashable type: 'set'
You can add frozensets to a set, since they are hashable (because they are immutable).
a = {1,2,3,4,5}
a.add(frozenset([10,11,12]))
I'd assume you are trying to get:
{1, 2, 3, 4, 5, 10, 11, 12}
You are appending a set which is unhashable. And when you add the tuple you will get:
{1, 2, 3, 4, 5, (10, 11, 12)}
You are trying you get the union:
a.union({10,11,12})
Or:
a | {10,11,12}
This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Understanding slicing
(38 answers)
Closed 6 months ago.
In C++, we do reverse(nums.begin() + 1, nums.end() - 2) for reversing a list or vector in ranges. So, is there something like this in Python for achieving the same?
All you need to do is use list slicing syntax:
>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> l[3:6] = l[5:2:-1]
>>> l
[1, 2, 3, 6, 5, 4, 7, 8, 9, 10]
This question already has answers here:
How can I generate a list of consecutive numbers? [duplicate]
(8 answers)
Closed 4 years ago.
I'm sure python has a built in way to create an x size list where the contents are 0 through x-1, but I don't know how to do it. I've searched on Google as well as on here, I'm sure that I must've not been using the correct wording to find what I needed. Please help.
Ex: len([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) = 10
You are looking for the range builtin:
range(x) # python 2
list(range(x)) # python 3
This question already has answers here:
Best way to find the intersection of multiple sets?
(7 answers)
Closed 5 years ago.
I have a list of n sets of integers denoted as lst = [S1, S2, S3 ... Sn] and I want to find the intersection of all the sets.
Is there an optimal way to do this?
If you have a list sets, you can trivially get their intersection with:
set.intersection(*lst)
This will produce a new set with only those values that are common between all the sets:
>>> lst = [{1, 2, 3}, {3, 5}, {2, 3}]
>>> set.intersection(*lst)
{3}
Edit: Misread, thought that you have multiple lists of numbers and generally asking how to find those numbers that are present in all of them. Will keep the original answer below, though, as some people still found it helpful to some degree.
Yes, it's called set intersection and can be used on the set data type.
Demo:
>>> s = set((1, 2, 3))
>>> s2 = set((2, 3, 4))
>>> s3 = set((3, 4, 5))
>>> s & s2
{2, 3}
>>> s & s2 & s3
{3}
If your current data is stored in lists, converting it to sets is just a matter of passing the lists to the set() constructor:
>>> numbers = [2, 7, 9, 10]
>>> set(numbers)
{2, 7, 9, 10}
Mind, though, that if a list contains duplicates, that information will get lost, and each duplicated element will only be present once in the resulting intersection.
This question already has answers here:
Is it safe to rely on Python function arguments evaluation order? [duplicate]
(2 answers)
How to prove that parameter evaluation is "left to right" in Python?
(6 answers)
Order of execution of expressions in Python
(3 answers)
Does python have an arguments processing order well defined?
(2 answers)
Closed 5 years ago.
I would like to understand the way Python evaluates function calls inside print statements or, more particulary, what does the call for
next(some_iter) return? A mutable reference to the element in the next position or just a copy of the element in the next position when some_iter was last evaluated?
The following code:
def foo(val, arr=set()):
arr.add(val)
return arr
print(foo(1))
print(foo(2))
print(foo(1), foo(3), foo(0))
Returns the output:
{1}
{1, 2}
{0, 1, 2, 3} {0, 1, 2, 3} {0, 1, 2, 3}
Python calls the function three times, holding what seems to be a reference to arr, then, since sets are mutable, the output is as can be seen above. foo(1) return value was not saved, that is {1, 2} when it is called in order of the print statement.
However
lst = [1, 2, 3, 4]
def bar(a):
a = lst
a[0] = 17
return a
x = iter(lst)
y = iter(lst)
print(next(x), bar(lst), next(y), next(x))
print(lst)
Would print
1 [17, 2, 3, 4] 17 3
[17, 2, 3, 4]
I would have expected
17 [17, 2, 3, 4] 17 3
[17, 2, 3, 4]
That is for next to hold a reference to the first element of lst, which would later be changed to 17, and for that reference to be printed as 17 eventually, but it is computed first returning 1.
Why does it not happen? In what way is the next() or iters different?