The de-weight of the Python list

Source: Internet
Author: User
Tags python list

#需求: #对列表去重: lis = [2,3,5,3,2,4,8,5,6,7,5] (currently three methods, continuously updated ....) Method of thinking from the https://www.cnblogs.com/nyist-xsk/p/7473236.html, thanks to understand the problem solved after the heavy

Lis = [2,3,5,3,2,4,8,5,6,7,5]#method One: Use the Set () set, which takes advantage of the de-weight of the set () set. The result is a row in ascending order.Lis1 =list (set (LIS))Print(LIS1)#method Two: Use the For loop and not in to determine the list that the method gets to is the same as the previous orderLis2 = [] forIinchlis:ifI not inchlis2:lis2.append (i)Print(LIS2)#method Three: Use the method of GroupBy () in class Itertools the method needs to be sorted first (the result of the final output according to the ordered rules)ImportItertoolslis= [2,3,5,3,2,4,8,5,6,7,5]lis2=[]lis.sort ()#do not set reverse=true default is ascending, setting is descending, this step is requiredLis1 =itertools.groupby (LIS) forKvinchLis1:lis2.append (k)Print(LIS2)#[2, 3, 4, 5, 6, 7, 8] Ascending

#其他方法之后更新

Thanks to the https://www.cnblogs.com/nyist-xsk/p/7473236.html author's ideas to solve the following problems.

#需求: Counts the number of occurrences of an element for lis = [2,3,5,3,2,4,8,5,6,7,5].

Lis = [2,3,5,3,2,4,8,5,6,7,5]# de -lis1 == {} for in Lis1:    # count print(DIC)

#需求如下: Give a list of shopping carts and count the results of the list output

"""
Goods = [{"Name": "Computer", "Price": 1999},
{"Name": "Logitech Mouse", "Price": 10},
{"Name": "Yacht", "Price": 20},
{"Name": "Beauty", "Price": 998},
{"Name": "Logitech Mouse", "Price": 20},
{"Name": "Computer", "Price": 1999},
{"Name": "PC", "Price": 2999}]
#输出结果:
New_goods = [{"Name": "Computer", "Price": 1999, "Count": 2},
{"Name": "Logitech Mouse", "price": Ten, "Count": 1},
{"Name": "Logitech Mouse", "price": +, "Count": 1},
{"Name": "Beauty", "price": 998, "Count": 1},
{"Name": "Yacht", "price": +, "Count": 1},
{"Name": "Computer", "Price": 2999, "Count": 1}]
"""
Goods = [{"name":"Computer"," Price": 1999},         {"name":"Logitech Mouse"," Price": 10},         {"name":"Yacht"," Price": 20},         {"name":"Beauty"," Price": 998},         {"name":"Logitech Mouse"," Price": 20},         {"name":"Computer"," Price": 1999},         {"name":"Computer"," Price": 2999}]#first go to the weightNew_goods = [] forGoodinchGoods:ifGood not inchnew_goods:new_goods.append (good)#to Count forNew_goodinchnew_goods:new_good["Count"] =Goods.count (New_good)Print(New_goods)

The de-weight of the Python list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.