列表生成式就是用一句语句生成一个列表,格式基本是:x for i in L
下面是使用for循环迭代dict而生成的一个复杂表达式,将输出后的字符串保存为html文档可以生成一个表格
d = {'adam': 95, 'lisa': 85, 'bart': 59}
def generate_tr(name, score):
if score < 60:
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
return '<tr><td>%s</td></tr>'
tds = [generate_tr(name, score) for name, score in d.items()]
print('<table>')
print('<tr><th>name</th><th>score</th><tr>')
print('\n'.join(tds))
print('</table>')
列表生成式的for循环后面还可以跟if进行条件过滤,格式是:x for i in L if true
isinstance(x,str)可以用来判断x是否为字符串
列表生成式也可以用for进行多层迭代,格式为:[a*b for a in L for b in L]
迭代是一种操作,在python中值得就是for循环
迭代还有索引迭代,因为有的时候我们需要拿出一个集合的索引
enumerate()可以将每个元素变成一个(index,element)的tuple
dict中的values()方法可以将dict中的value单独封装成一个list
dict中的keys()方法可以将dict中的key单独封装成一个list
dict中的ietms()方法可以将dict中的(key,value)封装成一个list方法

py基础之列表生成式的更多相关文章

  1. Python基础(列表生成式)

    import os; list1 = list(range(1,11)) list2 = [x*x for x in list1 if x % 2 == 0]#列表生成式时,把要生成的元素x * x放 ...

  2. python基础:列表生成式和生成器

    列表生成式(List Comprehension) 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 举个例子,要生成 list ...

  3. Py修行路 python基础 (十二) 协程函数应用 列表生成式 生成器表达式

    一.知识点整理: 1.可迭代的:对象下有_iter_方法的都是可迭代的对象 迭代器:对象._iter_()得到的结果就是迭代器 迭代器的特性: 迭代器._next_() 取下一个值 优点: 1.提供了 ...

  4. python基础——列表生成式

    python基础——列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 举个例子,要生成list [1, 2, 3, 4 ...

  5. Python基础笔记:高级特性:切片、迭代、列表生成式、生成器、迭代器

    题记: 在python中,代码不是越多越好,而是越少越好.代码不是越复杂越好,而是越简单越好. 1行代码能实现的功能,绝不写5行代码. 请始终牢记:代码越少,开发效率越高. 切片 >>&g ...

  6. Python入门基础之迭代和列表生成式

    什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...

  7. python 基础 列表生成式 生成器

    列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式 举个例子,要生成list [1, 2, 3, 4, 5, 6, 7, ...

  8. [py]列表生成式-支持条件,多值的拼接

    列表生成式 代码简洁一些 支持多条件, 过滤,或拼接某些值 支持返回多值 是一种生成式 # 生成一个列表 print(list(range(1, 11))) # 生成一个列表x^2 ## 方法1: 返 ...

  9. Python基础灬列表&字典生成式

    列表生成式 # 求1~10偶数的平方 # 1.常规写法 a_list = [] for i in range(1, 11): if i % 2 == 0: a_list.append(i * i) p ...

随机推荐

  1. 字符串常用方法总结与StringBuffer基础

    字符串 基本特性 final:字符串被修饰为final,是不能被继承的. immutable:字符串是不可改变的,例如创建了一个字符串对象,就不可改变它,即不能进行增删查改其中的字符.一旦创建好这个字 ...

  2. Metasploit详解

    title date tags layout Metasploit 详解 2018-09-25 Metasploit post 一.名词解释 exploit 测试者利用它来攻击一个系统,程序,或服务, ...

  3. LoggingService

    package me.zhengjie.common.aop.log; import java.lang.annotation.ElementType; import java.lang.annota ...

  4. pycharm debug的操作

    ###################################### """ pycharm的debug按钮是右上角的一个小虫子, debug,要打断点,debu ...

  5. Caused by: java.io.FileNotFoundException: class path resource [../../resources/config/spring.xml] cannot be opened because it does not exist

    在尝试使用Spring的Test的时候遇到了这个错误 原来的代码: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loca ...

  6. VLC查看日志的方法

    最近使用VLC去播放rtsp网络串流总是失败,显示要查看日志,查看的方法: 进入VLC的GUI,点击视图,添加界面,添加调试窗口: 添加调试窗口,会弹出界面: 上面会有报错的提示. 大佬如果有其他的办 ...

  7. Create Collection Type with Class Type Constraints

    Create Collection Type with Class Type Constraints: new TypeToken<ArrayList<ClassType>>( ...

  8. 常用面试sql(1)

    1:update qr_user_info set score =score+50 where level=3 2:delete from qr_user_info where level is nu ...

  9. MOOC(9)- 登录接口返回的cookie中有多个token

  10. HashMap、Hashtable、ConcurrentHashMap、ConcurrentSkipListMap对比及java并发包(java.util.concurrent)

    一.基础普及 接口(interface) 类(class) 继承类 实现的接口 Array √ Collection √ Set √ Collection List √ Collection Map ...