一个班的学生姓名和成绩如下,打印出及格学生的姓名及成绩:
A
B
C
D
E
F
G
67
41
88
99
65
55
27

C:
  1. #include <stdio.h>

  2. typedef struct {
  3.         char name[255];
  4.         int score;
  5. } testInfoT;

  6. int main()
  7. {
  8.         testInfoT ti[] = {
  9.                 {"A", 67},
  10.                 {"B", 41},
  11.                 {"C", 88},
  12.                 {"D", 99},
  13.                 {"E", 65},
  14.                 {"F", 55},
  15.                 {"G", 27},
  16.         };

  17.         for (int i = 0; i < sizeof(ti) / sizeof(testInfoT); i++){
  18.                 if (ti[i].score > 60){
  19.                         printf("%s:%d\n", ti[i].name, ti[i].score);
  20.                 }
  21.         }

  22.         return 0;
  23. }

python:

  1. testInfo = {
  2.         "A":67, \
  3.         "B":41, \
  4.         "C":88, \
  5.         "D":99, \
  6.         "E":65, \
  7.         "F":55, \
  8.         "G":27}

  9. print(filter(lambda x : x[1] > 60, testInfo.items()))

filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回.


10-16 20:32