Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
5 ตอบกลับ
18378 มุมมอง

iam hving list with repeated elements. so in this case how to delete the duplicate elements in the list using python?

อวตาร
ละทิ้ง

Refer this link and check different methods through which you can remove duplicate elements from list in python.

https://www.geeksforgeeks.org/python-ways-to-remove-duplicates-from-list/

คำตอบที่ดีที่สุด

I usually used list_wo_no_duplicate = list(set(list_with_ducplicates))  to remove duplicates.  Although I think there is no harm for not setting the variable to list again as set in most cases.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

For example:

def formatList(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if not (x in seen or seen_add(x))]

There are loads of topics about this on stackoverflow and Google. This example is taken from http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order and also keeps the order of your list!

 

A second option could be to create a second list, which only contains the clean values. In case you would like to keep both lists.

for i in mylist:
     if i not in newlist:
          newlist.append(i)

อวตาร
ละทิ้ง
ผู้เขียน

list = set(list) is this correct?

คำตอบที่ดีที่สุด

It can be easily done by given python code:

duplicate_list = [1, 3, 5, 1, 4, 5]

new_list = list(set ( duplicate_list))

 Thanks, Husain 

อวตาร
ละทิ้ง

Hi, I can get the id of duplicate records and use set() function which works perfectly for item ids but I would like to add the quantities of duplicate values. Can you guide how to sum the quantities of all ids in one2many?

คำตอบที่ดีที่สุด

Take a look at this blog 

How to hide an options from 'More' button?
 
https://odooforbeginnersblog.wordpress.com/2017/06/11/how-to-hide-an-options-in-more-button/
อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
7
มี.ค. 20
7323
1
มี.ค. 17
5121
1
ม.ค. 22
2007
2
ส.ค. 19
4394
3
ม.ค. 19
2782