slice

Slice a binary number into groups of five digits

人走茶凉 提交于 2019-12-20 02:47:07
问题 Is there any neat trick to slice a binary number into groups of five digits in python? '00010100011011101101110100010111' => ['00010', '00110', '10111', ... ] Edit: I want to write a cipher/encoder in order to generate "easy to read over the phone" tokens. The standard base32 encoding has the following disadvantages: Potential to generate accidental f*words Uses confusing chars like chars like 'I', 'L', 'O' (may be confused with 0 and 1) Easy to guess sequences ("AAAA", "AAAB", ...) I was

Is there a way to write generic code to find out whether a slice contains specific element in Go?

穿精又带淫゛_ 提交于 2019-12-20 02:16:21
问题 I want to know is there a generic way to write code to judge whether a slice contains an element, I find it will frequently useful since there is a lot of logic to fist judge whether specific elem is already in a slice and then decide what to do next. But there seemed not a built-in method for that(For God's sake, why?) I try to use interface{} to do that like: func sliceContains(slice []interface{}, elem interface{}) bool { for _, item := range slice { if item == elem { return true } }

json error, cannot unmarshal object into Go value

﹥>﹥吖頭↗ 提交于 2019-12-20 02:15:49
问题 I have this JSON data: { "InfoA" : [256,256,20000], "InfoB" : [256,512,15000], "InfoC" : [208,512,20000], "DEFAULT" : [256,256,20000] } With JSON-to-Go, I get this Go type definition: type AutoGenerated struct { InfoA []int `json:"InfoA"` InfoB []int `json:"InfoB"` InfoC []int `json:"InfoC"` DEFAULT []int `json:"DEFAULT"` } With this code (play.golang.org) package main import ( "encoding/json" "fmt" "os" "strings" ) func main() { type paramsInfo struct { InfoA []int `json:"InfoA"` InfoB []int

Different ways of Iterating over a list

爷,独闯天下 提交于 2019-12-20 01:59:19
问题 Can somebody tell me what's the difference between this code: x = [1, 2, 3, 4, 5, 6, 7] for i in x[:]: if i == 5: x.insert(0, i) And this code: x = [1, 2, 3, 4, 5, 6, 7] for i in x: if i == 5: x.insert(0, i) Why doesn't the second one work? I know it is mentioned in the Python tutorial, but I can't quite understand it. 回答1: In the first version, you create a copy (by slicing the list from the beginning to the end), in the second one you're iterating over the original list. If you iterate over

Golang: group and sum slice of structs

筅森魡賤 提交于 2019-12-19 17:58:02
问题 I come from the .NET world where I had LINQ so I could do in-memory queries like the one we usually see in SQL. I have a slice of this structure I want to group by 8 fields, and then sum another integer field. Something like: type Register struct { id1 int id2 int id3 int id4 int id5 int id6 int id7 int id8 int money int } I thought in: Creating an Equal function, to compare structures (those eight fields). Iterate over the collection I'm analyzing. For each item check if it is already in the

Golang: group and sum slice of structs

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 17:57:19
问题 I come from the .NET world where I had LINQ so I could do in-memory queries like the one we usually see in SQL. I have a slice of this structure I want to group by 8 fields, and then sum another integer field. Something like: type Register struct { id1 int id2 int id3 int id4 int id5 int id6 int id7 int id8 int money int } I thought in: Creating an Equal function, to compare structures (those eight fields). Iterate over the collection I'm analyzing. For each item check if it is already in the

matching end of string

情到浓时终转凉″ 提交于 2019-12-19 11:43:07
问题 I'm looking for the best most efficient way to match the end of a single string with a value from a predefined list of strings. Something like my_str='QWERTY' my_lst=['QWE','QQQQ','TYE','YTR','TY'] match='TY' or match=['TY'] Under the restrictions len(my_lst) is known but arbitrary thus could be very long, probably around 30 elements in my_lst may have different len so I can't just check a defined last portion of my_str every time for my_str as well as the matching elements in my_lst they can

How to efficiently index into a 1D numpy array via slice ranges

末鹿安然 提交于 2019-12-19 09:41:50
问题 I have a big 1D array of data. I have a starts array of indexes into that data where important things happened. I want to get an array of ranges so that I get windows of length L , one for each starting point in starts . Bogus sample data: data = np.linspace(0,10,50) starts = np.array([0,10,21]) length = 5 I want to instinctively do something like data[starts:starts+length] But really, I need to turn starts into 2D array of range "windows." Coming from functional languages, I would think of

How can I access a deeply nested dictionary using tuples?

≯℡__Kan透↙ 提交于 2019-12-19 07:51:30
问题 I would like to expand on the autovivification example given in a previous answer from nosklo to allow dictionary access by tuple. nosklo's solution looks like this: class AutoVivification(dict): """Implementation of perl's autovivification feature.""" def __getitem__(self, item): try: return dict.__getitem__(self, item) except KeyError: value = self[item] = type(self)() return value Testing: a = AutoVivification() a[1][2][3] = 4 a[1][3][3] = 5 a[1][2]['test'] = 6 print a Output: {1: {2: {

How can I access a deeply nested dictionary using tuples?

一曲冷凌霜 提交于 2019-12-19 07:51:10
问题 I would like to expand on the autovivification example given in a previous answer from nosklo to allow dictionary access by tuple. nosklo's solution looks like this: class AutoVivification(dict): """Implementation of perl's autovivification feature.""" def __getitem__(self, item): try: return dict.__getitem__(self, item) except KeyError: value = self[item] = type(self)() return value Testing: a = AutoVivification() a[1][2][3] = 4 a[1][3][3] = 5 a[1][2]['test'] = 6 print a Output: {1: {2: {