Essay python (2)

== python and the difference is in the

Are used to determine whether two variables are equal, the difference in:

  is: Analyzing two variable references are equal. Equal values, not necessarily the same references.

  ==: determining whether the value of the variable is equal to two. If the same reference value is equal to the constant.

. 1 = A 
B =. 1 
Print (ID (A)) # 1575434496 
Print (ID (B)) # 1575434496 
Print (A IS B) # True 
Print (A == B) True # 

A = 'Hello World' 
B = ' World Hello ' 
Print (B IS A) # True 
Print (A == B) True # 
# when only numeric and string type, a is b is True 

# for the list (list), tuple (tuple), dictionaries (dict), a collection (set) a is b is False 
A = [. 1, 2,. 3] 
B = [. 1, 2,. 3] 
Print (ID (A)) # 55,002,536 
Print (ID (B)) # 55,003,696 
Print (A IS B) False # 
Print (A == B) True # 

A = { 'name': 'Tom', 'Age':. 19} 
B = { 'name': 'Tom', 'Age':. 19} 
Print (A IS B) False # 
Print (A == B) True #

In fact, in python,

For integer between -5 to 256 (containing -5 and 256) will be initialized in memory, so when assign values ​​to variables, if in this interval, they are all pointing to the same memory address.

If Outside this range, or non-integer, it will allocate new memory space. It will return False.

In PyCharm in, because the interpreter is optimized, so the test of time even more than this range a is b also returns True.

So we only show the test results to the test terminal

##############################################终端测试##################################################
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a = -5 >>> b = -5 >>> print(a is b) True >>> a = -6 >>> b = -6 >>> print(a is b) False >>> a = 256 >>> b = 256 >>> print(a is b) True >>> a = 266 >>> b = 266 >>> print(a is b) False >>> a = 6.666 >>> b = 6.666 >>> print(a is b) False

 

A mechanism for Python interpreter intern (string resides) is.

Meaning that with a string constant pool only save a copy, so create two different strings, they all point to the same address of the constant pool.

But note that when a string has a space, it will not trigger intern mechanism (optimized interpreter Pycharm in, still triggers).

>>> a = 'helloworld'
>>> b = 'hello' + 'world'
>>> print(a is b)
True
>>> a = 'hello world'
>>> b = 'hello world'
>>> print(a is b)
False

 

Any array to achieve a specific sort

topic:

  Given an unordered list, according to the requirements and the positive sequence preceding odd arranged, after an even number, in reverse order.

  Such as: [5, 1, 8, 9, 2, 3, 6, 5, 7]. 

  After the order of: [1, 3, 5, 5, 7, 9, 8, 6, 2]

Solution a: The first idea, the list may be the subject, according to odd, even divided into two lists. Line thereof, respectively positive and reverse order. In the combination thereof.

# Initial list 
ARR = [. 5,. 1,. 8,. 9, 2,. 3,. 6,. 5,. 7] 

# define an even list 
A = [] 
# define an odd list 
B = [] 
for I in ARR: 
    # even-put into a list 
    IF I% 2 == 0: 
        a.append (I) 
        Continue 
    # b into odd list 
    b.append (I) 

# even number list in reverse order 
a.sort (Reverse = True) 
# default sort odd n sequence 
b.sort () 
B = B + A 
# [. 1,. 3,. 5,. 5,. 7,. 9,. 8,. 6, 2] 
Print (B)

Solution two: the first list in reverse order, then traverse, when faced with an odd number, it is removed from the list and inserted into the first one.

# Initial list 
ARR = [. 5,. 1,. 8,. 9, 2,. 3,. 6,. 5,. 7] 
# reverse order 
arr.sort (reverse = True) # [ 9, 8, 7, 6, 5, 5, 3 , 2,. 1] 
for I in Range (len (ARR)): 
    IF ARR [I]% 2 = 0:! 
        # encounter an even number, it is removed from the list (POP), and placed in the first a 
        arr.insert (0, arr.pop (I)) 
Print (ARR) # [. 1,. 3,. 5,. 5,. 7,. 9,. 8,. 6, 2]

Solution three: the use of functions and sorted lambda customize sorting. When an even number, return the largest number than the original list have to fight to the number. When an odd number, the return itself

arr = [5, 1, 8, 9, 2, 3, 6, 5, 7]
# [1, 3, 5, 5, 7, 9, 8, 2, 6]
print(sorted(arr, key=lambda x: x % 2 == 0 and 20 - x or x))

 

Python is calculated using the number of lightning within a million

Indian mathematician Kapuliejia (Dattaraya Ramchandra Kaprekar, 1905 - 1986) in one trip, encountered a violent storm, he saw a sign on the roadside was split in two, says 30 half, the other half write with 25. At this time, he suddenly found that 30 + 25 = 3025 = 2 ^ 55, 55, the number of combined split in half, then the square, just the original number. This figure, called the lightning or the number of american Karp number. (From Baidu Encyclopedia)

Ray minimum number of split is 81:  . 8. 1 = + 9² = 81. 9

Since it can be divided into two figures illustrate this length must be an even number, knows it very convenient to be able to calculate the number of lightning within a one million.

method one:

I in Range for (1000000): 
    I = STR (I) 
    # length of the filter is not even a digital 
    IF len (I)% 2 = 0:! 
        Continue 
    # A / B are two data 
    A = 0 
    B = 0 
    # if two bits taken directly 
    IF len (I) == 2: 
        A = int (I [0]) 
        B = int (I [. 1]) 
    the else: 
        # slice taken 
        a = int (i [: int (len (i) / 2)]) 
        B = int (I [int (len (I) / 2):]) 
    # results obtained 
    IF (A + B) ** 2 == int (I): 
        Print (I)

Second way:

I in Range for (1000000): 
    A = len (STR (I)) 
    IF A = 2% 0:! 
        Continue 
    the else: 
        # interception by obtaining the entire remainder of the embodiment 
        B ** 10 = (A // 2) 
        X int = (I / B) 
        Y% B = I 
        IF (X + Y) ** 2 == I: 
            Print (I)

  

The special string conversion Python

Any given string, requirements:

  • The lowercase letters to uppercase

  • Convert uppercase letters to lowercase

  • 9 is converted into a digital value by subtracting this number

# 方法一
def transform(string):
    new_str = ''
    for i in string:
        if i.isdigit():
            new_str += str(9 - int(i))
        else:
            new_str += i.swapcase()
    return new_str


# 方法二
def transform2(string):
    new_str = ''
    for i in string:
        if i.isdigit():
            new_str += str(9 - int(i))
        elif i.isupper():
            new_str += i.lower()
        elif i.islower():
            new_str += i.upper()
        else:
            new_str += i
    return new_str

if __name__ == '__main__':
    string = 'ava1241@o3#1-1231;.,,^$d'
    print(transform(string)) # AVA8758@O6#8-8768;.,,^$D
    print(transform2(string)) # AVA8758@O6#8-8768;.,,^$D
########## a method that can be written more pythonic ############ 
String = 'ava1241 O3 @ # 1-1231;. ,, ^ $ D' 
Result = '' .join ([STR (. 9 - int (I)) IF i.isdigit () the else i.swapcase () for I in String]) 
Print (Result) # AVA8758 O6 @ # 8-8768; ,, ^. $ D

isdigit (): for detecting whether a string of pure numbers, as long as if it contains a non-numeric return False.

= A '12315' 
Print (a.isdigit ()) True # 
# characters comprising A 
B = '12345a' 
Print (b.isdigit ()) False # 
# symbol comprises% 
C = '123% 15' 
Print (c.isdigit ()) # False

isalpha (): detecting whether the string only letters

a = 'abcd'
print(a.isalpha()) # True
b = 'abcd123'
print(b.isalpha()) # False
c = 'abc$d'
print(c.isalpha()) # False

swapcase (): for converting a string of uppercase and lowercase letters

a = 'aBcD%Fe'
print(a.swapcase()) # AbCd%fE

 

# All uppercase letters 
Print (A.upper ()) ABCDEFG # 
# to convert all lowercase letters 
Print (a.lower ()) ABCDEFG # 

# determines whether capitalized False 
Print ( 'aaa'.isupper ()) 
# determines whether lowercase True # 
Print ( 'bbb'.islower ())

 

Guess you like

Origin www.cnblogs.com/jjb1997/p/11410049.html