Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of practical skills of Python

Shulou Source: shulou.com Published: 2022-06-01 13:05:03 10月02日 Update

This article introduces the relevant knowledge of "case Analysis of Python practical skills". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1 、 List:all_equal

Implementation: verify that all elements of the list are the same

Tips: use [1:] and [:-1] to compare all the elements of the list

Def all_equal (lst): return lst [1:] = = lst [:-1]

Example:

All_equal ([1, 2, 3, 4, 5, 6]) # Falseall_equal ([1, 1, 1]) # True

2 、 List:all_unique

Implementation: return true if all values in the list are unique, otherwise false

Tips: use the set set () to remove duplicates in the list and compare the length of the original list with the result

Def all_unique (lst): return len (lst) = = len (set (lst))

Example:

X = [1, Trueall_unique, 3, 4, 5, 5, 6] y = [1, Trueall_unique, 2, 3, 4, 5] False

3 、 List:bifurcate

Implementation: list values are grouped. If the filter element is true, then the corresponding element belongs to the first group, otherwise it belongs to the second group

Tips: list derivation and enumerate () based on filter elements to each group

Def bifurcate (lst, filter): return [[x for iMagne x in enumerate (lst) if filter [I] = = True], [x for iMagne x in enumerate (lst) if filter [I] = = False]]

Example:

Bifurcate (['beep',' boop', 'foo',' bar'], [True, True, False, True]) # [['beep',' boop', 'bar'], [' foo']]

4 、 List:difference

Implementation: returns the difference between two iterables

Tips: create a collection of b and use the list derivation of a to retain elements that are not in _ b

Def difference (a, b): _ b = set (b) return [item for item in an if item not in _ b]

Example:

Difference ([1,2,3], [1,2,4]) # [3]

5 、 List:flatten

Implementation: an one-time integrated list

Tips: extract each value of a sublist using a nested list

Def flatten (lst): return [x for y in lst for x in y]

Example:

This is the end of the introduction of "Python practical skills case Analysis" of flatten ([[1pyrum 2pyrm 3reagio 4], [5pyrrine 6pyror8]) # [1pyrmus 2pyrmus 3pyrrine 4,5pyrrine 6pyrmus 7pyrrine 8]. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Examples elements practicality case analysis practical skills skills analysis content more knowledge achievements and then one-off two dilemmas reality differences situations articles cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Shulou Tech Info Redmi Apple