The python implementation turns the even number in the list into its square

Time:2021-10-13

I’ll stop talking nonsense. Let’s look at the code directly~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#Write a program to turn the even number in the list into his square
def word_len(s):
  # s = [i ** 2 for i in s if i % 2 == 0]
  L = []
  for i in s:
    if(i % 2 == 0):
      L.append(i** 2)
    else:
      L.append(i)
  return L
def main():
  s = [2, 6, 1, 3, 9, 4, 22]
  print(word_len(s))
main()

Supplement: the python list contains 10 integers between 1 and 100. Turn the odd number in the list into its square and the even number into its cube.

As follows:

The python implementation turns the even number in the list into its square

The above is my personal experience. I hope I can give you a reference, and I hope you can support developepper. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]