37,721
社区成员
发帖
与我相关
我的任务
分享
请教一个问题,我搞了半天也没搞出来:(
st = [['a b', 'c d', 'e f'], ['hj kj df gfgf']]
要把st改成如下模样,即去掉每个字符串的空格:
st = [['ab', 'cd', 'ef'], ['hjkjdfgfgf']]
其实这样就可以输出,但接下来怎么弄也没对:(
for i in st:
for j in i:
j.replace(' ','')
我曾想再建立一个list来保存,但也没弄好:(
比如这样:
>>> ts = []
>>> for i in st:
for j in i:
ts.append(j.replace(' ',''))
>>> ts
['ab', 'cd', 'ef', 'hjkjdfgfgf']
而如果这样:
>>> ts = []
>>> n = 0
>>> for i in st:
ts.append([])
for j in i:
ts[n].extend(j.replace(' ',''))
n += 1
>>> ts
[['a', 'b', 'c', 'd', 'e', 'f'], ['h', 'j', 'k', 'j', 'd', 'f', 'g', 'f', 'g', 'f']]
这是怎么回事啊?我越弄越糊涂了:(
比较前一个例:为什么结果不是[['a', 'b', 'c', 'd', 'e', 'f', 'h', 'j', 'k', 'j', 'd', 'f', 'g', 'f', 'g', 'f']
st = [['a b', 'c d', 'e f'], ['hj kj df gfgf']]
print [ [s.replace(' ', '') for s in x] for x in st]
>>> x = [1, 2, 3]
>>> [i + 1 for i in x]
[2, 3, 4]
st = [['a b', 'c d', 'e f'], ['hj kj df gfgf']]
ts = []
n = 0
for i in st:
ts.append([])
for j in i:
print j.replace(' ', '')
#ts[n].extend(j.replace(' ',''))
ts[n].append(j.replace(' ',''))
n += 1
print ts