列表w3resource练习(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gardenpalace/article/details/84196234
# 1对列表中的所有项进行求和
def sum_list(sdm):
    sfg = 0
    for x in sdm:
        sfg += x
    return sfg
print(sum_list([1,2,-8]))

# 3从列表中获取最大数字
list1 = ['1','5','-1','-9']
print(max(list1))

# 4从列表中获取最小数字
list2 = [12,34,11,-15,-100]
list2[3] = -111
print(min(list2))

# 7删除列表的重复项
a = [10,20,30,20,10,50,60,40,80,50,40]
uniq_items = []
for x in a:
    if x not in uniq_items:
        uniq_items.append(x)
print(uniq_items)

# 8检查列表是否为空
def dum_list(item):
    if len(item) <= 0:
        print("为空")
    else:
        print("不为空")
print(dum_list([12,34]))

# append extend
a = [11,22,333]
a.extend([44])
print(a)
a.extend('55')
print(a)
a.append(['t','y'])
print(a)

# 9复制一个列表
original_list = [10,22,44,3]
new_list = list(original_list)
print(original_list)
print(new_list)

# 10从给定的单词列表中查找长于n的单词列表
def long_words(n,str):
    len_word = []
    txt = str.split(" ")
    for x in txt:
        if len(x) > n:
            len_word.append(x)
    return len_word
print(long_words(3,"the quick brown fox jumps over the lazy dog"))

打印结果

-5
5
-111
[10, 20, 30, 50, 60, 40, 80]
不为空
None
[11, 22, 333, 44]
[11, 22, 333, 44, '5', '5']
[11, 22, 333, 44, '5', '5', ['t', 'y']]
[10, 22, 44, 3]
[10, 22, 44, 3]
['quick', 'brown', 'jumps', 'over', 'lazy']

猜你喜欢

转载自blog.csdn.net/gardenpalace/article/details/84196234