zoukankan      html  css  js  c++  java
  • python关于for循环的几个函数

    1.enumerate:返回2个值,1是当前的for循环的第几轮,2是循环得到的数值

    enumerate works by supplying a corresponding index to each element in the list that you pass it. Each time you go through the loop, index will be one greater, and item will be the next item in the sequence.

    choices = ['pizza', 'pasta', 'salad', 'nachos']
    
    print 'Your choices are:'
    for index, item in enumerate(choices):
        print index+1, item

    运行结果如下:

    Your choices are:
    1 pizza
    2 pasta
    3 salad
    4 nachos
    None

    2.zip:根据最小的列表,pairs它们

    It's also common to need to iterate over two lists at once. This is where the built-in zip function comes in handy.

    zip will create pairs of elements when passed two lists, and will stop at the end of the shorter list.zip can handle three or more lists as well!

    例子:

    list_a = [3, 9, 17, 15, 19]
    list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
    
    for a, b in zip(list_a, list_b):
        # Add your code here!
        if a >= b:
            print a
        else: 
            print b

    第一组得到a=3 b=2 输出3,第二组得到a=9 b=4 输出9。。。以此类推。

    3.For / else:for里面有break时,else的内容不执行。

    In this case, the else statement is executed after the for, but only if the for ends normally—that is, not with a break. This code will break when it hits 'tomato', so the else block won't be executed.

    fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
    
    print 'You have...'
    for f in fruits:
        if f == 'tomato':
            print 'A tomato is not a fruit!' # (It actually is.)
            break
        print 'A', f
    else:
        print 'A fine selection of fruits!'

    ps:小练习题:

    1.验证一个数是整数:

    def is_int(x):
        if int(x) == x:
            return True
        else:
            return False

     2.将类似于1234的数值加起来:1+2+3+4 = 10

    方法1:转换为字符,将数字一个个取出来

    def digit_sum(n):
        s = str(n)
        sum = 0
        for s1 in s:
            sum += int(s1)
        return sum 

    方法2:使用%和/得到一个个数字

    def digit_sum(n):
        sum = 0
        while n > 0:
            sum += (n % 10)
            n = int(n / 10)
        return sum
            

    3.迭代计算阶乘

    def factorial(x):
        f = 1
        while x > 1:
            f = f * x
            x -= 1
        return f

    4.判断是否是质数

    def is_prime(x):
        if x == 1:
            return False
        elif x == 2:
            return True
        else:
            s = 2
            while s != x:
                if x % s == 0:
                    return False
                s += 1
            else:
                return True

    5.倒序输出一个字符串:

    def reverse(text):
        s = ""
        for i in range(0,len(text)):
            a = text[len(text) -1 - i]
            s = s + a
        return s

     6.返回输入列表中不重复的内容:

    def remove_duplicates(list):
        s = []
        for i in list:
                if i not in s:
                    s.append(i)
        return s

     7.返回一个列表里的中值:

    def median(list):
        list_sorted = sorted(list)
        length = len(list_sorted)
        print list_sorted
        if length % 2 == 0:
            return float(list_sorted[length/2 - 1] + list_sorted[length/2])/2
        else:
            return list_sorted[length/2]

  • 相关阅读:
    MFC添加右键菜单
    人生导师——如何学习C++的Windows方向
    删除CListCtrl中具有某一相同数据的所有行
    向某地址写入值并执行
    问题解决——使用CriticalSection后 0xXXXXXXXX处最可能的异常: 0xC0000005: 写入位置 0x00000014 时发生访问冲突
    问题解决——Win7 64 安装 AutoCAD 2010 32位 和 清华天河PC CAD
    问题解决——在结构体中使用set保存结构体数据
    问题解决——基于MSCOMM32.OCX控件的类在客户机不能创建控件
    问题解决——ShowWindow不显示窗口
    问题解决——cout 输出 CString
  • 原文地址:https://www.cnblogs.com/bangbangjiang/p/3375840.html
Copyright © 2011-2022 走看看