python字符串,列表,字典推导式

# 字符串类型推导式,数据类型里面的元素必须是字符串
s = "|"
print(s.join(i for i in ['hello','world','lixin']))
print(s.join(['hello','world','lixin']))
print(s.join(str(i) for i in range(10) if i % 2 == 0)) 高级推导式if进行判断


l = []
for i in ['hello','world','lixin']:
    l.append(i)
print(s.join(l))

#列表推导式
import random
print([random.randint(1,5) for i in range(5)])

#9*9 乘法列表推导式

print([f'{y} * {x} = { x * y }' for x in range(1,10) for y in range(1,x+1)])

#字典推导式
print({f'h{i}': i for i in range(5)})

#集合推导式
squared = {x**2 for x in [1, 1, 2]}

tuple_j = [('你好','我好')]
ss = (x+y for x,y in tuple_j).__next__() #元祖推导式,元祖解包后生成迭代器通过next输出内容
#迭代器样子 <generator object <genexpr> at 0x0000000002945360>
#x+y for x,y in tuple_j  对元祖进行解包
print(ss)

h# 生成迭代器处理方法详细介绍 https://blog.csdn.net/renduy/article/details/42489471?utm_source=blogkpcl13

 

相关推荐
©️2020 CSDN 皮肤主题: 护眼 设计师:闪电赇 返回首页