Комментарии:
it's only works if all elements same data type i mean 25 and "25" will treated differently , i ran into similar issues i had to convert all elements to same data type and then do list set
Ответитьnp.unique
ОтветитьI love your tips so much .
They are so much useful. Keep doing it thank you
Use numpy unique instead
ОтветитьPython secrets and power tips are why I come from time to time to this wonderful channel.
ОтветитьThere is another method: for x in list: for y in cleared_list: if x == y: break; cleared_list.append(x)
ОтветитьSir please tell how to make pandas series in python in vs code
ОтветитьBut why the set changes the order?
Ответитьoh thank you, this way actually saved my time
Ответитьnp.unique
Ответитьyou are genius, wirklich ))
ОтветитьAnd i was doing "for" loops lol 😂
Ответитьsorted(set([3,2,1,1,1,1,3]))
-> [1,2,3]
While searching for my own video I got your video 😂
ОтветитьIn ruby on rails we just do arr.uniq
ОтветитьShort and Sweet!
Ответитьyou can also do it this way:
define a new list, and set it to be empty
and using the in operator check if the number is in the new list or not
if not then it will be added to the new list otherwise it will be skipped
e.g:
numbers = [1,2,3,3,4,5,5]
uniques = []
for number in numbers:
if number not in uniques:
uniques.append(number)
print(uniques)
also if you use *insert*(0,number) instead of append it will be ordered backwards
you could just sort the array and then iterate through the array and check if the adjacent element is the same as the previous element
Ответитьi use ordered dict for making sure I have the same order.
ОтветитьOrderedSet? Or A = []; [A.append(b) for b in B if b not in A]
ОтветитьLove your videos!!! Best time I ever spent watching yt short :D
ОтветитьYou can also use the built in method of
`x.union(x)`, it automatically removes all duplicates
Why is the dict guaranteed to be ordered if it's not an OrderedDict?
ОтветитьEven if order is guaranteed in this case, I would not rely on implicitly ordered dictionaries.
ОтветитьPretty sure python versions is important here. Previous versions of python, dictionaries were not ordered
ОтветитьI wanna do it in java.
ОтветитьIn JS just set(sort())
ОтветитьOr you create an empty list to copy the values of the original list only once
ori_list = [7,4,8,12,7,1,4]
emp_list = [ ]
for I in ori_list():
if I not in emp_list()
emp_list.append(i)
print(emp_list)
Even though yours isn't as long. And you used a simgle function
Pandas unique()
ОтветитьWhy not use list comprehension
ОтветитьHands up the people who did two pointer technique?
ОтветитьMay I know which software you use to do your video editing ? Thank you !
ОтветитьThis is pretty much open source life (Languages, OS, etc.)
Person 1: "What do we call something that removes duplicates from a list?"
Person 2: "Maybe, have an object type called List that supports RemoveDups method as in My list.RmoveDups()?"
Person 1: "Nonsense! Have you not heard open source rule #1? All of our naming conventions must give no god damn clue as to what they do!"
do this one next: Find the Most Common Number(s) in a List. Note: Number(s), as in, it is possible that multiple elements are the most common.
extra challenge could be: List elements can be anything, not necessarily just numbers, it could be dictionaries / lists / tuples, etc. Still find the most common elements.
While this works many times there is actually no guarantee that it will always work since a dictionary does not need to stay ordered.
ОтветитьAnd what is your computation time on that 😂😂😂
ОтветитьLol he sounds exactly like every Bad guy german in every movie ever :D
ОтветитьSuperb, i learnt something new. Thanks.
ОтветитьGood job my BrODAR. 😆
Ответитьnumbers = [1,3,3,2,25,7,2,6,8,231,67,5,9,0,7,9,0]
unique_numbers = set()
for number in numbers:
unique_numbers.add(number)
print(numbers)
print(list(unique_numbers))
Output:
[1, 3, 3, 2, 25, 7, 2, 6, 8, 231, 67, 5, 9, 0, 7, 9, 0]
[0, 1, 2, 3, 67, 5, 6, 7, 8, 231, 9, 25]
Can't you just write this?
my_list = [elements here]
new_list = []
for e in my_list:
if e not in new_list:
new_list.append(e)
Me, who's currently learning Matlab:
Unique()
Hi people, please do not follow this advice. Not only is likely that this behavior is not guaranteed by the python language (and that it could change in newer versions), but the code does not communicate its intention. Ever written in a codebase that makes no sense? It’s from little things like this.
*3rd option: just code it. It will never break and you can actually read the code.
```
duplicate_list = [56, 4, 81, 9, 56, 4]
cleaned_list = list()
seen = set()
for elem in duplicate_list:
if elem in seen:
continue
cleaned_list.append(elem)
seen.add(elem)
```
Note: it might be possible to do this with a list comprehension, but I’m not sure if you can sneak the set.add in there somehow. The loop just works and is easy to follow!
Or use lodash
ОтветитьHi I was recently doing linear regression algorithm from scarch...
when i use my algorithm to predict it works finely with the datasets that is available in scikit learn...
but when i made a dataset of my own.... the values of bias and weights become very large ..,. and when the bias and weights get added or subracted it get some error like ur values become infinity so u cant add or reduce... is that any problem in my algorithm or is that any problem with my dataset
for i in range(5,200,9):
c = (i - 32) * (5/9)
X_train.append([c])
y_train.append(i)
for i in range(239,320,9):
c = (i - 32) * (5/9)
X_test.append([c])
y_test.append(i)
This is how i made my own dataset is there any problem in this.......
What about writing a loop, and use another list or some kind of a data structure that allows to find elements in an efficient way, and... Oh no no no 😲
ОтветитьOnly when on >py3.6 otherwise use ordered dict
ОтветитьOrduh
ОтветитьCan't we simply use for loop
Ответить