t****u 发帖数: 709 | 1 numberList=[5, 12, 0, 7, 2, 5, 10, 9, 12, 5]
def numRepeats(numberList, number):
return map(str, numberList).count(str(number))
print numRepeats(numberList, 5)
第一个一行就足够了
def diagonal(multiArray):
nrow = len(multiArray)
diagonalList = []
for i in xrange(nrow):
diagonalList.append(multiArray[i][i])
return diagonalList
print diagonal(multiArray)
对空 multiArray 也一样 没问题. |
|
k*****6 发帖数: 26 | 2 有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.... 阅读全帖 |
|
k*****6 发帖数: 26 | 3 有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.... 阅读全帖 |
|
k*****6 发帖数: 26 | 4 有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.... 阅读全帖 |
|
k*****6 发帖数: 26 | 5 有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.... 阅读全帖 |
|
a****f 发帖数: 17 | 6 1 sum([1 for ele in numberList if ele == 5])
2 [ele[i] for i,ele in enumerate(inArray)] |
|