zoukankan      html  css  js  c++  java
  • python_code list_3

    >>> seq=['foo','x41','?','***']
    >>> def func(x):
    return x.isalnum()

    >>> list(filter(func,seq))
    ['foo', 'x41']
    >>> seq
    ['foo', 'x41', '?', '***']
    >>> [x for x in seq if x.isalnum(),seq]
    SyntaxError: invalid syntax
    >>> [x for x in seq if x.isalnum()]
    ['foo', 'x41']
    >>> list(filter(lambda x: x.isalnum(),seq))
    ['foo', 'x41']
    >>> list(filter(None,[1,2,0,4,0,6]))
    [1, 2, 4, 6]
    >>> [1,2,3,4,5]-[2,3]
    Traceback (most recent call last):
    File "<pyshell#11>", line 1, in <module>
    [1,2,3,4,5]-[2,3]
    TypeError: unsupported operand type(s) for -: 'list' and 'list'
    >>> import random
    >>> x=[random.randint(1,100)for i in range(10)]
    >>> x
    [82, 2, 90, 58, 81, 18, 42, 40, 19, 94]
    >>> list(map(lambda i: i+5,x))
    [87, 7, 95, 63, 86, 23, 47, 45, 24, 99]
    >>> y=[random.randint(1,10)for i in range(10)]
    >>> y
    [4, 8, 10, 10, 7, 2, 9, 1, 7, 4]
    >>> import operator
    >>> sum(map(operator.mul,x,y))
    3354
    >>> sum((i*j for i,j in zip(x,y)))
    3354
    >>> list(map(operator.add,x,y))
    [86, 10, 100, 68, 88, 20, 51, 41, 26, 98]
    >>> list(map(lambda i,j: i+j, x, y))
    [86, 10, 100, 68, 88, 20, 51, 41, 26, 98]
    >>> aList=[x*x for x in range(10)]
    >>>
    >>> aList
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    >>> aList=[]
    >>> for x in range(10)
    SyntaxError: invalid syntax
    >>> for x in range(10):
    aList.append(x*x)


    >>> aList
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    >>> aList=list(lambda x: x*x, range(10))
    Traceback (most recent call last):
    File "<pyshell#32>", line 1, in <module>
    aList=list(lambda x: x*x, range(10))
    TypeError: list() takes at most 1 argument (2 given)
    >>> aListt=list(map(lambda x:x*x, range(10)))
    >>> aListt
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    >>> freshfruit=['banana','loganberry','passion fruit']
    >>> aList=[w.strip() for w in freshfruit]
    >>> aList
    ['banana', 'loganberry', 'passion fruit']
    >>> freshfruit=['banana','loganberry','passion fruit']
    >>> aList=[]
    >>> for item in freshfruit:
    aList.append(item.strip())


    >>> aList
    ['banana', 'loganberry', 'passion fruit']
    >>> freshfruit=['banana','loganberry','passion fruit']
    >>> aList =list(map(lambda x: x.strip(),freshfruit))
    >>> aList
    ['banana', 'loganberry', 'passion fruit']
    >>> freshfruit=['banana','loganberry','passion fruit']
    >>> aList=list(map(str.strip,freshfruit))
    >>> aList
    ['banana', 'loganberry', 'passion fruit']
    >>> sum([2**x for x in range(64)])
    18446744073709551615

  • 相关阅读:
    linux shell习题
    The logback manual #02# Architecture
    The logback manual #01# Introduction
    算法导论(第三版)练习 10.1-1 ~ 10.1-7
    算法笔记 #006# 快速排序 × 算法导论(第三版)练习 7.1-1 ~ 7.1-4
    Linux笔记 #08# shell编程从零开始到低配学生管理系统
    Maven笔记 #01# 入门
    Java日志学习资料收集
    jsp中用EL读取了数据库里面的时间,怎么设置格式显示的格式
    ajax异步处理时,如何在JS中获取从Servlet或者Action中session,request
  • 原文地址:https://www.cnblogs.com/cmnz/p/6883350.html
Copyright © 2011-2022 走看看