求助:关于2个python的题目# DataSciences - 数据科学
k*6
1 楼
有python比较熟练的朋友帮忙看下这两道题吗?万分感激。。
Do both of these problems using at least one loop each and not using any
special pre-existing functions that give you the answer with just one line
of code.
1. Make a function to count the number of times a specified number is
repeated in a list/array. That means that if we have a number list
numberList=[5, 12, 0, 7, 2, 5, 10, 9, 12, 5];
and we use the function call
numRepeats(numberList, 5)
we get the output: 3 --- meaning 5 occurs three times in numberList.
2. Make a function that takes in a multi-dimensional array/list and returns
the "diagonal" --- a list of the elements at the 1st row-and-1st column, 2nd
row-and-2nd column, 3rd row-and-3rd column, etc. That means if we have a
multi-dimensional array:
multiArray = [[3, -4, 12, 5], [5, 2, 11, -5], [2, 2, 0, 5], [-5, -3, 2, 2]
];
and we use the function call
diagonal(multiArray)
we get the output [3, 2, 0, 2] .
Do both of these problems using at least one loop each and not using any
special pre-existing functions that give you the answer with just one line
of code.
1. Make a function to count the number of times a specified number is
repeated in a list/array. That means that if we have a number list
numberList=[5, 12, 0, 7, 2, 5, 10, 9, 12, 5];
and we use the function call
numRepeats(numberList, 5)
we get the output: 3 --- meaning 5 occurs three times in numberList.
2. Make a function that takes in a multi-dimensional array/list and returns
the "diagonal" --- a list of the elements at the 1st row-and-1st column, 2nd
row-and-2nd column, 3rd row-and-3rd column, etc. That means if we have a
multi-dimensional array:
multiArray = [[3, -4, 12, 5], [5, 2, 11, -5], [2, 2, 0, 5], [-5, -3, 2, 2]
];
and we use the function call
diagonal(multiArray)
we get the output [3, 2, 0, 2] .