这篇文章主要围绕使用Python,我可以保留一个持久字典并对其进行修改吗?和python保留字可以做变量名吗展开,旨在为您提供一份详细的参考资料。我们将全面介绍使用Python,我可以保留一个持久字典
这篇文章主要围绕使用Python,我可以保留一个持久字典并对其进行修改吗?和python保留字可以做变量名吗展开,旨在为您提供一份详细的参考资料。我们将全面介绍使用Python,我可以保留一个持久字典并对其进行修改吗?的优缺点,解答python保留字可以做变量名吗的相关问题,同时也会为您带来c – 子集一个向量并对其进行排序、c – 我可以轻松地创建一个模板函数,它接受任意类型的任意容器并对其进行操作吗?、Golang将数组传递给函数并对其进行修改、Python – 在字典列表中查找重复项并对其进行分组的实用方法。
本文目录一览:- 使用Python,我可以保留一个持久字典并对其进行修改吗?(python保留字可以做变量名吗)
- c – 子集一个向量并对其进行排序
- c – 我可以轻松地创建一个模板函数,它接受任意类型的任意容器并对其进行操作吗?
- Golang将数组传递给函数并对其进行修改
- Python – 在字典列表中查找重复项并对其进行分组
使用Python,我可以保留一个持久字典并对其进行修改吗?(python保留字可以做变量名吗)
因此,我想将字典存储在持久文件中。是否可以使用常规词典方法在该文件中的词典中添加,打印或删除条目?
看来我可以使用cPickle来存储字典并加载它,但是我不确定从哪里获取它。
c – 子集一个向量并对其进行排序
无论如何,我已经陷入了一个简单的问题,如果我可以修复会帮助很多.我有一个NumericVector我想要子集,然后排序.下面的代码排序整个向量(并且还将处理NAs,这是我需要的).
我的问题是,我想提取这个向量的一部分,排序并将其用于其他处理 – 我该怎么做?例如,对于长度为10的向量,我如何提取和排序元素5:10?
#include <Rcpp.h>using namespace Rcpp;// [[Rcpp::export]]RcppExport SEXP rollP(SEXP x) { NumericVector A(x); // the data A = sort_unique(A); return A;}
我叫R:
sourceCpp( "rollP.cpp")rollP(10:1)# [1] 1 2 3 4 5 6 7 8 9 10
解决方法
include <Rcpp.h>using namespace Rcpp;// [[Rcpp::export]]NumericVector rollP(NumericVector A,int start,int end) { NumericVector B(end-start+1) ; std::copy( A.begin() + start-1,A.begin() + end,B.begin() ) ; return B.sort() ;}// [[Rcpp::export]]NumericVector rollP2(NumericVector A,int end) { NumericVector B( A.begin() + start-1,A.begin() + end ) ; return B.sort() ;}// [[Rcpp::export]]NumericVector rollP3(NumericVector A,int end) { NumericVector B = A[seq(start-1,end-1)] ; return B.sort() ;}
开始和结束是指基于1的指数,就好像你从R通过A [开始:结束].
c – 我可以轻松地创建一个模板函数,它接受任意类型的任意容器并对其进行操作吗?
// This method is wrong,won't work,need your help template < template <typename T> class U > void foo(U& u) { T& blah = *u.begin(); } int main(int,char**) { vector<int> myVec(4,10); foo<vector<int> >(myVec); // This is how I want to call it,even better if I can leave the parameters out and just do foo(myVec); return EXIT_SUCCESS; }
真的我想要做的是避免以下因为它似乎多余:
template <typename T,typename U> void foo(U& u) { T& blah = *u.begin(); } int main(int,10); foo<int,std::vector<int> >(myVec); // first int in parameters is redundant cause I already provide int as arg to vector return EXIT_SUCCESS; }
解决方法
template < typename U> void foo(U& u) { typename U::value_type blah = *u.begin(); }
Golang将数组传递给函数并对其进行修改
如何解决Golang将数组传递给函数并对其进行修改?
解释很简单: 在上面的代码中 没有
声明或明确使用单个数组。您的tab
局部变量和tab
参数是slices 。
在Go语言中,数组的长度是该类型的一部分,例如[3]int
(在一定程度上是正确的,[2]int
并且[3]int
是2种不同/不同的数组类型)。如果长度不存在([2]int
在复合文字中
像显式一样或隐式一样[...]int{1, 2, 3}
),则该长度不是数组类型,而是切片类型。
是的,当您阅读时,数组值表示其所有元素,并且在传递(或分配)时会复制其所有元素。切片不过是小的描述符,标头,描述了数组的连续部分。当切片被传递(或分配)时,仅复制此标头(包括指针),该标头指向相同的基础数组。因此,如果您修改切片副本的元素,则更改将反映在原始切片中,因为只有一个支持该元素的后备阵列。
如果您想确切地了解切片头中的内容,则可以检查reflect.SliceHeader
类型:它struct
包含指向切片第一个元素的指针,切片的长度和容量。
切成薄片:用法和内部原理
数组,切片(和字符串):“追加”的机制
解决方法
在大多数语言(如c ++)中,传递数组会导致通过引用隐式传递它,因此对函数中传递的数组进行的任何更改都将导致更改原始数组。我正在学习Golang,在Alan
AA Donovan和Brian W. Kernighan撰写的《 Go编程语言》一书中,它的行为不同于其他语言-不会隐式地通过引用传递数组。
这让我有些困惑-这是否意味着在没有引用的情况下传递数组不应该修改数组本身?让我说明一下:
func main() {
tab := []int{1,2,3}
fmt.Println(tab)
// Results in [1 2 3]
reverse(tab)
fmt.Println(tab)
// Results in [3 2 1]
}
func reverse(tab []int) {
for i,j := 0,len(tab)-1; i < j; i,j = i+1,j-1 {
tab[i],tab[j] = tab[j],tab[i]
}
}
在上面的代码中,数组不是由引用传递的,但是反向函数会修改原始数组,因此它的工作方式有点像C ++程序那样。谁能解释我的区别?
PS:很抱歉,如果这是一个假的问题,我对Golang完全陌生,并且试图很好地理解基本知识。
Python – 在字典列表中查找重复项并对其进行分组
# JSON file (film.json) [{"year": ["1999"],"director": ["Wachowski"],"film": ["The Matrix"],"price": ["19,00"]},{"year": ["1994"],"director": ["Tarantino"],"film": ["Pulp Fiction"],"price": ["20,{"year": ["2003"],"film": ["Kill Bill vol.1"],"price": ["10,"film": ["The Matrix Reloaded"],"price": ["9,99"]},"film": ["Pulp Fyction"],"price": ["15,"director": ["E. de Souza"],"film": ["Street fighter"],"price": ["2,{"year": ["1999"],{"year": ["1982"],"director": ["Ridley Scott"],"film": ["Blade Runner"],99"]}]
我可以导入json文件:
import json json_file = open('film.json') f = json.load(json_file)
但在那之后,我无法在f中找到事件,并按电影片名分组.
这就是我想要实现的目标:
## result grouped by 'film' #group 1 {"year": ["1999"],00"]} {"year": ["1999"],00"]} #group 2 {"year": ["1994"],00"]} {"year": ["1994"],00"]} #group X ...
或更好:
new_dict = { 'group1':[[],[],...],'group2':[[],'groupX':[...] }
目前我正在测试嵌套,但没有运气..
谢谢.
注意:“纸浆fyction”是未来实现的模糊字符串匹配的错误,现在我只需要一个’重复的石斑鱼’
note2:使用python 2.x.
解决方法
collections.defaultdict()
object为新密钥显示列表,然后按电影标题键入:
from collections import defaultdict grouped = defaultdict(list) for film in f: grouped[film['film'][0]].append(film)
电影[‘电影’] [0]值用于分组电影.如果您想使用更复杂的标题分组,则必须创建该密钥的规范版本.
演示:
>>> from collections import defaultdict >>> import json >>> with open('film.json') as film_file: ... f = json.load(film_file) ... >>> grouped = defaultdict(list) >>> for film in f: ... grouped[film['film'][0]].append(film) ... >>> grouped defaultdict(<type 'list'>,{u'Street fighter': [{u'director': [u'E. de Souza'],u'price': [u'2,00'],u'film': [u'Street fighter'],u'year': [u'1994']}],u'Pulp Fiction': [{u'director': [u'Tarantino'],u'price': [u'20,u'film': [u'Pulp Fiction'],u'Pulp Fyction': [{u'director': [u'Tarantino'],u'price': [u'15,u'film': [u'Pulp Fyction'],u'The Matrix': [{u'director': [u'Wachowski'],u'price': [u'19,u'film': [u'The Matrix'],u'year': [u'1999']},{u'director': [u'Wachowski'],u'year': [u'1999']}],u'Blade Runner': [{u'director': [u'Ridley Scott'],99'],u'film': [u'Blade Runner'],u'year': [u'1982']}],u'Kill Bill vol.1': [{u'director': [u'Tarantino'],u'price': [u'10,u'film': [u'Kill Bill vol.1'],u'year': [u'2003']}],u'The Matrix Reloaded': [{u'director': [u'Wachowski'],u'price': [u'9,u'film': [u'The Matrix Reloaded'],u'year': [u'2003']}]}) >>> from pprint import pprint >>> pprint(dict(grouped)) {u'Blade Runner': [{u'director': [u'Ridley Scott'],u'Street fighter': [{u'director': [u'E. de Souza'],u'year': [u'2003']}]}
使用SoundEx分组电影将如下:
from itertools import groupby,islice,ifilter _codes = ('bfpv','cgjkqsxz','dt','l','mn','r') _sounds = {c: str(i) for i,code in enumerate(_codes,1) for c in code} _sounds.update(dict.fromkeys('aeIoUy')) def soundex(word,_sounds=_sounds): grouped = groupby(_sounds[c] for c in word.lower() if c in _sounds) if _sounds.get(word[0].lower()): next(grouped) # remove first group. sdx = ''.join([k for k,g in islice((g for g in grouped if g[0]),3)]) return word[0].upper() + format(sdx,'<03') grouped_by_soundex = defaultdict(list) for film in f: grouped_by_soundex[soundex(film['film'][0])].append(film)
导致:
>>> pprint(dict(grouped_by_soundex)) {u'B436': [{u'director': [u'Ridley Scott'],u'K414': [{u'director': [u'Tarantino'],u'P412': [{u'director': [u'Tarantino'],u'year': [u'1994']},{u'director': [u'Tarantino'],u'S363': [{u'director': [u'E. de Souza'],u'T536': [{u'director': [u'Wachowski'],u'year': [u'2003']},u'year': [u'1999']}]}
关于使用Python,我可以保留一个持久字典并对其进行修改吗?和python保留字可以做变量名吗的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c – 子集一个向量并对其进行排序、c – 我可以轻松地创建一个模板函数,它接受任意类型的任意容器并对其进行操作吗?、Golang将数组传递给函数并对其进行修改、Python – 在字典列表中查找重复项并对其进行分组等相关知识的信息别忘了在本站进行查找喔。
本文标签: