This article mainly introduces the relevant information about the example code of how to operate slice and list in python and GO. Friends who need it can refer to the following
How to operate slice and list in python and GO Example code
The GO code traverses slices, looks for a certain slice, and counts the number.
type Element interface{} func main() { a := []int{1, 2, 3, 4, 1} for _, i := range a { fmt.Println(i) } for i := 0; i < len(a); i++ { //fmt.Println(i) } fmt.Println(index0(a, 3)) fmt.Println(index0([]string{"a", "b", "c", "d", "e"}, "e")) sort.Ints(a) //排序 fmt.Println(a) } // func index0(a Element, i interface{}) int { if b, ok := a.([]int); ok { if c, ok1 := i.(int); ok1 { for indexC, v := range b { if v == c { return indexC } } } } if b, ok := a.([]string); ok { if c, ok1 := i.(string); ok1 { for indexC, v := range b { if v == c { return indexC } } } } return -1 }
It can be seen that slice in the above GO language does not have a method to find a certain element. I customized a method
The python code below is very concise
a=[1,2,3,4,1] for b in a : print(b) i=0 while i <len(a): print(a[i]) i=i+1 #print( sorted(a)) 方式一排序 a.sort() print(a) print( a.index(3)) a.count(1)
[Related recommendations]
1. Geek Academy Python Video Tutorial
3. Marco Education Python Basic Grammar Full Explanation Video
The above is the detailed content of Detailed explanation of how python and GO operate slices and lists. For more information, please follow other related articles on the PHP Chinese website!