dictionary

Java面试题之Java基础

≡放荡痞女 提交于 2021-01-23 08:21:37
1 、作用域 public,private,protected, 以及不写时的区别 答: 区别如下: 作用域 当前类 同一package 子孙类 其他package public √ √ √ √ protected √ √ √ × friendly √ √ × × private √ × × × 不写时默认为friendly 2 、 Anonymous Inner Class ( 匿名内部类 ) 是否可以 extends( 继承 ) 其它类,是否可以 implements( 实现 )interface( 接口 ) 答: 匿名的内部类是没有名字的内部类。不能extends(继承) 其它类,但一个内部类可以作为一个接口,由另一个内部类实现。 3 、 Static Nested Class 和 Inner Class 的不同 答: Nested Class (一般是C++的说法),Inner Class (一般是JAVA的说法)。Java内部类与C++嵌套类最大的不同就在于是否有指向外部的引用上。注: 静态内部类(Inner Class)意味着1创建一个static内部类的对象,不需要一个外部类对象,2不能从一个static内部类的一个对象访问一个外部类对象 4 、 & 和 && 的区别 答: &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and) 5 、

How to add multiple values per key in python dictionary

旧巷老猫 提交于 2021-01-23 00:33:19
问题 My program needs to output a list of names with three numbers corresponding to each name however I don't know how to code this is there a way I could do it as a dictionary such as cat1 = {"james":6, "bob":3} but with three values for each key? 回答1: The value for each key can either be a set (distinct list of unordered elements) cat1 = {"james":{1,2,3}, "bob":{3,4,5}} for x in cat1['james']: print x or a list (ordered sequence of elements ) cat1 = {"james":[1,2,3], "bob":[3,4,5]} for x in cat1

How to add multiple values per key in python dictionary

荒凉一梦 提交于 2021-01-23 00:31:19
问题 My program needs to output a list of names with three numbers corresponding to each name however I don't know how to code this is there a way I could do it as a dictionary such as cat1 = {"james":6, "bob":3} but with three values for each key? 回答1: The value for each key can either be a set (distinct list of unordered elements) cat1 = {"james":{1,2,3}, "bob":{3,4,5}} for x in cat1['james']: print x or a list (ordered sequence of elements ) cat1 = {"james":[1,2,3], "bob":[3,4,5]} for x in cat1

Python extract max value from nested dictionary

僤鯓⒐⒋嵵緔 提交于 2021-01-22 06:32:22
问题 I have a nested dictionary of the form: {'2015-01-01': {'time': '8', 'capacity': '5'}, '2015-01-02': {'time': '8', 'capacity': '7'}, '2015-01-03': {'time': '8', 'capacity': '8'} etc} The dictionary is created from a csv file using dictreader. What I would like to be able to do is return the maximum value of capacity. So in this case 8. I can use: for k,v in input_dict.items(): if temp_max < int(v['capacity']): temp_max = int(v['capacity']) which works but I wondered if there was a neater

python语法

只愿长相守 提交于 2021-01-22 04:54:32
python语法 python语法优缺点 编辑方法 注释 变量 输出 输入 延时打印 运算符 常用数据类型转换 if 判断语句 循环 dict和set 函数 递归函数 高级特性 python语法优缺点 1、优点 简单 易学 免费、开源 可移植性 可扩展性 2、缺点 运行速度 国内市场较小 中文资料匮乏 构架选择太多 3、使用场景 web应用开发 操作系统管理、服务器运维的自动化脚本 科学计算 桌面应用程序 服务器软件(爬虫等) 游戏(逻辑、服务器) 构思实现,产品早期原型 编辑方法 一、python第一种编辑方法 1、打开终端,输入 python,进入python环境,输入exit(),退出python (默认进入python 2交互程序) 2、输入python3,进入python 3交互程序 二、python第二种编辑方式 1、先通过命令行查找python2或python3的地址,记下来 2、通过vi编辑器编写程序,并在首行加上指定的解释器,即 (Windows系统会忽略这个注释) 3、查看新建文件是否有可执行权限,若没有,就添加 ls -l chmod u+x test.py 4、以上设置完成后,输入 ./test.py 即可执行程序 三、python第三种编辑方式 1、在试用本地文本编辑器编写程序并保存 2、在终端直接通过python 文件名 执行 注:推荐使用 python

Python, store a dict in a database

ぃ、小莉子 提交于 2021-01-20 18:12:57
问题 What's the best way to store and retrieve a python dict in a database? 回答1: If you are not specifically interested into using a traditionally SQL database, such as MySQL, you could look into unstructured document databases where documents naturally map to python dictionaries, for example MongoDB. The MongoDB python bindings allow you to just insert dicts in the DB, and query them based on the values in the dict. See for example the code below from the tutorial: >>> from pymongo import

Python, store a dict in a database

吃可爱长大的小学妹 提交于 2021-01-20 18:12:13
问题 What's the best way to store and retrieve a python dict in a database? 回答1: If you are not specifically interested into using a traditionally SQL database, such as MySQL, you could look into unstructured document databases where documents naturally map to python dictionaries, for example MongoDB. The MongoDB python bindings allow you to just insert dicts in the DB, and query them based on the values in the dict. See for example the code below from the tutorial: >>> from pymongo import

Python, store a dict in a database

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-20 18:12:06
问题 What's the best way to store and retrieve a python dict in a database? 回答1: If you are not specifically interested into using a traditionally SQL database, such as MySQL, you could look into unstructured document databases where documents naturally map to python dictionaries, for example MongoDB. The MongoDB python bindings allow you to just insert dicts in the DB, and query them based on the values in the dict. See for example the code below from the tutorial: >>> from pymongo import

Python, store a dict in a database

ぃ、小莉子 提交于 2021-01-20 18:11:24
问题 What's the best way to store and retrieve a python dict in a database? 回答1: If you are not specifically interested into using a traditionally SQL database, such as MySQL, you could look into unstructured document databases where documents naturally map to python dictionaries, for example MongoDB. The MongoDB python bindings allow you to just insert dicts in the DB, and query them based on the values in the dict. See for example the code below from the tutorial: >>> from pymongo import

Python: create dictionary using dict() with integer keys?

北城以北 提交于 2021-01-20 16:16:08
问题 In Python, I see people creating dictionaries like this: d = dict( one = 1, two = 2, three = 3 ) What if my keys are integers? When I try this: d = dict (1 = 1, 2 = 2, 3 = 3 ) I get an error. Of course I could do this: d = { 1:1, 2:2, 3:3 } which works fine, but my main question is this: is there a way to set integer keys using the dict() function/constructor? 回答1: Yes, but not with that version of the constructor. You can do this: >>> dict([(1, 2), (3, 4)]) {1: 2, 3: 4} There are several