首页 >后端开发 >Python教程 >Python 列表教程

Python 列表教程

Mary-Kate Olsen
Mary-Kate Olsen原创
2025-01-07 06:23:48870浏览

Python List Tutorial

#Problem 1: Print the following Output from the 2 given lists.
#10 5 10 7
#20 5 20 10 20 30
#30 5 30 10 
l1 = [10,20,30]
l2 = [5,10,30]
for value in l1:
    for elem in l2:
        if value != elem:
            print(value, elem, end=' ')
    print()


#Problem 2 : Missing Values

print('===================================================')
l1 = [10,20,30,40,50,70,80,100,120,130,170]
d = l1[1] - l1[0]
i=0
while i<(len(l1)-1):
    if (l1[i+1] - l1[i]) != d :
        print('Missing Value:',l1[i]+d)
    i+=1        


#Problem 3 : l1 = [5,0,10,3,2,7,6,5,4,9,1]
#Find pair of elements with a given sum =10
print('===================================================')
l1 = [5,0,10,3,2,7,6,5,4,9,1]
i=0
j=0
while i <  len(l1):
    while j < len(l1):
        if l1[i]+l1[j] == 10 and j != i:
            print('#Element:',i,'-',l1[i],'#Index[',j,']',l1[j])
        j+=1
    i+=1
    j=0







以上是Python 列表教程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn