slice

go语言学习之路三:切片

柔情痞子 提交于 2020-04-03 18:41:44
  前面讲了变量的有关知识,这里对于其他的数据类型就不多作介绍,(和C差不多),因此重点来讨论下切片。 一、切片是引用类型,这里要稍微介绍两个概念:值类型,构造类型和引用类型   1、值类型:是一种由类型的实际值表示的数据类型。如果向一个变量分配值类型,则该变量被附以全新的值的副本,go语言的值类型包括布尔型,整型,浮点型,复数型。   2、构造类型:和c中差不多,包括数组,结构体和字符串   3、引用类型:由类型的实际值引用表示的数据类型。如果为某个变量分配一个引用类型,则该变量将应用原始值,不创建任何副本。go语言引用类型包括切片、字典和通道。   切片通常用来实现变长数组,原型定义如下: struct Slice {   byte *array;   unit32 len;   unit32 cap; }; 它抽象为以下三个部分:   指向被引用的底层数组的指针;切片中元素的个数;切片分配的存储空间。 二、切片的创建和声明   1、基于底层数组创建切片   var array1=[10]int{1,2,3,4,5,6,7,8,9,10}   var slice[]int   slice1=array1[:5]   slice2=array1[5:]   slice3=array1[:]   slice4=array1   slice5=array1[0:len(array1)

An Array of Sequences(1)(含timeit 和 字符串格式化链接)

浪尽此生 提交于 2020-03-30 03:30:22
1. Listcomps (List comprehensions) do everything the map and filter functions do. (map 函数和 filter函数能做的,列表生成式都可以做) 列表生成式和 map, filter 函数运行时间的比较示例: import timeit TIMES = 10000 SETUP = """ symbols = '$¢£¥€¤' def non_ascii(c): return c > 127 """ timeit.timeit() def clock(label, cmd): res = timeit.repeat(cmd, setup=SETUP, number=TIMES) print(label, *('{:.3f}'.format(x) for x in res)) clock('listcomp :', '[ord(s) for s in symbols if ord(s) > 127]') clock('listcomp + func :', '[ord(s) for s in symbols if non_ascii(ord(s))]') clock('filter + lambda :', 'list(filter(lambda c: c > 127, map(ord, symbols)

Swapping Elements of a Slice (in-place)

不打扰是莪最后的温柔 提交于 2020-03-25 18:37:07
问题 I have posted my solution in the answers below. The question will not be updated with even more code to not further increase clutter. I'm trying to rotate all elements in a Vec<Vec<T>> clockwise. The vector is guaranteed to be square, as in v.len() == v[0].len() . The idea is to find all elements that are equivalent under rotational symmetry to v 's center swap these elements in place, using std::mem::swap My current code does not change the state of the vec. How do I fix this? fn rotate<T>(v

Loading every nth element with numpy.fromfile [duplicate]

99封情书 提交于 2020-03-24 00:29:23
问题 This question already has an answer here : Read binary flatfile and skip bytes (1 answer) Closed 19 days ago . I want to create a numpy array from a binary file using np.fromfile . The file contains a 3D array, and I'm only concerned with a certain cell in each frame. x = np.fromfile(file, dtype='int32', count=width*height*frames) vals = x[5::width*height] The code above would work in theory, but my file is very large and reading it all into x causes memory errors. Is there a way to use

Remove from slice inplace in Golang

偶尔善良 提交于 2020-03-23 12:08:08
问题 I have the following test that is printing the original input slice (after the filtering) without the element that has been removed, but with an extra element at the end making the input slice of the same length, even if after the filtering it should be shorter. I've gone through this doc https://github.com/golang/go/wiki/SliceTricks#delete However I think I am missing some gotchas about Go, because it seems I am using slices with the wrong approach. how can I avoid to have an "output slice"?

How to read two characters from an input string?

这一生的挚爱 提交于 2020-03-23 12:06:27
问题 I want my program to read one character from an input of random string, but when there is a space in the string, I want it to read the next two characters. For example, if I type H He , I want it to return the value for H , then detect a space then return He . How do I do this? This code is a small part in school assignment (calculating the molecular mass of random compounds). string=input('enter:') pos=0 start=None for a in string: if a == 'H': print(string[start:1]) elif a == ' ': pos=int

svg preserveAspectRatio 属性

狂风中的少年 提交于 2020-03-21 15:57:34
<svg viewBox="-1 -1 162 92" xmlns="http://www.w3.org/2000/svg"> <defs> <path id="smiley" d="M50,10 A40,40,1,1,1,50,90 A40,40,1,1,1,50,10 M30,40 Q36,35,42,40 M58,40 Q64,35,70,40 M30,60 Q50,75,70,60 Q50,75,30,60"></path> </defs> <!-- (width>height) meet --> <rect x="0" y="0" width="20" height="10"> <title>xMidYMid meet</title> </rect> <svg viewBox="0 0 100 100" width="20" height="10" preserveAspectRatio="xMidYMid meet" x="0" y="0"> <use href="#smiley"></use> </svg> <rect x="25" y="0" width="20" height="10"> <title>xMinYMid meet</title> </rect> <svg viewBox="0 0 100 100" width="20" height="10"

Go 函数

江枫思渺然 提交于 2020-03-19 03:06:54
Golang之匿名函数和闭包 Go语言支持匿名函数,即函数可以像普通变量一样被传递或使用。 使用方法如下: main.go package main import ( "fmt" ) func main() { var v func(a int) int v = func(a int) int { return a * a } fmt.Println(v(6)) //两种写法 v1 := func(i int) int { return i * i } fmt.Println(v1(7)) } GO语言的匿名函数就是闭包,以下是《GO语言编程》中对闭包的解释 基本概念 闭包是可以包含自由(未绑定到特定对象)变量的代码块,这些变量不在这个代码块内或者 任何全局上下文中定义,而是在定义代码块的环境中定义。要执行的代码块(由于自由变量包含 在代码块中,所以这些自由变量以及它们引用的对象没有被释放)为自由变量提供绑定的计算环 境(作用域)。 闭包的价值 闭包的价值在于可以作为函数对象或者匿名函数,对于类型系统而言,这意味着不仅要表示 数据还要表示代码。支持闭包的多数语言都将函数作为第一级对象,就是说这些函数可以存储到 变量中作为参数传递给其他函数,最重要的是能够被函数动态创建和返回。 再次讲解闭包: 一个函数和与其相关的引用环境,组合而成的实体: package main import

Calculating sha256 gives different results after appending slices depending on if I print out the slice before or not

本秂侑毒 提交于 2020-03-16 07:47:32
问题 I am calculating a sha256 from multiple strings. I convert them to byte slices in a specific way and append them all together and then compute the hash using the built in library. However, depending on if I print out the slice before calculating the sha256 or not I wierdly get different results. When testing it in playground I cannot reproduce it. The tested code can been seen and run on https://play.golang.org/p/z8XKx-p9huG where it actually gives the same result in both cases. func getHash

Calculating sha256 gives different results after appending slices depending on if I print out the slice before or not

為{幸葍}努か 提交于 2020-03-16 07:47:27
问题 I am calculating a sha256 from multiple strings. I convert them to byte slices in a specific way and append them all together and then compute the hash using the built in library. However, depending on if I print out the slice before calculating the sha256 or not I wierdly get different results. When testing it in playground I cannot reproduce it. The tested code can been seen and run on https://play.golang.org/p/z8XKx-p9huG where it actually gives the same result in both cases. func getHash