找到 34423 篇文章,关于编程
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': ... 阅读更多
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 包创建一个矩阵。它显示在控制台上。矩阵的均值是 ... 阅读更多
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 ... 阅读更多
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 = ... 阅读更多
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 ... 阅读更多
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 + ... 阅读更多
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): ... 阅读更多
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 : ['%', '%', '%']解释将所需的包导入环境。定义一个字符串并... 阅读更多
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) ... 阅读更多
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: ... 阅读更多
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP