构造一个1, 3, 5, 7, ..., 99
的列表,可以通过循环实现:
取list的前一半的元素,也可以通过循环实现:
1. L = []
n = 1 while n <= 99: L.append(n) n = n + 2
2. L = list(range(1,100,2)) print(L)
print(L[:len(L)//2])
3. L = list(filter(lambda x: x%2 == 1, range(1,100)))
print(L[:int(len(L)//2)])
4.
L=[] n=1 for n in range (1,99,2): L.append(n) print(L[:len(range (1,99,2))])
5. L = list(range(1,100,2))
print(L)
6. L = list(range(1,100,2)) print(L[0:(len(L)/2)])
7. # 构造奇数list L: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
L = [] for n in range(1, 20, 2): L.append(n) print(‘L:‘, L) # 取list的前一半的元素 H: [1, 3, 5, 7, 9] H = [] for i in range(0, int(len(L) /2)): H.append(L[i]) print(‘H:‘, H)
L = list(range(1, 20, 2))
print(‘L:‘, L)
H = L[:int(len(L) /2)]
print(‘H:‘, H)
print(‘L:‘, L)
H = L[:int(len(L) /2)]
print(‘H:‘, H)
8. L=[]
for i in range(1,100,2): n = i L.append(n)
if 99 in L:
print (L)
9. L=list(range(100)) print(L[1:50:2]) 最简单的写法,切片写法
10.print([i for i in range(1,99,2) if i <99/2])
11. L = list(range(1,99,2)) s = L[:len(L)//2] print(s)
12.
L = [n for n in range(1, 99, 2)]
print(L[:len(L)//2])
13.
l=[]
n=1
print (L)
9. L=list(range(100)) print(L[1:50:2]) 最简单的写法,切片写法
10.print([i for i in range(1,99,2) if i <99/2])
11. L = list(range(1,99,2)) s = L[:len(L)//2] print(s)
12.
13.
while n<=99:
l.append(n)
n=n+2
print(l[:len(l)/2])
14. L = [n for n in range(1, 99, 2)] print(L[:(len(L)//2)+1]) 这样才对
l.append(n)
n=n+2
print(l[:len(l)/2])
14. L = [n for n in range(1, 99, 2)] print(L[:(len(L)//2)+1]) 这样才对
时间: 2024-10-12 16:07:40