int与str,str与list均可以互转,int与list的互转通过str中转
字符串转int
x = int(s, base=10)
列表(int)转int
l = [1,2,3]
x = int("".join(list(map(str,l)))) # x=123
int转字符串
s = str(x)
字符串转列表
str1 = "12345"
list_name = list(str)
或者
str1 = "1,2,3,4,5"
list_name = str.split(',')
列表转字符串
str = 'chr'.join(list_name)
获取列表的索引(反查列表索引)
https://www.cnblogs.com/changtao/p/10731874.html
list_name.index(element_name)
列表生成式
print([[1,k] for k in list_name])
返回一个列表,元素是[1,k1], [1, k2], …
带if判断
c = [1, 3, -3, 4, -2, 8, -7, 6]
d = [x for x in c if x > 0]
两个列表
# coding:utf-8
a = [1, 2, 3, 4, 5]
b = ["a", "b", "c", "d", "e"]
# 多个参数列表生成式
c = [str(x)+str(y) for x, y in zip(b, a)]
print(c)
# 结果:['a1', 'b2', 'c3', 'd4', 'e5']