找到 10786 篇文章 关于 Python
655 次查看
当需要显示包含所有列表元素的行时,可以使用一个标志值、简单的迭代和 `append` 方法。示例如下所示:`my_list = [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] print("The list is :") print(my_list) sub_list = [1, 2] result = [] for row in my_list: flag = True for element in sub_list: if element not in row: flag = False ... 阅读更多
144 次查看
当需要过滤不包含空格字符串的行时,可以使用列表推导式、正则表达式、`not` 运算符和 `any` 方法。示例如下所示:`import re my_list = [["python is", "fun"], ["python", "good"], ["python is cool"], ["love", "python"]] print("The list is :") print(my_list) my_result = [row for row in my_list if not any(bool(re.search(r"\s", element)) for element in row)] print("The resultant list is :") print(my_result)输出The list is : [[‘python is’, ‘fun’], [‘python’, ‘good’], [‘python is cool’], [‘love’, ‘python’]] The resultant list is : [[‘python’, ‘good’], [‘love’, ‘python’]]解释需要导入必要的包…… 阅读更多
163 次查看
当需要查找等距连续字符字符串时,可以使用列表推导式、`all` 运算符和 `ord` 方法。示例如下所示:`my_list = ["abc", "egfg", "mpsv", "abed", 'xzbd', 'agms'] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(ord(sub[index + 1]) - ord(sub[index]) == ord(sub[1]) - ord(sub[0]) for index in range(0, len(sub) - 1))] print("The resultant list is :") print(my_result)输出The list is : ['abc', 'egfg', 'mpsv', 'abed', 'xzbd', 'agms'] The resultant list is : ['abc', 'mpsv', 'agms']解释定义一个字符串值列表并显示…… 阅读更多
110 次查看
当需要重构 K 位数元素时,可以使用列表推导式和 `append` 方法。示例如下所示:`my_list = [231, 67, 232, 1, 238, 31, 793] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) temp = ''.join([str(ele) for ele in my_list]) my_result = [] for index in range(0, len(temp), K): my_result.append(int(temp[index: index + K])) print("The resultant list is :") print(my_result)输出The list is : [231, 67, 232, 1, 238, 31, 793] The value of K is 3 The resultant…… 阅读更多
216 次查看
当需要检查列表中所有“y”是否出现在“x”之后时,可以使用 `enumerate` 属性以及特定条件。示例如下所示:`my_list = [11, 25, 13, 11, 64, 25, 8, 9] print("The list is :") print(my_list) x, y = 13, 8 x_index = my_list.index(x) my_result = True for index, element in enumerate(my_list): if element == y and index < x_index: my_result = False break if(my_result == True):…… 阅读更多
173 次查看
当需要测试所有行是否与其他矩阵包含任何公共元素时,可以使用简单的迭代和标志值。示例如下所示:`my_list_1 = [[3, 16, 1], [2, 4], [4, 31, 31]] my_list_2 = [[42, 16, 12], [42, 8, 12], [31, 7, 10]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = True for idx in range(0, len(my_list_1)): temp = False for element in my_list_1[idx]: if element in my_list_2[idx]: temp = True …… 阅读更多
101 次查看
当需要对连续元素重新排序时,可以使用 `Counter` 方法、空列表和简单的迭代。示例如下所示:`from collections import Counter my_list = [21, 83, 44, 52, 61, 72, 81, 96, 18, 44] print("The list is :") print(my_list) my_frequencys = Counter(my_list) my_result = [] for value, count in my_frequencys.items(): my_result.extend([value]*count) print("The resultant list is :") print(my_result)输出The list is : [21, 83, 44, 52, 61, 72, 81, 96, 18, 44] The resultant list is : [21, 83, 44, 44, 52, 61, 72, 81, 96, 18]解释…… 阅读更多
86 次查看
当需要删除列表中“x”之前的所有“y”出现时,可以使用列表推导式和 `index` 方法。示例如下所示:`my_list = [4, 45, 75, 46, 66, 77, 48, 99, 10, 40, 5, 8] print("The list is :") print(my_list) a, b = 8, 4 index_a = my_list.index(a) my_result = [ele for index, ele in enumerate(my_list) if ele != b or (ele == b and index > index_a) ] print("The resultant list is ") print(my_result)输出The list is : [4, 45, 75, 46, 66, 77, 48, 99, 10,…… 阅读更多
111 次查看
当需要打印矩阵中特定长度的行时,可以使用列表推导式。示例如下所示:`my_list = [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] print("The list is :") print(my_list) my_key = 4 my_result = [sub for sub in my_list if len(sub) == my_key] print("The resultant list is :") print(my_result)输出The list is : [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] The resultant list is : [[22, 4, 63, 7]]解释一个…… 阅读更多
162 次查看
当需要打印具有最大和的特定数量的行时,可以使用 `sorted` 方法和 `lambda` 方法。示例如下所示:`my_list = [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] print("The list is :") print(my_list) my_key = 3 print("The key is") print(my_key) my_result = sorted(my_list, key=lambda row: sum(row), reverse=True)[:my_key] print("The resultant list is :") print(my_result)输出The list is : [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] The key is 3 The resultant list is…… 阅读更多
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP