美文网首页
python编程技巧1(数据结构相关)

python编程技巧1(数据结构相关)

作者: xin激流勇进 | 来源:发表于2018-08-10 19:39 被阅读0次
    image.png
    >>> from random import randint
    >>> data = [randint(-10, 10) for _ in range(10)]
    >>> data
    [-8, 0, 0, -2, 10, 9, 0, 9, 6, -4]
    >>> filter(lambda x: x >= 0, data)
    <filter object at 0x000001E40E9A3DA0>
    >>> list(filter(lambda x: x >= 0, data))
    [0, 0, 10, 9, 0, 9, 6]
    >>> [x for x in data if x >= 0]
    [0, 0, 10, 9, 0, 9, 6]
    >>> d = {x: randint(60, 100) for x in range(1, 21)}
    >>> {k: v for k, v in d.items() if v >= 90}
    {4: 91, 9: 90, 14: 97, 16: 96, 20: 99}
    >>> s = set(data)
    >>> s
    {0, 6, 9, 10, -8, -4, -2}
    >>> {x for x in s if x % 3 == 0}
    {0, 9, 6}
    
    image.png
    >>> student = ('Jim', 16, 'male', 'jim123@qq.com')
    >>> student[0] #name
    'Jim'
    >>> student[3] #mail
    'jim123@qq.com'
    >>> name, age, gender, mail = range(4)
    >>> student[mail]
    'jim123@qq.com'
    >>> student[name]
    'Jim'
    
    >>> from collections import namedtuple
    >>> student = namedtuple('Student', ['name', 'age', 'gender', 'mail'])
    >>> s = student('Jim', 16, 'male', 'jim123@qq.com')
    >>> s.index
    <built-in method index of Student object at 0x0000020D7697E1A8>
    >>> s.name
    'Jim'
    >>> s
    Student(name='Jim', age=16, gender='male', mail='jim123@qq.com')
    >>> s1 = student(name='Merry', age=16, gender='male', mail='Merry123@qq.com')
    >>> s1.name
    'Merry'
    
    image.png
    >>> from random import randint
    >>> data = [randint(1, 10) for _ in range(30)]
    >>> c = dict.fromkeys(data, 0)
    >>> c
    {2: 0, 4: 0, 7: 0, 1: 0, 6: 0, 10: 0, 8: 0, 3: 0, 9: 0}
    >>> for i in data:
        c[i] += 1   
    >>> c
    {2: 5, 4: 4, 7: 5, 1: 3, 6: 4, 10: 2, 8: 2, 3: 4, 9: 1}
    
    >>> from collections import Counter
    >>> c1 = Counter(data)
    >>> c1
    Counter({2: 5, 7: 5, 4: 4, 6: 4, 3: 4, 1: 3, 10: 2, 8: 2, 9: 1})
    >>> c1.most_common(3)
    [(2, 5), (7, 5), (4, 4)]
    
    >>> c3 = Counter(re.split('\W+', aticle))
    >>> c3.most_common(3)
    [('you', 14), ('your', 10), ('password', 7)]
    
    image.png
    >>> from random import randint
    >>> {x: randint(60, 100) for x in 'xyzabcslfj'}
    {'x': 97, 'y': 63, 'z': 87, 'a': 93, 'b': 89, 'c': 93, 's': 94, 'l': 76, 'f': 63, 'j': 85}
    >>> d = {x: randint(60, 100) for x in 'xyzabcslfj'}
    >>> sorted(d)
    ['a', 'b', 'c', 'f', 'j', 'l', 's', 'x', 'y', 'z']
    >>> list(iter(d))
    ['x', 'y', 'z', 'a', 'b', 'c', 's', 'l', 'f', 'j']
    >>> sorted(d.items(), key=lambda x: x[1])
    [('s', 60), ('f', 62), ('x', 69), ('a', 69), ('y', 77), ('b', 78),
     ('z', 83), ('c', 84), ('l', 86), ('j', 90)]
    >>> sorted(d.items(), key=lambda x: x[1], reverse=True)
    [('j', 90), ('l', 86), ('c', 84), ('z', 83), ('b', 78), ('y', 77), 
    ('x', 69), ('a', 69), ('f', 62), ('s', 60)]
    
    image.png
    >>> from random import randint, sample
    >>> sample('abcdefg', 3)
    ['e', 'b', 'd']
    >>> sample('abcdefg', randint(3, 6))
    ['b', 'd', 'c', 'e']
    >>> d1 = {k: randint(1, 4) for k in sample('abcdefg', randint(3, 6))}
    >>> d1
    {'b': 1, 'd': 3, 'c': 2, 'f': 4, 'a': 1}
    >>> d2 = {k: randint(1, 4) for k in sample('abcdefg', randint(3, 6))}
    >>> d3 = {k: randint(1, 4) for k in sample('abcdefg', randint(3, 6))}
    >>> d2
    {'b': 1, 'g': 4, 'c': 3, 'd': 3}
    >>> d3
    {'a': 3, 'c': 3, 'd': 4, 'g': 2}
    >>> [k for k in d1 if k in d2 and k in d3]
    ['d', 'c']
    
    >>> d_list = [d1, d2, d3]
    >>> [k for k in d_list[0] if all(map(lambda x : k in x, d_list[1:]))]
    ['d', 'c']
    
    >>> d1.keys()
    dict_keys(['b', 'd', 'c', 'f', 'a'])
    >>> d1.keys() & d2.keys()
    {'d', 'c', 'b'}
    >>> from functools import reduce
    >>> reduce(lambda a, b: a * b, range(1, 10))
    362880
    >>> list(map(dict.keys, d_list))
    [dict_keys(['b', 'd', 'c', 'f', 'a']), dict_keys(['b', 'g', 'c', 'd']), dict_keys(['a', 'c', 'd', 'g'])]
    >>> reduce(lambda a, b: a&b, list(map(dict.keys, d_list)))
    {'d', 'c'}
    
    image.png
    >>> from collections import OrderedDict
    >>> d = OrderedDict()
    >>> d['Jim'] = (1, 24)
    >>> d['Bob'] = (2, 30)
    >>> d['Merry'] = (3, 35)
    >>> d
    OrderedDict([('Jim', (1, 24)), ('Bob', (2, 30)), ('Merry', (3, 35))])
    >>> for i in d:
        print(i)
    Jim
    Bob
    Merry
    
    >>> player = list('ABCDEFGHI')
    >>> for i in range(len(player)):
        input()
        p = choice(player)
        player.remove(p)
        print(p, i+1, time()-start_time)
        d[p] = (i+1, time()-start_time)
    
    OrderedDict([('I', (1, 1533718954.1411808)),
     ('A', (2, 1533718954.7458303)),
     ('D', (3, 1533718955.2383544)), 
    ('F', (4, 1533718955.6509078))])
    

    相关文章

      网友评论

          本文标题:python编程技巧1(数据结构相关)

          本文链接:https://www.haomeiwen.com/subject/tgnavftx.html