python编程题-基础

2018-07-21  本文已影响0人  KingJX

list_all = [12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]

print(list_all)
if (len(list_all) % 2) == 0:
    a = int(len(list_all) / 2)
    print(list_all[a-1], list_all[a])
else:
    a = int(len(list_all) // 2)
    print(list_all[a])
    
输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
448 12
print(list_all)
sum1 = sum(list_all)
print(sum1)

输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
1212
print(list_all)
list_1 = []
for i in range(0, len(list_all)):
    if list_all[i] % 2:
        list_1.append(i)
print(list_1)

输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
[1, 3, 9, 11]
print(list_all)
list_2 = []
for i in range(0, len(list_all)):
    if list_all[i] % 2:
        list_2.append(list_all[i])
print(list_2)

输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
[45, 59, 151, 15]
print(list_all)
list3 = []
for x in list_all:
    list3.append(x * 2)
print(list3)

[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
[24, 90, 156, 118, 652, 896, 24, 8, 16, 302, 108, 30]
print(list_all)
list3 = list_all[:]
list3[0] = sum(list_all)
print(list3)

输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
[1212, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
print(list_all)
list_odd = []
list_even = []
for x in list_all:
    if x % 2 == 0:
        list_even.append(x)
    else:
        list_odd.append(x)
print(list_odd,list_even)

输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
[45, 59, 151, 15] [12, 78, 326, 448, 12, 4, 8, 54]
print(list_all)
list_a = list_all[:]
list_b = []
for x in list_a[0:5]:
    list_b.append(x)
print(list_b)

输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
[12, 45, 78, 59, 326]
str_1 = []
str_2 = []
str_3 = []
for i in range(1, 37):
    str_1.append(i)

for a in range(0, 6):
    a = a*7
    b = str_1[a]
    str_2.append(b)

for a in range(1, 7):
    a = a*5
    b = str_1[a]
    str_3.append(b)
sum1 = sum(str_2+str_3)
print(sum1)

输出结果:
222
str1 = [234, 546, 74553, 26343, 745, 523, 7345, 2, 62, 23]

for i in range(0, 10):
    for j in range(0, 10):
        if str1[i] < str1[j]:
            str1[i], str1[j] = str1[j], str1[i]
print(str1)

输出结果:
[2, 23, 62, 234, 523, 546, 745, 7345, 26343, 74553]
list_1 = [2, 23, 62, 234, 523, 546, 745, 7345, 26343, 74553]
print(list_1)
number = int(input("请输入你想要插入的数:"))
list_1.append(number)
for i in range(0, len(list_1)):
    for j in range(0, len(list_1)):
        if list_1[i] < list_1[j]:
            list_1[i], list_1[j] = list_1[j], list_1[i]
print(list_1)

输出结果:
[2, 23, 62, 234, 523, 546, 745, 7345, 26343, 74553]
请输入你想要插入的数:90
[2, 23, 62, 90, 234, 523, 546, 745, 7345, 26343, 74553]

names = ['aaa', 'bbb', 'ccc', 'aaa', 'ddd', 'bbb', 'kkk', 'ooo', 'aaa', 'qqq']
print("去除重复名字前", names)
new_names = names[:]
k = 0
for i in names:
    k = 0
    for j in new_names:
        if i == j:
            k += 1
        if k == 2:
            new_names.remove(j)
            k = 1
print('去除重复名字后', new_names)

输出结果:
去除重复名字前 ['aaa', 'bbb', 'ccc', 'aaa', 'ddd', 'bbb', 'kkk', 'ooo', 'aaa', 'qqq']
去除重复名字后 ['ccc', 'ddd', 'bbb', 'kkk', 'ooo', 'aaa', 'qqq']
list_all = [12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
list1 = list_all[:]
for _ in range(0, 6-3+1):
    del list1[2]
print(list_all)
print(list1)

输出结果:
[12, 45, 78, 59, 326, 448, 12, 4, 8, 151, 54, 15]
[12, 45, 12, 4, 8, 151, 54, 15]
list_a = [12, 45, 12, 4, 8, 151, 54, 15]
list_b = ['aaa', 'bbb', 'ccc', 'aaa', 'ddd']
list_c = list_a + list_b
print(list_a, '\n', list_b)
new_c = list_c[:]
k = 0
for i in list_c:
    k = 0
    for j in new_c:
        if i == j:
            k += 1
        if k == 2:
            new_c.remove(j)
            k = 1
print(new_c)

输出结果:
[12, 45, 12, 4, 8, 151, 54, 15] 
 ['aaa', 'bbb', 'ccc', 'aaa', 'ddd']
[45, 12, 4, 8, 151, 54, 15, 'bbb', 'ccc', 'aaa', 'ddd']
上一篇下一篇

猜你喜欢

热点阅读