Python 练习题

1 寻找平衡点：

numbers = [1,3,5,7,8,25,4,20,29];
sum = 0;
for num in numbers:
sum += num;#求出数组的和

for index in range(len(numbers)):
former = 0;
after = 0;
i = 0;
for i in range(index):#求出当前循环点的前半段数据
former += numbers[i];
after = sum - former - numbers[index];#用和减去当前数和前半部分和

if(former == after):
print ("The balance point is:",numbers[index]);

（1）for循环中的递增迭代： for index in range(len(numbers)): 对于数组的索引迭代，需要用到len（）求数组的长度，用range进行索引迭代。

for i in range(index):在这句循环的时候，本来我的意思是i 递增到index 循环，但是非常不幸运，编译不过：

Traceback (most recent call last):
File "BalancePoint.py", line 15, in <module>
for i in index:
TypeError: 'int' object is not iterable

2 删除list里面的重复元素：

a = [1,2,3,3,4,5,1,1]
a = list(set(a))                    # ? set ?????
print (a,"/n");

shit，我居然还没学到set，这是在网上看到的方法。NND,我很郁闷的说！我用的是传统的方法，折腾死我了，不得不说，很久没写代码了，生疏了！

NameList = ["Carol","Andy","Terry","Feng","Carol","Jie","Terry"];

NameList.sort();
Flag=[];
for index in range(len(NameList)):
Flag.insert(index,0);# = 0;

index =0;
secondIndex=0;
for index in range(len(NameList)-1):
inx = index+1;
for secondIndex in NameList[index+1:]:
if (NameList[index]== secondIndex):
#del NameList[secondIndex];
#Flag.insert(secondIndex,1);
Flag[index] = 1;
inx = inx +1;
print("/n");
print (Flag,"/n");
index = 0;
end = range(len(NameList));
for index in end:
if (Flag[index] ==1):
del NameList[index];
#print (index,"/n");

print (NameList,"/n");

Flag=[];
for index in range(len(NameList)):
Flag.insert(index,0);# = 0;