Redian新闻
>
求助:关于2个python的题目
avatar
求助:关于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] .
avatar
l*n
2
同学先找本书看看,啥语言都可以...

【在 k*****6 的大作中提到】
: 有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.

avatar
k*6
3
我编程很弱,已经在看但是做不出来,但是又急需这两个题目的coding。
手忙脚乱的好急,希望能得到一些帮助,真心感谢。

【在 l******n 的大作中提到】
: 同学先找本书看看,啥语言都可以...
avatar
g*u
4
童鞋,你编程实在有点弱啊。。。
def numRepeats(numlist, num):
count = 0
for i in numlist:
if i == num: count += 1
return count

def diagonal(multiArray):
m = len(multiArray)
if m == 0: return []
n = len(multiArray[0])
diag = []
for i in range(min(m,n)):
diag.append(multiArray[i][i])
return diag

【在 k*****6 的大作中提到】
: 有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.

avatar
t*u
5
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 的大作中提到】
: 有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.

avatar
w*a
6
这个跟python没有关系,都是编程的入门问题。
你先学习编程吧,喜欢哪个语言就用哪个,你需要的是编程的思维。

【在 k*****6 的大作中提到】
: 有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.

avatar
B*g
7
啥也不说了,你直接发包子吧
1
def numRepeats(numlist, num):
return numlist.count(num)
or
def numRepeats(numlist, num):
return sum(1 for s in numlist if s==num)
2
def diagonal(multiArray):
return [s[idx] for idx,s in enumerate(multiArray)]

【在 k*****6 的大作中提到】
: 有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.

avatar
d*g
8
这是很基础的东西 不会python 不过用C++ 之类很容易

【在 k*****6 的大作中提到】
: 有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.

avatar
T*u
9
1. len(filter( lambda x: x==thisValue, array))
2. yourDiagonoal = [item[i] for i,item in zip(range(len(yourMultiArray)),
yourMultiArray)]
avatar
s*h
10
估计出题人会认为.count是调用预置函数。

【在 t****u 的大作中提到】
: 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])

avatar
l*8
11
不可能吧。你是不是不习惯看英语啊?

【在 k*****6 的大作中提到】
: 有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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。