2019-06-12

1.已知一个数字列表,求列表中心元素。

nums1=[1,2,3,4,5,6,7]
length=len(nums1)
if length%2==0:
    index1=length/2
    index2=length/2-1
    print(nums1[index2],nums1[index1])
else:
    index=length//2
    print(nums1[index])

2.已知一个数字列表,求所有元素和。

nums1=[1,2,3,4,5,6,7]
a=0
for item in nums1:
    a+=item
print(a)

3.已知一个数字列表,输出所有奇数下标元素。

nums1=[1,2,3,4,5,6,7]
for index in nums1[:len(nums1)-1]:
    if index%2!=0:
        print(nums1[index])

4.已知一个数字列表,输出所有元素中,值为奇数的元素。

nums=[1,2,3,4,5,6,7]
a=0
for item in nums:
    if item%2!=0:
        a+=item
print(a)

5.已知一个数字列表,将所有元素乘二。

例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]

nums=[1,2,3,4,5,6,7]
new_nums=[]
for item in nums:
    item*=2
    new_nums.append(item)
print(new_nums)

6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的

例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']

names = ['张三', '李四', '大黄', '张三','王二','老三','老四','老五','老刘','老李']
length=len(names)
for item in names[:]:
    if names.count(item)>1:
        names.remove(item)
print(names)

7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表

例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']

list1 = [97, 98, 99]
list=[]
for item in list1:
    new=chr(item)
    list.append(new)
print(list)

8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)

fens=[85,86,87,88,89,90,91,92,93,94,95]
fens.remove(max(fens))
fens.remove(min(fens))
length=len(fens)
print(sum(fens)/length)

9.有两个列表A和B,使用列表C来获取两个列表中公共的元素

例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']

A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
D=A+B
C=[]
for item in D:
    ct=D.count(item)
    if ct>1:
        C.append(item)
for item in C:
    if C.count(item)>1:
        C.remove(item)
print(C)

10.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)

例如: nums = [19, 89, 90, 600, 1] —> 600

nums = [19, 89, 90, 600, 1]
max1=0
length=len(nums)

for index in range(length):
    if max1<nums[index]:
        max1=nums[index]
print(max1)

11.获取列表中出现次数最多的元素

例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3

nums = [1,2,3,1,4,2,1,3,7,3,3]
max1=0
for item in nums:
    ct=nums.count(item)
    if max1<ct:
        max1=ct
max_nums=[]
for item in nums:
    if ct==nums.count(item):
        if item not in max_nums:
            max_nums.append(item)
print(max_nums)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.已知一个数字列表,求列表中心元素. 2.已知一个数字列表,求所有元素和。 3.已知一个数字列表,输出所有奇数下...
    我感觉你很皮阅读 689评论 0 0
  • 1.使用位运算判断一个数是否是奇数num & 1 ==0 num 是偶数num & 1 ==1 num 是奇数...
    多多爸是小白阅读 4,381评论 0 0
  • 1.1 信号、系统及信号处理 信号定义为随时间、空间、或其他自变量而变化的物理量。数学上,我们把一个信号描述为...
    崔冬明阅读 694评论 0 0
  • /* 打印1-100之间所有奇数之和 */ var b = 0; for (var i = 1;i<100; i+...
    寻_4533阅读 858评论 0 0
  • 凌晨三点,我A和老驾驶着铁骑在这个城市边缘游走。车是两辆从一朋友车行以试驾为由租借来的雅马哈摩托。缘由是老A说想看...
    a002fc25f10f阅读 1,612评论 0 0