Ternary expressions & List Resolution & Builder expressions

Source: Internet
Author: User

Ternary expression list resolution builder expression

Three -dimensional expression

In making simple judgments, ternary expressions can simplify the code:

def max (x, y):
    if x > y: Return
        x
    else: return
        y
# This function can be reduced to the following form:
def max_new (x,y):
    return x if x > y else y

Format: x If condition else y if condition set, returns x, otherwise returns y list Resolution

List parsing is also a ternary expression that facilitates the generation of another sequence from one sequence.
The basic format is as follows:

Give a few chestnuts:

s = ' Hello '
l =[x.upper () for x in S] Print
(l)    # Prints The result as follows:
[' H ', ' E ', ' l ', ' l ', ' O ']

list = [1,3,59, 45,60]
l_new = [x for x in list if x > m] Print
(l_new) The     results are as follows:
[59, 60]

In specific applications, you should first write out the basic structure of the above figure, sometimes the output expression can be written very complex, the input sequence can also be nested, if the judge can also nest for I in such as the following chestnut:

# The file a.txt content
# Apple 3
# Tesla 100000 1
# Mac 3000 2
# Lenovo 30000 3
# Chicken 3
# requires the use of list resolution, Remove each row from the file a.txt and make the following format
# [{' name ': ' Apple ', ' price ': ' Count ': 3},{...},{},...]

The file = R ' D:\Pythonworks\homework\170616 function list resolves \a.txt ' with
open (file,encoding= ' utf-8 ') as F:
    print ([{' Name ': Line.strip (). Split (') [0],\
            ' price ': Line.strip (). Split (') [1], \
            ' count ': Line.strip (). Split (") [2]} For lines in F
           ]
# The analysis topic can be found that the output expression is a dictionary element, and the input sequence is a line of the file, which first writes the basic form:
# [{' Name ': x, ' Price ': x, ' Count ': x} ' for L ine in F]
# and then break out x

# method from line two:
with open (file,encoding= ' utf-8 ') as F:
    print ([{' Name ': i[0], ' Price ': i[1], ' count ': i[2]}for i in [Line.strip (). Split (") for lines in F]])
# 1 strip () is a space and line break before and after the string is removed. A space within a string is also a character.
# 2 The automatic shutdown file function of the with open () as F: statement, which indicates the indent with, closes.
# 3 above this nested two list parsing. The last list is the input sequence for the final list.
Builder Expression

As with list parsing, you can get a generator by replacing [].

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.