1. 程式人生 > >請使用迭代查詢一個list中最小和最大值,並返回一個tuple(Python)

請使用迭代查詢一個list中最小和最大值,並返回一個tuple(Python)

from collections import Iterable, Iterator



def g():

    yield 1

    yield 2

    yield 3



print('Iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))

print('Iterable? \'abc\':', isinstance('abc', Iterable))

print('Iterable? 123:', isinstance(123, Iterable))

print('Iterable? g():', isinstance(g(), Iterable))



print('Iterator? [1, 2, 3]:', isinstance([1, 2, 3], Iterator))

print('Iterator? iter([1, 2, 3]):', isinstance(iter([1, 2, 3]), Iterator))

print('Iterator? \'abc\':', isinstance('abc', Iterator))

print('Iterator? 123:', isinstance(123, Iterator))

print('Iterator? g():', isinstance(g(), Iterator))



# iter list:

print('for x in [1, 2, 3, 4, 5]:')

for x in [1, 2, 3, 4, 5]:

    print(x)



print('for x in iter([1, 2, 3, 4, 5]):')

for x in iter([1, 2, 3, 4, 5]):

    print(x)



print('next():')

it = iter([1, 2, 3, 4, 5])

print(next(it))

print(next(it))

print(next(it))

print(next(it))

print(next(it))



d = {'a': 1, 'b': 2, 'c': 3}



# iter each key:

print('iter key:', d)

for k in d.keys():

    print('key:', k)



# iter each value:

print('iter value:', d)

for v in d.values():

    print('value:', v)



# iter both key and value:

print('iter item:', d)

for k, v in d.items():

    print('item:', k, v)



# iter list with index:

print('iter enumerate([\'A\', \'B\', \'C\']')

for i, value in enumerate(['A', 'B', 'C']):

    print(i, value)



# iter complex list:

print('iter [(1, 1), (2, 4), (3, 9)]:')

for x, y in [(1, 1), (2, 4), (3, 9)]:

    print(x, y)
#函式尋找最大最小值
def  findmaxmin(L):
    if L!=[]:
        max=L[0]
        min=L[0]
        for l in L:
            if max<l:
                max=l
            if min>l:
                min=l
        return (min,max)
    else:
        return(None,None)

L=[1,9,4,5]
print(findmaxmin(L))

這裡將列表生成式和生成器的主要用法都概括了!