楼主: karl886
1407 5

求救:关于python的两个题目 [推广有奖]

  • 0关注
  • 0粉丝

小学生

78%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
52 点
帖子
6
精华
0
在线时间
9 小时
注册时间
2012-6-28
最后登录
2015-8-27

楼主
karl886 发表于 2015-8-27 11:21:26 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+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.

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] .
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python Dimensional dimension Functions specified specified function problems repeated special

沙发
万人往LVR 在职认证  发表于 2015-8-28 16:29:41
def numRepeats(List,Element):
    return(len([x for x in List if x==Element]))

def diagonal(multiArray):
    return([multiArray[i][i] for i in range(len(multiArray))])
已有 1 人评分论坛币 收起 理由
admin_kefu + 10 热心帮助其他会员

总评分: 论坛币 + 10   查看全部评分

藤椅
yi_wang0888 发表于 2015-8-28 16:36:27
def numRepeat(numlist,num):
    return numlist.count(num)
已有 1 人评分论坛币 收起 理由
admin_kefu + 10 热心帮助其他会员

总评分: 论坛币 + 10   查看全部评分

板凳
yi_wang0888 发表于 2015-8-28 16:38:29
def numberRepeat(numlist,num)
   return numlist.count(num)

报纸
听风之声 在职认证  发表于 2015-8-28 18:50:49
第一题如楼上所言, 不难
第二题也不难 用numpy的包也很方便
import numpy as np

def diagonal(multiArray)
      matrix_data = np.array(multiArray)
     return np.diag(matrix_data)
已有 1 人评分论坛币 收起 理由
admin_kefu + 10 热心帮助其他会员

总评分: 论坛币 + 10   查看全部评分

地板
zzbyy 发表于 2015-8-31 11:51:31
def repeatCount(seq, num):
    store = []
    for i in range(len(seq)):
        if seq[i] == num:
            store.append(seq[i])
    return len(store)

def diagonal(arr):
    store = []
    for i in range(len(arr)):
        store.append(arr[i][i])
    return store


已有 1 人评分论坛币 收起 理由
admin_kefu + 10 热心帮助其他会员

总评分: 论坛币 + 10   查看全部评分

here

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 05:25