找到 10786 篇文章 关于 Python

Python – 从字符串列表中获取除指定字母外的所有元素

AmitDiwan
更新于 2021年9月4日 11:16:20

357 次浏览

当需要从字符串列表中获取除指定字母外的所有元素时,可以使用列表推导式和 `append` 方法。以下是演示:示例 在线演示`my_list = ["hi", "is", "great", "pyn", "pyt"]` `print("The list is :")` `print(my_list)` `my_key = 'n'` `print("The value for key is ")` `print(my_key)` `my_result = []` `for sub in my_list:`    `my_result.append(''.join([element for element in sub if element == my_key]))` `print("The result is :")` `print(my_result)`输出The list is : ['hi', 'is', 'great', 'pyn', 'pyt'] The value for key is n The result is ... 阅读更多

Python - 打印矩阵中在给定索引处具有相同元素的行

AmitDiwan
更新于 2021年9月4日 11:15:06

247 次浏览

当需要打印矩阵中在给定索引处具有相同元素的行时,可以使用列表推导式和 `all` 运算符。以下是演示:示例 在线演示`my_list = [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], [98, 74, 27]]` `print("The list is :")` `print(my_list)` `my_key = 1` `print("The key is ")` `print(my_key)` `my_result = [element for element in my_list if all(str(i)[my_key] == str(element[0])[my_key] for i in element)]` `print("The result is :")` `print(my_result)`输出The list is : [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, ... 阅读更多

Python 程序 – 打印列表中峰值或谷值的计数

AmitDiwan
更新于 2021年9月4日 11:13:43

250 次浏览

当需要打印列表中峰值或谷值的计数时,可以使用简单的迭代和特定条件。以下是演示:示例 在线演示`my_list = [11, 12, 24, 12, 36, 17, 28, 63]` `print("The list is :")` `print(my_list)` `my_result = 0` `for index in range(1, len(my_list) - 1):`    `if my_list[index + 1] > my_list[index] < my_list[index - 1] or my_list[index + 1] <`    `my_list[index] > my_list[index - 1]:`       `my_result += 1` `print("The result is :")` `print(my_result)`输出The list is : [11, 12, 24, ... 阅读更多

Python – 列表中的连续除法

AmitDiwan
更新于 2021年9月4日 11:11:48

159 次浏览

当需要查找列表中的连续除法时,可以定义一个方法,该方法迭代列表的元素并使用 `/` 运算符确定结果。以下是演示:示例 在线演示`def consec_division(my_list):`    `my_result = my_list[0]`    `for idx in range(1, len(my_list)):`       `my_result /= my_list[idx]`    `return my_result` `my_list = [2200, 500, 100, 50, 20, 5]` `print("The list is :")` `print(my_list)` `my_result = consec_division(my_list)` `print("The result is :")` `print(my_result)`输出The list is : [2200, 500, 100, 50, 20, 5] The result is ... 阅读更多

Python – 从矩阵中过滤表示字典键的不可变行

AmitDiwan
更新于 2021年9月4日 11:10:11

106 次浏览

当需要从矩阵中过滤表示字典键的不可变行时,可以使用列表推导式和 `isinstance` 方法。以下是演示:示例 在线演示`my_list = [[24, 15, [32, 33, 12]], ["pyt", 8, (14, 54)], [{15:24}, 13, "fun"], [True, "cool"]]` `print("The list is :")` `print(my_list)` `my_result = [row for row in my_list if all(isinstance(element, int) or isinstance(element, bool) or isinstance(element, float) or isinstance(element, tuple) or isinstance(element, str) for element in row)]` `print("The result is :")` `print(my_result)`输出The list is : [[24, 15, [32, 33, 12]], ['pyt', 8, (14, 54)], [{15: 24}, ... 阅读更多

Python – 提取特定数据类型行

AmitDiwan
更新于 2021年9月4日 11:09:15

300 次浏览

当需要提取特定数据类型行时,可以使用列表推导式、`isinstance` 方法和 `all` 运算符。以下是演示:示例 在线演示`my_list = [[14, 35, "Will"], [12, 26, 17], ["p", "y", "t"], [29, 40, 21]]` `print("The list is :")` `print(my_list)` `my_data_type = int` `my_result = [row for row in my_list if all(isinstance(element, my_data_type) for element in row)]` `print("The result is :")` `print(my_result)`输出The list is : [[14, 35, 'Will'], [12, 26, 17], ['p', 'y', 't'], [29, 40, 21]] The result is : [[12, 26, 17], [29, 40, 21]]解释一个列表 ... 阅读更多

Python – 按符号分组连续元素

AmitDiwan
更新于 2021年9月4日 11:07:44

235 次浏览

当需要按符号分组连续元素时,可以使用 `^` 运算符和简单的迭代以及 `enumerate`。以下是演示:示例 在线演示`my_list = [15, -33, 12, 64, 36, -12, -31, -17, -49, 12, 43, 30, -23, -35, 53]` `print("The list is :")` `print(my_list)` `my_result = [[]]` `for (index, element) in enumerate(my_list):`    `if element ^ my_list[index - 1] < 0:`       `my_result.append([element])`    `else:`       `my_result[-1].append(element)` `print("The result is :")` `print(my_result)`输出The list is : [15, -33, 12, 64, 36, -12, -31, -17, -49, 12, ... 阅读更多

Python – K 个中间元素

AmitDiwan
更新于 2021年9月4日 11:02:24

863 次浏览

当需要确定 K 个中间元素时,可以使用 `//` 运算符和列表切片。以下是演示:示例 在线演示`my_list = [34, 56, 12, 67, 88, 99, 0, 1, 21, 11]` `print("The list is : ")` `print(my_list)` `K = 5` `print("The value of K is ")` `print(K)` `beg_indx = (len(my_list) // 2) - (K // 2)` `end_indx = (len(my_list) // 2) + (K // 2)` `my_result = my_list[beg_indx: end_indx + 1]` `print("The result is : " )` `print(my_result)`输出The list is : [34, 56, 12, 67, 88, 99, 0, 1, 21, ... 阅读更多

Python – 过滤排序行

AmitDiwan
更新于 2021年9月4日 11:01:30

342 次浏览

当需要过滤排序行时,可以使用列表推导式以及 `sorted` 和 `list` 方法。以下是演示:示例 在线演示`my_list = [[99, 6, 75, 10], [1, 75, 2, 4, 99], [75, 15, 99, 2], [1, 4, 15, 99]]` `print("The list is :")` `print(my_list)` `my_result = [sub for sub in my_list if sub == list(sorted(sub)) or sub == list(sorted(sub, reverse=True))]` `print("The resultant list is :")` `print(my_result)` 输出The list is : [[99, 6, 75, 10], [1, 75, 2, 4, 99], [75, 15, 99, 2], [1, 4, 15, 99]] The resultant list ... 阅读更多

Python – 按 K 的倍数排序行

AmitDiwan
更新于 2021年9月4日 11:00:24

113 次浏览

当需要按 K 的倍数排序行时,可以定义一个方法,该方法使用列表推导式和模运算符。以下是演示:示例 在线演示`def multiple_sort_val(row):`    `return len([ele for ele in row if ele % K == 0])` `my_list = [[11, 44, 7, 11], [7, 5, 44, 11], [11, 6, 35, 44], [92, 92, 5]]` `print("The list is :")` `print(my_list)` `K = 11` `print("The value for K is ")` `print(K)` `my_list.sort(key=multiple_sort_val)` `print("The resultant list is :")` `print(my_list)`输出The list is : [[11, 44, 7, 11], [7, 5, 44, ... 阅读更多

广告
© . All rights reserved.