随笔分类 - Python学习
摘要:我有一个字典如下所示: stats = {'a': 1, 'b': 3000, 'c': 0} key是字符串, value是int值, 我想找到最大值对应的key, 在上例中答案是 ‘b’ 回答: max(stats, key=stats.get) 来源: https://stackoverflo
阅读全文
摘要:原始数据为: >>> a # I have array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 0]]) 需要变成的数据为: >>> new_a
阅读全文
摘要:In[38]: df.groupby(['name','month'])['text'].apply(','.join).reset_index() Out[38]: name month text 0 name1 11 du 1 name1 12 aj,oj 2 name2 11 fin,katt
阅读全文
摘要:解决方案: 可以使用包, itertools.chain() import itertools list2d = [[1,2,3], [4,5,6], [7], [8,9]] merged = list(itertools.chain(*list2d)) 也可以不使用解包的方法, 使用itertoo
阅读全文
摘要:解决方式: 问题出现在MAC OS系统中, 解决方法是将当前文件夹的所有者设为用户本人. sudo chown -R $(whoami) . 原文链接: https://stackoverflow.com/questions/25108496/pycharm-community-edition-pr
阅读全文
摘要:ERROR Invalid control character 的解决方式
阅读全文
摘要:不要用反斜杠连接导入包 Python会将圆括号, 中括号和花括号中的行隐式的连接起来 长URL放在统一行也不要用反斜杠连接 不要在逗号, 分号, 冒号前面加空格, 但应该在它们后面加(除了在行尾). 在二元操作符两边都加上一个空格, 比如赋值(=), 比较(==, <, >, !=, <>, ⇐,
阅读全文
摘要:报错的意思是说,没有对应的路径的文件夹。 虽然能够生成文件,但是必须要有生成的文件夹。 常用的方法是先判断是否文件夹存在,如果不存在,则先创建对应的文件夹。 import os outname = 'name.csv' outdir = './dir' if not os.path.exists(o
阅读全文
摘要:使用sonar在本地对代码进行扫描主要分为两部分: 1、sonarqube软件是生成UI界面控制台,来显示最终的跑出来的检测结果 2、sonar-scanner是检测工具,将代码拷贝到制定区域进行检测 具体的操作步骤: 1、安装sonarqube服务器软件 1)下载社区版地址:https://www
阅读全文
摘要:原文:https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os import struct print(str
阅读全文
摘要:import glob file_list = [] for ext in ('*.pdf', '*.PDF'): res_paths = os.path.join(directory, ext) file_list.extend(glob.glob(res_paths)) for file in
阅读全文
摘要:来源 https://leetcode cn.com/problems/remove nth node from end of list/ 题目描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 示例: 给定一个链表: 1 2 3 4 5, 和 n = 2. 当删除了倒数第二个
阅读全文
摘要:来源 https://leetcode cn.com/problems/binary tree inorder traversal/ 题目描述 给定一个二叉树,返回它的中序 遍历。 示例: 输入: [1,null,2,3] 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
阅读全文
摘要:来源 https://leetcode cn.com/problems/n ary tree preorder traversal/ 题目描述 给定一个 N 叉树,返回其节点值的前序遍历。 例如,给定一个 3叉树 : 返回其前序遍历: [1,3,5,6,2,4]。 说明: 递归法很简单,你可以使用迭
阅读全文
摘要:来源 https://leetcode cn.com/problems/n ary tree level order traversal/ 题目描述 给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4],
阅读全文
摘要:来源 https://leetcode-cn.com/problems/longest-common-prefix/ 题目描述 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。 示例 1: 输入: ["flower","flow","flight"] 输出:
阅读全文
摘要:来源 https://leetcode cn.com/problems/longest substring without repeating characters/ 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解
阅读全文
摘要:来源 https://leetcode cn.com/problems/add two numbers/ 题目描述 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链
阅读全文
摘要:原文来源:https://stackoverflow.com/questions/2136556/in python how do i split a string and keep the separators 问: 下面是最简单的解释:我是这么用的 但是我想要的是下面这样的 因为我想将一个字符串
阅读全文
摘要:原文来源:https://stackoverflow.com/questions/2136556/in python how do i split a string and keep the separators 这是解释这个问题的最简单方法。这是我正在使用的: 这就是我想要的: 原因是我想将一个字
阅读全文