python 列表解析 List Comprehensions 一行创建list 去掉list中每个字符串首尾的字符

列表解析 List Comprehensions

  • 列表解析使创建list更可读
>>> # 创建一个list包含0到9的平方
>>> [x**2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  • for 可以有0或多个
>>> # 创建一个list包含符合条件的坐标
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
  • for可嵌套
>>> # 转array为list
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> # 创建矩阵
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

去掉list中每一个字符串的首尾字符

# 有一个很恶心的文件,这里选取一部分
f = '/// \n        /// 微信用户\n        /// \n        WeiXin = 0,\n        /// \n        /// QQ用户\n        /// \n        QQ = 1,\n        /// \n        /// 微博用户\n        /// \n        WeiBo = 2,\n        '

# 用re.split()分割,区分分隔符好像是用+|
import re
file_split = re.split(r'\n+|\n+',file)
# ['/// ',
# '        /// 微信用户\n        /// ',
# '        WeiXin = 0,\n        /// ',
# '        /// QQ用户\n        /// ',
# '        QQ = 1,\n        /// ',
# '        /// 微博用户\n        /// ',
# '        WeiBo = 2,\n        /// ']

# 去掉两遍的所有符号
file_strip = [x.strip('/,\n ') for x in file_split]
# ['',
#  '微信用户',
#  'WeiXin = 0',
#  'QQ用户',
#  'QQ = 1',
#  '微博用户',
#  'WeiBo = 2']

# 存入df
description = []
values = []
for i in range(1,len(file_strip),2):
    description.append(file_strip[i])
    values.append(file_strip[i+1])
df = pd.DataFrame({'description':description,
			       'values':values})
#  	    description 	values
# 0 	微信用户 	    WeiXin = 0
# 1 	QQ用户 	        QQ = 1
# 2 	微博用户 	    WeiBo = 2	  

参考:https://docs.python.org/3/tutorial/datastructures.html

你可能感兴趣的:(python)