dictionary

2-Python基础语法-内存管理-运算符-程序控制

為{幸葍}努か 提交于 2021-02-19 05:52:22
[toc] 1 Python 基础语法 所有语言都有它自己规定的格式,Python也不例外,下面从几个方面来了解。 1.1 注释 在Python中主要的使用的注释方式是#号,当然在某些情况下三对单引号也是可以的,比如在函数中可以作为函数的说明。 num =int(input('>>>: ')) # 输入一个数字 if num >= 1000: # 条件判断 if num >= 10000: # 大于10000 print('5') # 打印5位 else: '''否则''' print('4') '''打印4''' else: if num >= 100: print('3') elif num >= 10: print('2') else: print('1') 1.2 缩进 C语言使用的是花括号来表示层级关系,但是看起来不简洁, 挺难看 ,而Python使用的是缩进的方式表示层级关系,并且约定 4个空格 为一个层级。 1.3 续行 当我们一行的代码超过了一屏显示的长度以后,多数情况下为了直观,我们会另起一行,接着些,在Python中在行尾使用 \ 标识续行,但如果在表达式或者某一段代码中使用了括号,那么Python认为括号内是一个整体,内部跨行不需要使用 \ 。 1.4 标识符 很多时候,我们写代码的过程中会对我们需要的数据进行存储并命名,方便后续使用,这个命名被称为标识符

Map to 2d array with Streams

十年热恋 提交于 2021-02-19 04:17:26
问题 I am trying to create a 2d array of String using Streams: String[] fruit1DArray; String[][] fruit2DArray; Map<String, String> fruitMap = new HashMap<>(); fruitMap.put("apple", "red"); fruitMap.put("pear", "green"); fruitMap.put("orange", "orange"); fruit1DArray = fruitMap.entrySet() .stream() .map(key -> key.getKey()) .toArray(size -> new String[size]); fruit2DArray = fruitMap.entrySet() .stream() .map(entry-> new String[]{entry.getKey()}) .toArray(size -> new String[size][1]); System.out

Bind to arbitrary Dictionary<,> by using a converter to cast object

烈酒焚心 提交于 2021-02-19 04:15:27
问题 I'm trying to workaround the difficulties in binding to a Dictionary in WinRT Xaml (also referenced here). I want to use a converter to do this rather than having to change all of my view-models or business code to return a List of a custom Key Value class. This means I need to cast an object to a List<> of some type. public object Convert(object value, Type targetType, object parameter, string temp) { if(value is IDictionary) { dynamic v = value; foreach (dynamic kvp in v) { } } return /

Map to 2d array with Streams

半腔热情 提交于 2021-02-19 04:10:46
问题 I am trying to create a 2d array of String using Streams: String[] fruit1DArray; String[][] fruit2DArray; Map<String, String> fruitMap = new HashMap<>(); fruitMap.put("apple", "red"); fruitMap.put("pear", "green"); fruitMap.put("orange", "orange"); fruit1DArray = fruitMap.entrySet() .stream() .map(key -> key.getKey()) .toArray(size -> new String[size]); fruit2DArray = fruitMap.entrySet() .stream() .map(entry-> new String[]{entry.getKey()}) .toArray(size -> new String[size][1]); System.out

Bind to arbitrary Dictionary<,> by using a converter to cast object

只愿长相守 提交于 2021-02-19 04:07:15
问题 I'm trying to workaround the difficulties in binding to a Dictionary in WinRT Xaml (also referenced here). I want to use a converter to do this rather than having to change all of my view-models or business code to return a List of a custom Key Value class. This means I need to cast an object to a List<> of some type. public object Convert(object value, Type targetType, object parameter, string temp) { if(value is IDictionary) { dynamic v = value; foreach (dynamic kvp in v) { } } return /

Bind to arbitrary Dictionary<,> by using a converter to cast object

两盒软妹~` 提交于 2021-02-19 04:06:00
问题 I'm trying to workaround the difficulties in binding to a Dictionary in WinRT Xaml (also referenced here). I want to use a converter to do this rather than having to change all of my view-models or business code to return a List of a custom Key Value class. This means I need to cast an object to a List<> of some type. public object Convert(object value, Type targetType, object parameter, string temp) { if(value is IDictionary) { dynamic v = value; foreach (dynamic kvp in v) { } } return /

Show the dictionary key in django template

江枫思渺然 提交于 2021-02-19 02:06:36
问题 Im wondering how i could show the dictionary key itself in a django template Example dictionary: resources = {'coin': coin, 'grain': grain, 'iron': iron, 'stone': stone, 'wood': wood,} Template <b>Coin: </b>{{ upgrade.coin }} Were i want to use the dictionary key (+some html) instead of the hard coded "Coin:" Can anyone please help me out? 回答1: Use for tag with dict.items if you want to print all key/value pairs: {% for key, value in resources.items %} <b>{{ key }}: </b>{{ value }} {% endfor

Generating a good hash code (GetHashCode) for a BitArray

非 Y 不嫁゛ 提交于 2021-02-19 02:02:32
问题 I need to generate a fast hash code in GetHashCode for a BitArray. I have a Dictionary where the keys are BitArrays, and all the BitArrays are of the same length. Does anyone know of a fast way to generate a good hash from a variable number of bits, as in this scenario? UPDATE: The approach I originally took was to access the internal array of ints directly through reflection (speed is more important than encapsulation in this case), then XOR those values. The XOR approach seems to work well

appending data to python dictionary

两盒软妹~` 提交于 2021-02-18 22:49:36
问题 I have used the following code to initialize a dictionary from the list of keys z=df1[2].value_counts().keys().tolist() mydict=dict.fromkeys(z,None) further, I have used value=df2[2].value_counts().keys().tolist() counts=df2[2].value_counts().tolist() for j,items in value: if mydict.has_key(items): mydict.setdefault(items,[]).append(counts[j]) it is generating the following error mydict.setdefault(items,[]).append(counts[j]) AttributeError: 'NoneType' object has no attribute 'append' 回答1:

Obtaining length of list as a value in dictionary in Python 2.7

倖福魔咒の 提交于 2021-02-18 22:28:09
问题 I have two lists and dictionary as follows: >>> var1=[1,2,3,4] >>> var2=[5,6,7] >>> dict={1:var1,2:var2} I want to find the size of the mutable element from my dictionary i.e. the length of the value for a key. After looking up the help('dict') , I could only find the function to return number of keys i.e. dict.__len__() . I tried the Java method(hoping that it could work) i.e. len(dict.items()[0]) but it evaluated to 2 . I intend to find this: Length of value for first key: 4 Length of value