找到 34423 篇文章 相关编程

Python – 具有唯一值列表的字典

AmitDiwan
更新于 2021-09-13 11:51:39

118 次查看

当需要获取具有唯一值列表的字典时,会使用“set”运算符和列表方法,以及简单的迭代。示例下面是同一个的演示 -my_dictionary = [{'Python' : 11, 'is' : 22}, {'fun' : 11, 'to' : 33}, {'learn' : 22}, {'object':9}, {'oriented':11}] print("The dictionary is : " ) print(my_dictionary) my_result = list(set(value for element in my_dictionary for value in element.values())) print("The resultant list is : ") print(my_result) print("The resultant list after sorting is : ") my_result.sort() print(my_result)输出The dictionary is : [{'Python': 11, 'is': 22}, {'fun': 11, 'to': ... 阅读更多

Python – 获取矩阵均值

AmitDiwan
更新于 2021-09-13 11:50:12

188 次查看

当需要获取矩阵元素的均值时,在将其导入环境后,会使用“Numpy”包中的“mean”方法。示例下面是同一个的演示 -import numpy as np my_matrix = np.matrix('[24, 41; 35, 25]') print("The matrix is : " ) print(my_matrix) my_result = my_matrix.mean() print("The result is : ") print(my_result)输出The matrix is : [[24 41] [35 25]] The result is : 31.25解释将所需的包导入环境。使用 Numpy 包创建一个矩阵。它显示在控制台上。矩阵的均值是 ... 阅读更多

Python – 如果键存在于列表和字典中,则提取键的值

AmitDiwan
更新于 2021-09-13 11:49:16

485 次查看

当需要提取键的值(如果键存在于列表和字典中)时,会使用简单的迭代和“all”运算符。示例下面是同一个的演示 -my_list = ["Python", "is", "fun", "to", "learn", "and", "teach", 'cool', 'object', 'oriented'] my_dictionary = {"Python" : 2, "fun" : 4, "learn" : 6} K = "Python" print("The value of K is ") print(K) print("The list is : " ) print(my_list) print("The dictionary is : " ) print(my_dictionary) my_result = None if all(K in sub for sub in ... 阅读更多

Python – 按键的第 i 个索引值对字典列表进行排序

AmitDiwan
更新于 2021-09-13 11:47:30

382 次查看

当需要根据键的第“i”个索引值对字典列表进行排序时,会使用“sorted”方法和lambda方法。示例下面是同一个的演示 -my_list = [{"Python" : "Best", "to" : "Code"}, {"Python" : "Good", "to" : "Learn"}, {"Python" : "object", "to" : "cool"}, {"Python" : "oriented", "to" : "language"}] print("The list is : " ) print(my_list) K = "Python" print("The value of K is ") print(K) i = 2 print("The value of i is :") print(i) my_result = ... 阅读更多

Python – 检查拆分是否相等

AmitDiwan
更新于 2021-09-13 11:45:31

203 次查看

当需要检查字符串中的拆分是否相等时,会使用“len”方法、“list”方法和“set”运算符以及“if”条件。示例下面是同一个的演示 -my_string = '96%96%96%96%96%96' print("The string is : " ) print(my_string) my_split_char = "%" print("The character on which the string should be split is :") print(my_split_char) my_result = len(list(set(my_string.split(my_split_char)))) == 1 print("The resultant list is : ") if(my_result == True): print("All the splits are equal") else: print("All the splits are not equal")输出The string is ... 阅读更多

Python – 元素的分组连续范围索引

AmitDiwan
更新于 2021-09-13 11:43:27

150 次查看

当需要获取列表中元素的分组连续范围索引时,会创建一个 defaultdict。会使用简单的迭代以及“groupby”方法、“len”方法、“list”方法和“append”方法。示例下面是同一个的演示 -from itertools import groupby from collections import defaultdict my_list = [63, 12, 84, 91, 52, 39, 25, 27, 20, 11, 0, 9] print("The list is : " ) print(my_list) my_index = 0 my_result = defaultdict(list) for key, sub in groupby(my_list): element = len(list(sub)) my_result[key].append((my_index, my_index + ... 阅读更多

Python – 在前缀出现时拆分字符串

AmitDiwan
更新于 2021-09-13 11:39:00

341 次查看

当需要根据前缀的出现拆分字符串时,会定义两个空列表,并定义一个前缀值。会使用简单的迭代以及“append”方法。示例下面是同一个的演示 -from itertools import zip_longest my_list = ["hi", 'hello', 'there', "python", "object", "oriented", "object", "cool", "language", 'py', 'extension', 'bjarne'] print("The list is : " ) print(my_list) my_prefix = "python" print("The prefix is :") print(my_prefix) my_result, my_temp_val = [], [] for x, y in zip_longest(my_list, my_list[1:]): my_temp_val.append(x) if y and y.startswith(my_prefix): ... 阅读更多

Python – 从字符串中提取百分比

AmitDiwan
更新于 2021-09-13 11:27:44

345 次查看

当需要从字符串中提取百分比时,会使用正则表达式和包的“findall”方法。示例下面是同一个的演示 -import re my_string = 'Python is % always fun % to learn % and teach' print("The list is : " ) print(my_string) my_result = re.findall('\d*%', my_string) print("The resultant list is : ") print(my_result)输出The list is : Python is % always fun % to learn % and teach The resultant list is : ['%', '%', '%']解释将所需的包导入环境。定义一个字符串并将其... 阅读更多

Python – 从给定的元组列表中过滤所有大写字符

AmitDiwan
更新于 2021-09-13 11:26:19

544 次查看

当需要从元组列表中过滤所有大写字符时,会使用简单的迭代、布尔值、“append”方法和“isupper”方法。示例下面是同一个的演示 -my_list = [("PYTHON", "IS", "Fun"), ("PYTHON", "COOl"), ("PYTHON", ), "ORIENTED", "OBJECT"] print("The list is : " ) print(my_list) my_result_list = [] for sub_list in my_list:    my_result = True    for element in sub_list:       if not element.isupper():          my_result = False          break    if my_result:       my_result_list.append(sub_list) ... 阅读更多

Python 程序用于删除列表中索引处的元素

AmitDiwan
更新于 2021-09-13 11:24:11

165 次查看

当需要删除列表中索引处的元素时,会使用“enumerate”属性、“not in”运算符、简单的迭代和“append”方法。示例下面是同一个的演示 -my_list = [91, 75, 15, 45, 69, 78, 23, 71, 36, 72] print("The list is : " ) print(my_list) print("The list after sorting is : " ) my_list.sort() print(my_list) index_list = [2, 4, 5, 7] print("The index values stored in the list are :") print(index_list) my_result = [] for index, element in enumerate(my_list): if index not in index_list: ... 阅读更多

广告
© . All rights reserved.