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

  • 相关阅读:
    CareerCup Questions List 职业杯题目列表
    [CareerCup] Guards in a museum 博物馆的警卫
    [LeetCode] 7. Reverse Integer 翻转整数
    Python笔记11------一个K-means聚类的小例子
    python笔记10-----便捷网络数据NLTK语料库
    Python笔记9-----不等长列表转化成DataFrame
    Python笔记8----DataFrame(二维)
    Python笔记7----Pandas中变长字典Series
    Python笔记6----数组
    Python笔记5----集合set
  • 原文地址:https://www.cnblogs.com/cmnz/p/6883350.html
Copyright © 2011-2022 走看看