字符串分割成 python
String split in python
在下面的循环中,content
是一个包含未知数量字符串的列表。每个字符串都包含一个名称,名称后有一组数字,每个数字由 space 分隔。我正在尝试使用 split
将名称和每个分数放入一个变量中,但我遇到了麻烦,因为每个名称都有可变数量的分数。如果不知道每个名字会有多少分,我怎么能做到这一点?
for i in content:
name, score1, score2 = i.split()
print name, score1, score2
您可以使用 slicing for assignment :
for i in content:
s=i.split()
name,scores=s[0],s[1:]
最后,您将在 name
变量中获得名称,在 scores
中获得分数列表。
在python3中你可以使用star expressions
:
for i in content:
name,*scores=i.split()
您可以使用Extended Iterable Unpacking
content = ['this 3 5 2', 'that 3 5']
for i in content:
name, *score = i.split()
print(name, score)
这 Python 3.x 仅兼容。
对于Python 2.x,
content = ['this 3 5 2', 'that 3 5']
for i in content:
splitted_content = i.split()
name, dynamic_score = splitted_content[0], splitted_content[1:]
print name, dynamic_score
这个切片算法在Python2.x
first, rest = seq[0], seq[1:]
被更清洁且可能更高效的替代:
first, *rest = seq
for i in content:
print i.split(" ")[0],i.split(" ")[1],i.split(" ")[2]
拆分 returns 一个列表,因此您必须索引才能获取值。
我喜欢上面@kasra 的回答,因为它适用于 Python 2.x 和 3.x(还没有足够的分数来评论 Kasra 的 post)
只是添加一些示例代码来为可能想知道的其他人进行说明:
#!/usr/bin/env python
# coding: utf-8
fi = open('bowling_scores.txt','r')
for line in fi:
if len(line) > 1: #skip blank rows
rec=line.split(' ') #subst any delimiter, or just use split() for space
bowler,scores=rec[0],rec[1:]
print bowler, scores
fi.close()
输入文件 bowling_scores.txt
如下:
John 210 199 287 300 291
Paul 188 165 200
George 177 201
Ringo 255 189 201 300
Yoko 44
Brian
会给你这样的输出:
John ['210', '199', '287', '300', '291']
Paul ['188', '165', '200']
George ['177', '201']
Ringo ['255', '189', '201', '300']
Yoko ['44']
Brian []
在下面的循环中,content
是一个包含未知数量字符串的列表。每个字符串都包含一个名称,名称后有一组数字,每个数字由 space 分隔。我正在尝试使用 split
将名称和每个分数放入一个变量中,但我遇到了麻烦,因为每个名称都有可变数量的分数。如果不知道每个名字会有多少分,我怎么能做到这一点?
for i in content:
name, score1, score2 = i.split()
print name, score1, score2
您可以使用 slicing for assignment :
for i in content:
s=i.split()
name,scores=s[0],s[1:]
最后,您将在 name
变量中获得名称,在 scores
中获得分数列表。
在python3中你可以使用star expressions
:
for i in content:
name,*scores=i.split()
您可以使用Extended Iterable Unpacking
content = ['this 3 5 2', 'that 3 5']
for i in content:
name, *score = i.split()
print(name, score)
这 Python 3.x 仅兼容。
对于Python 2.x,
content = ['this 3 5 2', 'that 3 5']
for i in content:
splitted_content = i.split()
name, dynamic_score = splitted_content[0], splitted_content[1:]
print name, dynamic_score
这个切片算法在Python2.x
first, rest = seq[0], seq[1:]
被更清洁且可能更高效的替代:
first, *rest = seq
for i in content:
print i.split(" ")[0],i.split(" ")[1],i.split(" ")[2]
拆分 returns 一个列表,因此您必须索引才能获取值。
我喜欢上面@kasra 的回答,因为它适用于 Python 2.x 和 3.x(还没有足够的分数来评论 Kasra 的 post)
只是添加一些示例代码来为可能想知道的其他人进行说明:
#!/usr/bin/env python
# coding: utf-8
fi = open('bowling_scores.txt','r')
for line in fi:
if len(line) > 1: #skip blank rows
rec=line.split(' ') #subst any delimiter, or just use split() for space
bowler,scores=rec[0],rec[1:]
print bowler, scores
fi.close()
输入文件 bowling_scores.txt
如下:
John 210 199 287 300 291
Paul 188 165 200
George 177 201
Ringo 255 189 201 300
Yoko 44
Brian
会给你这样的输出:
John ['210', '199', '287', '300', '291']
Paul ['188', '165', '200']
George ['177', '201']
Ringo ['255', '189', '201', '300']
Yoko ['44']
Brian []