Python list Go heavy

Source: Internet
Author: User
Tags python list
Python list Go heavy

A. {}.fromkeys (list). Keys ()

List2 = {}.fromkeys (list1). Keys ()

Two. Set

List2 = List (set (List1))

Three. Itertools.grouby

ids = [1,4,3,3,4,2,3,4,5,6,1]

Ids.sort ()

it = itertools.groupby (IDs)

For K, G in it:

Print K

Four, stupid method

ids = [1,2,3,3,4,2,3,4,5,6,1]

News_ids = []

For ID in IDs:

If ID not in News_ids:

News_ids.append (ID)

Print News_ids

There are four kinds of features, the re-ordering of the elements after the change, efficiency is said to be the first faster than the second kind of

Five. The index is sorted again this can be de-weighed and maintain element order

#要结果是 [1, 4, 3, 2, 5, 6] do not [1, 2, 3, 4, 5, 6]

ids = [1,4,3,3,4,2,3,4,5,6,1]

News_ids = List (set (IDS))

News_ids.sort (Key=ids.index)

Print News_ids #[1, 4, 3, 2, 5, 6]

VI: Reduce

ids = [1,4,3,3,4,2,3,4,5,6,1]

Func = lambda x,y:x if y in x else x + [y]

Print reduce (func, [[],] + IDs) #[1, 4, 3, 2, 5, 6]

  • 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.