我有一长串这样的令牌:
a_lis = [['hi how are you'],
['im fine thanks'],
['cool lets go to the restaurant']
,...,
['I can't now']]
我想用这样的逗号分隔每个标记或术语(*):
a_lis = [['hi', 'how', 'are', 'you'],
['im', 'fine', 'thanks'],
['cool', 'lets', 'go', 'to', 'the', 'restaurant']
,...,
['I', 'can't', 'now']]
我试图:
[x.replace(' ', ', ') for x in a_lis]
但是,不起作用。艾米关于如何获得(*)的想法?
答案 0 :(得分:3)
如果a_lis
是
a_lis = [["hi how are you"],
["im fine thanks"],
["cool lets go to the restaurant"],
["I can't now"]]
然后我们可以将其展平为一个字符串列表,并split
每个字符串
[s.split() for l in a_lis for s in l]
给我们
[['hi', 'how', 'are', 'you'], ['im', 'fine', 'thanks'], ['cool', 'lets', 'go', 'to', 'the', 'restaurant'], ['I', "can't", 'now']]
答案 1 :(得分:3)
使用map的另一种方法
a_lis = [["hi how are you"],["good and you"]]
new_list = list(map(lambda x: x[0].split(' '), a_lis))
# new_list is [['hi', 'how', 'are', 'you'], ['good', 'and', 'you']]
答案 2 :(得分:1)
a_list = ['hmm this is a thing', 'and another']
new_list = []
for i in a_list:
new_list.append(i.split(' '))
print(new_list)
基本上,对带有空格作为参数的字符串使用.split()
方法。另外,请确保在这些字符串周围加上引号!
如果您有一个列表列表,只需添加另一个for循环:
a_list = [['hmm, this is a thing', 'and another'], ['more things!']]
new_list = []
for i in a_list:
sub_list = []
for k in i:
sub_list.append(k.split(' '))
new_list.append(sub_list)
答案 3 :(得分:0)
这是一种实用的方法:
Angular 6 Routes
<a routerLink="/signup">Register</a>
const appRoutes: Routes = [
{ path: 'signup', component: SignupComponent }
];