Py学习  »  Python

喝杯咖啡的功夫就能学会的100个非常有用的Python技巧(2)

机器学习研究组订阅 • 2 年前 • 273 次点击  

作者:Fatos Morina

编译:ronghuaiyang

导读

接上一篇,34~66条。

34. Strings 和 tuples 是不可修改的

这一点在上一点中已经提到过,但我想强调一下,因为这是非常重要的。

name = "Fatos"
print(id(name))  # 4422282544

name = "fatos"
print(id(name))  # 4422346608
my_tuple = (1234)
print(id(my_tuple))  # 4499290128

my_tuple = ('a''b')
print(id(my_tuple))  # 4498867584

35. Lists, sets, 和 dictionaries 是不可修改的

这意味着我们可以在不丢失绑定的情况下更改对象:

cities = ["Munich""Zurich""London"


    
]
print(id(cities))  # 4482699712

cities.append("Berlin")
print(id(cities))  # 4482699712

下面是另一个集合的例子:

my_set = {1234}
print(id(my_set))  # 4352726176

my_set.add(5)
print(id(my_set))  # 4352726176

36. 你可以把集合转换为不可修改的集合

这样,你就不能再修改它了:

my_set = frozenset(['a''b''c''d'])

my_set.add("a")

如果你这样做,就会抛出一个错误:

AttributeError: 'frozenset' object has no attribute 'add'

37. " if-elif "块可以在没有else块的情况下存在

但是,如果前面没有“if”,“elif”就不能独立存在:

def check_number(number):
    if number > 0:
        return "Positive"
    elif  number == 0:
        return "Zero"

    return "Negative"


print(check_number(1))  # Positive

38. 使用sorted()查看2个字符串是否是相同的字母但次序不一样

def check_if_anagram(first_word, second_word):
    first_word = first_word.lower()
    second_word = second_word.lower()
    return sorted(first_word) == sorted(second_word)

print(check_if_anagram("testinG""Testing"))  # True
print(check_if_anagram("Here""Rehe"))  # True
print(check_if_anagram("Know""Now"))  # False

39. 获取一个字符的Unicode值

print(ord("A"))  # 65
print(ord("B"))  # 66
print(ord("C"))  # 66
print(ord("a"))  # 97

40. 一行代码获取字典中所有的keys

dictionary = {"a"1"b"2

"c"3}

keys = [i for i, _ in dictionary.items()]

print(keys)  # ['a', 'b', 'c']

41. 一行代码获取字典中所有的值

dictionary = {"a"1"b"2"c"3}

values = [i for _, i in dictionary.items()]

print(values)  # [1, 2, 3]

42. 交换字典中的keys和values

dictionary = {"a"1"b"2"c"3}

reversed_dictionary = {j: i for i, j in dictionary.items()}

print(reversed)  # {1: 'a', 2: 'b', 3: 'c'}

43. 你可以将布尔型值转换为数字

print(int(False))  # 0
print(float(True))  # 1.0

44. 你可以算术操作中使用布尔值

“False”是0,而“True”是1。

x = 10


    

y = 12
result = (x - False)/(y * True)
print(result)  # 0.8333333333333334

45. 你可以将任何数据的类型转换为布尔值

print(bool(.0))  # False
print(bool(3))  # True
print(bool("-"))  # True
print(bool("string"))  # True
print(bool(" "))  # True

46. 将一个值转换为复数

print(complex(102))  # (10+2j)

也可以将数字转换为十六进制数:

print(hex(11))  # 0xb

47. 把值加到列表的第一个位置

如果你使用append(),你将从右边插入新的值。

也可以使用*insert()*来指定要插入新元素的索引和元素。在我们的例子中,我们想把它插入到第一个位置,所以我们使用0作为下标:

my_list = [345]

my_list.append(6)
my_list.insert(02)
print(my_list)  # [2, 3, 4, 5, 6]

48. Lambda方法只能在一行里

在使用lambdas方法的时候,不能超过一行。

让我们试试以下方法:

comparison = lambda x: if x > 3:
                    print("x > 3")
                else:
                    print("x is not greater than 3")

将抛出以下错误:

result = lambda x: if x > 3:
 ^
SyntaxError: invalid syntax

49. lambda中的条件语句应该始终包含“else”部分

我们试下下面的:

comparison = lambda x: "x > 3" if x > 3

我们将得到以下错误:

comparison = lambda x: "x > 3" if x > 3
                                          ^
SyntaxError: invalid syntax

50. filter() 返回一个新的对象

my_list = [1234]

odd = filter(lambda x: x % 2 == 1, my_list)

print(list(odd))   # [1, 3]
print(my_list)  # [1, 2, 3, 4]

51. map() 返回一个新的对象

my_list = [1234]

squared = map(lambda x: x ** 2, my_list)

print(list(squared))   # [1, 4, 9, 16]
print(my_list)  # [1, 2, 3, 4]

52. range() 中有一个步长的参数,但是知道的并不多

for number in range(1103):
    print(number, end=" ")
# 1 4 7

53. range() 默认从0开始

所以你根本不需要包含它。

def range_with_zero(number):
    for i in range(0, number):
        print(i, end=' ')


def range_with_no_zero(number):
    for i in range(number):
        print(i, end=' ')


range_with_zero(3)  # 0 1 2
range_with_no_zero(3)  # 0 1 2

54. 不需要将长度和0比较

如果长度大于0,则默认为True,所以你不需要将其与0进行比较:

def get_element_with_comparison(my_list):
    if len(my_list) > 0:
        return my_list[0]

def get_first_element(my_list):
    if len(my_list):
        return my_list[0]

elements = [1234]
first_result = get_element_with_comparison(elements)
second_result = get_element_with_comparison(elements)

print(first_result == second_result)  # True

55. 可以在同一范围内多次定义相同的方法

但是,只有最后一个被调用,因为它覆盖了以前的。

def get_address():
    return "First address"

def get_address():
    return "Second address"

def get_address():
    return "Third address"

print(get_address())  # Third address

56. 你可以访问私有属性

class Engineer:
    def __init__(self, name):
        self.name = name
        self.__starting_salary = 62000

dain = Engineer('Dain')
print(dain._Engineer__starting_salary)  # 62000

57. 查看对象的内存使用

import sys

print(sys.getsizeof("bitcoin"))  # 56

58. 定义一个方法,调用的时候想传多少参数都可以

def get_sum(*arguments):
    result = 0
    for i in arguments:
        result += i
    return result


print(get_sum(123))  # 6
print(get_sum(12345))  # 15
print(get_sum(1234567))  # 28

59. 使用super() 或者父类的名字调用父类的初始化

使用*super()*调用父类初始化器:

class Parent:
    def __init__(self, city, address):
        self.city = city
        self.address = address

class Child(Parent):
    def __init__(self, city, address, university):
        super().__init__(city, address)
        self.university = university

child = Child('Zürich''Rämistrasse 101''ETH Zürich')
print(child.university)  # ETH Zürich

使用父类的名称调用父类:

class


    
 Parent:
    def __init__(self, city, address):
        self.city = city
        self.address = address


class Child(Parent):
    def __init__(self, city, address, university):
        Parent.__init__(self, city, address)
        self.university = university


child = Child('Zürich''Rämistrasse 101''ETH Zürich')
print(child.university)  # ETH Zürich

注意,使用**init()super()**调用父类初始化器只能在子类的初始化器中使用。

60. 你可以在自己的类中重新定义“+”操作符

当你在两个int数据类型之间使用**+**操作符时,你将得到它们的和。

然而,当你在两个字符串数据类型之间使用它时,你将合并它们:

print(10 + 1)  # Adding two integers using '+'
print('first' + 'second')  # Merging two strings '+'

这表示操作符重载

你也可以在你自己的类中使用它:

class Expenses:
    def  __init__(self, rent, groceries):
        self.rent = rent
        self.groceries = groceries

    def __add__(self, other):
        return Expenses(self.rent + other.rent,
                        self.groceries + other.groceries)


april_expenses = Expenses(1000200)
may_expenses = Expenses(1000300)

total_expenses = april_expenses + may_expenses
print(total_expenses.rent)  # 2000
print(total_expenses.groceries)  # 500

61. 你还可以在自己的类中重新定义“

下面是另一个你可以自己定义的操作重载的例子:

class Game:
    def __init__(self, score):
        self.score = score

    def __lt__(self, other):
        return self.score 

first = Game(1)
second = Game(2)

print(first # True

类似地,就像前面的两种情况,我们可以根据自己的需要重写*eq()*函数:

class Journey:
     def __init__(self, location, destination, duration):
        self.location = location
        self.destination = destination
        self.duration = duration

    def __eq__(self, other):
        return ((self.location == other.location) and
                (self.destination == other.destination) and
                (self.duration == other.duration))


first = Journey('Location A''Destination A''30min')
second = Journey('Location B''Destination B''30min')

print(first == second)

你也可以类似地定义:

  • sub() for -
  • mul() for *****
  • truediv() for /
  • ne() for !=
  • ge() for >=
  • gt() for >

62. 你可以为类的对象定义一个自定义的可打印版本

class Rectangle:


    

    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __repr__(self):
        return repr('Rectangle with area=' + str(self.a * self.b))


print(Rectangle(34))  # 'Rectangle with area=12'

63. 交换字符串中的字符大小写

string = "This is just a sentence."
result = string.swapcase()print(result)  # tHIS IS JUST A SENTENCE.

64. 检查是否所有字符都是字符串中的空格

string = "  "
result = string.isspace()print(result)  # True

65. 检查字符串中的所有字符是否都是字母或数字

name = "Password"
print(name.isalnum())  # True, because all characters are alphabetsname = "Secure Password "
print(name.isalnum())  # False, because it contains whitespacesname = "S3cur3P4ssw0rd"
print(name.isalnum())  # Truename = "133"
print(name.isalnum())  # True, because all characters are numbers

66. 检查字符串中的所有字符是否都是字母

string = "Name"
print(string.isalpha())  # Truestring = "Firstname Lastname"
print(string.isalpha())   # False, because it contains whitespacestring = “P4ssw0rd”
print(string.isalpha())  # False, because it contains numbers


END

英文原文:https://towardsdatascience.com/100-helpful-python-tips-you-can-learn-before-finishing-your-morning-coffee-eb9c39e68958


想要了解更多资讯,请扫描下方二维码,关注机器学习研究会

                                          


转自:AI公园

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/114341
 
273 次点击