How can i add subset to set in python [duplicate] - python

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}

Related

Is there any similar function to reverse the list in Python for ranges like in C++? [duplicate]

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]

set(sorted()) method in python [duplicate]

This question already has answers here:
Sorting a set of values
(2 answers)
Closed 1 year ago.
When I used sorted on the set in python it returns sorted list. But I want to return the set. I tried set(sorted()) but it doesn't sort the set.
I tried another way. But why set(sorted()) isn't sorting the set?
my tried code
Sets are unsorted, that is their nature. Taking a set (or any collection) then sorting it, then making a set out of that, will be unlikely to produce sorted results.
If you require the items in a sorted manner, you should turn them into something that can be sorted (like a list, which is what you get from sorted(someSet)). For example:
>>> a = {100, 99, 1, 2, 5, 6} # A set
>>> sorted(a) # A sorted list from that set.
[1, 2, 5, 6, 99, 100]
>>> set(sorted(a)) # An (unsorted) set from that sorted list.
{1, 2, 99, 100, 5, 6}

covert list of integers to string [duplicate]

This question already has answers here:
Easiest way to join list of ints in Python?
(3 answers)
How can I convert each item in the list to string, for the purpose of joining them? [duplicate]
(9 answers)
Closed 1 year ago.
I want to convert a list to string e.g list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
would be list = '1234567890'
I tried ''.join() but this doesn't work since the list consists of integers
You need to convert each item to string first
''.join(str(x) for x in list)
As you have a list of int values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] (don't use builtin list for naming) you may convert them to str before with
a generator expression
result = ''.join(str(x) for x in values)
map function
result = ''.join(map(str, values))
you can try:
lst=[1,3,2,4,4]
list_string=''
for i in lst:
list_string+=str(i)
print(list_string)
note: you can not use list as variable.

Ordering a set in base python [duplicate]

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)

Generate a list of length x: [0, .., x-1] [duplicate]

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

Categories