美文网首页
2018-10-08 作业

2018-10-08 作业

作者: 楠木cral | 来源:发表于2018-10-08 19:26 被阅读0次

1.已知⼀个列表,求列表中⼼元素。

list1 = [5, 'hello', 'python', '千锋', '余婷姐姐', '变形金刚', 26, [1, 2, 3], 'ding']
if len(list1)&1:
list1_center1 = list1[(len(list1)-1)//2]
print(list1_center1)
else:
list1_center1 = list1[(len(list1)-2)//2]
list1_center2 = list1[(len(list1)-2)//2+1]
print(list1_center1, list1_center2)

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

list2 = [25, 98, 56, 49, 69, 46]
sum1 = 0
for x in range(len(list2)):
    sum1 += list2[x]
print('list1列表的元素和为:%d' % sum1)

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

list3 = [5, 'hello', 'python', '千锋', '余婷姐姐', '变形金刚', 26, [1,     2, 3], 'ding']
# 方法一:
print(list3[1::2])
# 方法二:
for x in range(len(list3)):
    if x % 2 != 0:
        print(list3[x], end=" ")
    else:
        continue

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

list4s = [25, 98, 56, 49, 69, 46, 55, 30, -9, 64]
# 方法一:
for x in range(len(list4s)):
    if list4s[x] % 2 != 0:
        print(list4s[x], end=" ")
    else:
        continue
# 方法二:
for list4 in list4s[:]:
    if list4 % 2 == 0:
        list4s.remove(list4)
print(list4s)

5.已知⼀个列表,将所有元素乘⼆。

list5 = [5, 'hello', 'python', '千锋', '余婷姐姐', '变形金刚', 26, [1, 2, 3], 'ding']
for x in range(len(list5)):
    list5[x] = list5[x] * 2
print(list5)

6.已知⼀个列表,将所有元素加到第⼀个元素中。

list6 = ['hello', 'python', '千锋', '余婷姐姐', '变形金刚', 'ding']
x = 1
while x < len(list6):
    list6[0] = list6[0] + list6[x]
    x += 1
print(list6)

7.已知⼀个列表A,将奇数位置元素存到B列表中,偶数元素存到C列表中。

listA = [56, 98, 55, 23, 10, 12, 77, 54, 65, 3]
listB = []
listC = []
for x in range(len(listA)):
    if x % 2 != 0:
        listB.append(listA[x])
    if listA[x] % 2 == 0:
        listC.append(listA[x])
print(listB)
print(listC)

8.把A列表的前5个元素复制到B列表中。

listA = [56, 98, 55, 23, 10, 12, 77, 54, 65, 3]
listB = listA[0:5].copy()
print(listB)

有10个不重复的数字,要求按从⼤到⼩排序。

list10 = [54, 69, 23, 52, 65, 12, 3, 78, 97, 2]
list10.sort(reverse=True)
print(list10)

11.有⼀个⻓度是10的列表,要求删除某⼀个位置的元素

list11 = [5, 'hello', 'python', '千锋', '余婷姐姐', '变形金刚', 26, [1, 2, 3], 'ding', 25]
print(list11)
index = int(input("请输入要删除元素的位置下标:"))
del list11[index]
print('删除后的列表为:')
print(list11)

12.有⼀个⻓度是10的列表,按递增排列,⽤户输⼊⼀个数,插⼊适当位置。

list12 = [54, 69, 23, 52, 65, 12, 3, 78, 97, 2]
list12.sort()
print(list12)
num = int(input("请输入要插入的数:"))
for x in range(len(list12)):
    if num < list12[x]:
        list12.insert(x, num)
        break
    if x == len(list12)-1:
        list12.append(num)
print('插入完成后的列表:')
print(list12)

13.有⼀个⻓度是10的列表,数组内有10个⼈名,要求去掉重复的

names = ['余婷姐姐', '张老师', '淼淼姐', '大傻子', '余婷姐姐', '小可爱', '淼淼姐', '利利', '余婷姐姐', '小可爱']
print(names)
new = []
for name in names:
    if name not in new:
        new.append(name)
print('去重之后的姓名列表:')
print(new)

14.把A列表的第三到第六位之间的元素删除。

listA = [5, 'hello', 'python', '千锋', '余婷姐姐', '变形金刚', 26, [1, 2, 3], 'ding']
del listA[3:5]
print('删除之后的列表:')
print(listA)

15.已知A列表,B列表,定义⼀个列表C,要求C包含A,B数组中的数据(⽆重复值)。

listA = [5, 'hello', '小可爱', 'python', '千锋', '余婷姐姐', '变形金刚', 26, [1, 2, 3], 'ding']
listB = ['余婷姐姐', '张老师', '淼淼姐', '大傻子', '小可爱', '利利', 26]
listC = []
for x in range(len(listA)):
    if listA[x] not in listC:
        listC.append(listA[x])

for x in range(len(listB)):
    if listB[x] not in listC:
        listC.append(listB[x])
print(listC)

相关文章

网友评论

      本文标题:2018-10-08 作业

      本文链接:https://www.haomeiwen.com/subject/qhyjaftx.html