python and GO operation slice list way instance code

  • 2020-05-27 06:02:24
  • OfStack

python and GO operation slice, list way instance code

The GO code goes through slice, looking for some slice, and counting.


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) // The sorting 
  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
}

You can see that in the GO language above, slice has no way to find an element. I'm going to custom 1 method

The code for python below is very clean


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))  way 1 The sorting 
a.sort()
print(a)
print( a.index(3))
a.count(1)

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: