17 Respuestas
+ 8
Oh it looks like you have to define an array using [] instead of {}. Look at the following code
https://code.sololearn.com/c3hrKJ23XPw6/?ref=app
+ 8
Ah… I got it! I did some test and it seems it works for () and [], but never {}. Thank you very much for helping me understand!
https://code.sololearn.com/cNr1Cdi8If1o/?ref=app
+ 6
静文 毛 Sets are similar to lists or dictionaries.
They are created using curly braces, and are unordered, which means that they can't be indexed.
Due to the way they're stored, it's faster to check whether an item is part of a set using the in operator, rather than part of a list.
a = {1,2,3,4}
print(3 in a)
+ 5
Lea a list makes it ordered because it can be indexed.
And indexing starts from Zero
Eg.
a = [1,2,3,4]
print(a[0]).
Whereas.
A set is unordered and can't be indexed.
The difference is you can access elements in a list by their index while an element in a set is accessed using the in keyword
+ 4
MATOVU CALEB Ok, understand, thank you for helping me!
+ 4
Let me put this for future help and to anyone who comes across this.
Use:
() for tuples
[] for lists
{} for sets and dictionaries
Whenever you want to index, make sure you build your arrays using lists. It helps a lot also whenever you want to change items in the list unlike tuples where you index but can't items in the tuple
With dictionaries, you will index the keys in order to get the values. For sets, it's more useful in mathematics. Basically Real Analysis.
In your case, a more elaborate answer would be,
Array = [[1,2], [1,3]] #N/B this is now a 2D array in the case of numpy
To print 2 directly. Note that the first element in the array is, [1,2] and the second element is [1,3]. Don't forget python is a zero indexing language. Thus,
array[0][1] prints 2 as the output.
[0] means the first item in the array list. And [1] means the second element in the first element of array list.
I hope this clarifies further and adds more on top of the variety of feedbacks you have gotten.
+ 3
You mean, other than the statement
print(array[0][1])
? I don't think there's another way without using this kind of indexing
+ 3
array={[1,2],[3,4]}
print(array[0][1])
The code above doesn’t return “2”, instead it returns a TypeError: unhashable type: ‘list’
+ 3
Oladimeji Abdrahman MATOVU CALEB I checked the difference between list and set. It says:
A set is unordered and unindexed, and dosen’t allow duplicates.
A list is ordered.
How do you understand “ordered”? I kinda get confuse what is ordered/unordered? It seems I can put various type of data in a list as well, i.e.:
sample_list = [1, “red”, False, (0.5, 0.6, 0.7)]
It doesn’t look very order to me….
+ 3
Walter Thank you for helping!
+ 2
That makes sense. Thank you Matovu!
+ 1
Actually, as [] is for array data type, {} is for set data type, so using that means you're declaring an entire different data type although there's a way it can also be access
+ 1
How to haking wife
+ 1
Irfan Ahmed whose wife you wanna hack? I won't recommend you hacking anyone's wife, to preserve world peace =)
0
静文 毛 happy to help =)
- 5