day6 作业

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

答案:
# author_lorry   
# date: 2018/11/12
import random
import string
n = input("你想看奇数还是偶数:?输入奇数or偶数")
if n == "奇数":
    flag = True
    while flag:
        a = random.randint(1,10)
        if a % 2 ==0:
            flag = False
if n == "偶数":
    flag = True
    while flag:
        a = random.randint(1,10)
        if a % 2 !=0:
            flag = False
i = 1   #此处用来生成随机数字和字母,然后添加在列表中
list1 = []
list1_middle =[ ]
while i 

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

答案:
# author_lorry   
# date: 2018/11/12
import random
n = int(input("列表中几个数字求和?"))
m = int(input("几位数求和?"))
num1 = []
for i in range(n):
    num = random.randint((10**(m-1)),1*(10**m)-1)
    num1.append(num)
print(num1)
sum_num1 = sum(num1)
print(sum_num1)

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

答案:
# author_lorry
# date: 2018/11/12
import random
import string
i = 1   
list1 = []
list1_jishu  =[]
while i <5: #此处用来生成随机数字和字母,然后添加在列表中
    str1 = string.ascii_letters
    str11 = random.choice(str1)
    list1.append(str11)
    num = random.randint(1,20)
    list1.append(num)
    i += 1
print(list1)
len_list1 = len(list1)
for index in range(1,len_list1,2):
    jishu_yuansu = list1[index]
    list1_jishu.append(jishu_yuansu)
print(list1_jishu)

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

答案:
# author_lorry
# date: 2018/11/12
import random
import string
list1 = []
jishu_value =[ ]
oushu_value = []
i = 0
while i <=10:
    num = random.randint(1,20)
    list1.append(num)
    str1 = string.ascii_letters
    str11 = random.choice(str1)
    list1.append(str11)
    num1 = random.randint(1,20)
    list1.append(num1)
    i += 1
print("生成的随机列表为:",list1)
print()
for item in list1:
    if str(item).isdigit():
        num_item = int(item)
        if num_item %2 !=0:
            jishu_value.append(num_item)
        else:
            oushu_value.append(num_item)
    else:
        print("%s"%item,end=" ")
print("不是数字")
print()
print(oushu_value,"是偶数")
print(jishu_value,"是奇数")

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

答案:

若是将元素来两遍

# author_lorry
# date: 2018/11/12
import random
import string
list1 = []
i = 0
while i <=3:
    num = random.randint(1,20)
    list1.append(num)
    str1 = string.ascii_letters
    str11 = random.choice(str1)
    list1.append(str11)
    num1 = random.randint(1,20)
    list1.append(num1)
    i += 1
print("生成的随机列表为:",list1)
list2 = list1*2
print(list2)

若是列表中数值元素2倍的形式。

# author_lorry
# date: 2018/11/12
#若是列表中数值元素2倍的形式。
import random
import string
list1 = []
list2 =[ ]
i = 0
while i <=3:
    num = random.randint(1,20)
    list1.append(num)
    num1 = random.randint(1,20)
    list1.append(num1)
    i += 1
print("生成的随机列表为:",list1)
#5.已知一个列表,将所有元素乘二。
for item in list1:
    new_item = item*2
    list2.append(new_item)
print(list2)

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

答案:

1.使用循环进行去重

names = ["lorry","lorrybz","lorry123","lorry","小明","小明","小红","小明","狗蛋","二狗子",]
index = 0
while index < len(names):
    i = 1
    while index+i < len(names):
        if names[index] ==names[index+i]:
            names.remove(names[index+i])
            print(names)
        i+=1
    index +=1
print()
print("去重后的名字为",names)

1.使用集合快速去重

names = ["lorry","lorrybz","lorry123","lorry","小明","小明","小红","小明","狗蛋","二狗子",]
names1 = set(names)
print(names1)

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

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

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

答案:
# author_lorry
#list1 = [97, 98, 99]  -> list1 = ['a', 'b', 'c']
import random
i = 0
list1 = []
list2 = []
while i <= 10:
    num = random.randint(0,6535)
    list1.append(num)
    i += 1
print(list1)
i = 0
while i < len(list1):
    item = int(list1[i])
    chr_item = chr(item)
    list2.append(str(chr_item))
    i +=1
print(list2)
二、
# author_lorry
num = []
list = []
for i in range(6536):
    new = chr(i)
    list.append(str(new))
print(list)
# author_lorry
import random
i = 0
list1 = []
list2 = []
while i <= 10:
    num = random.randint(0,6535)
    list1.append(num)
    i += 1
print(list1)
for item in list1:
    chr_item = chr(item)
    list2.append(str(chr_item))
print(list2)

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

答案:
# author_lorry
import random
scores = []
n = int(input("几个评委随机打分:?"))
new_scores = []
for _ in range(n):
    score = random.randint(700,1000)
    scores.append(score)
    scores.sort()
print(scores)
aa = scores.index(max(scores))
bb = scores.index(min(scores))
a = scores.pop(aa)
b = scores.pop(bb)
new_scores = scores
len_new_scores = len(new_scores)
average_new_scores = sum(new_scores)/len_new_scores
print("%d个评委打分,在去掉最高分%d,最低分%d后,平均得分为%s"%(n,a,b,average_new_scores))

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

答案:

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

答案:
# author_lorry
import random
import string
list1 = []
for i in range(10):
    str1 = string.ascii_letters
    str11 = random.choice(str1)
    list1.append(str11)
    num = random.randint(1,20)
    list1.append(num)
    num1 = random.randint(1,20)
    list1.append(num1)
    i += 1
list2 = []
for i in range(10):
    str1 = string.ascii_letters
    str11 = random.choice(str1)
    list2.append(str11)
    num = random.randint(1,20)
    list2.append(num)
    num1 = random.randint(1,20)
    list2.append(num1)
    i += 1
print(list1) #生成随机含有字母和数字的列表
print(list2)
public_yuansu = []
for item in list1:
    index = 0
    while index <= len(list2)-1:
        if item == list2[index]:
            public_yuansu.append(item)
        index += 1
public_yuansu=list(set(public_yuansu))  #  因为随机列表的生成可能有重复,因此对公共元素的列表进行去重
print(public_yuansu)

使用 in进行查找。

import random
import string
list1 = []
for i in range(10):
    str1 = string.ascii_letters
    str11 = random.choice(str1)
    list1.append(str11)
    num = random.randint(1,20)
    list1.append(num)
    num1 = random.randint(1,20)
    list1.append(num1)
    i += 1
list2 = []
for i in range(10):
    str1 = string.ascii_letters
    str11 = random.choice(str1)
    list2.append(str11)
    num = random.randint(1,20)
    list2.append(num)
    num1 = random.randint(1,20)
    list2.append(num1)
    i += 1
print(list1)
print(list2)  #生成随机含有字母和数字的列表
public_yuansu = []
for item in list1:
    if item in list2:
        public_yuansu.append(item)
print(public_yuansu)   #打印出两个列表的公共元素
index = 0            #  因为随机列表的生成可能有重复,因此对公共元素的列表进行去重
while index < len(public_yuansu):
    i = 1
    while index +i < len(public_yuansu):
        if public_yuansu[index] == public_yuansu[index+i]:
            public_yuansu.remove(public_yuansu[index+i])
        i +=1
    index += 1
print(public_yuansu)

你可能感兴趣的:(day6 作业)