两个列表的区别是什么?

时间:2016-03-19 10:48:19

标签: python regex

我有一个列表,我使用正则表达式删除列表中的字符串中的空格完美 -

newrooms = re.sub(r'\s+', " ", str(newrooms)) 

原始列表看起来像 -

[['4      11-12pm', 'MR252 (30)'], ['5      10.30-12pm', 'MR252 (30)'], ['8      10-11am', 'MR252 (30)'], ['9      11-12pm', 'MR252 (30)'], ['10      10-11am', 'MR252 (30)'], ['10      11-12pm', 'MR251 (22)'], ['12      10-11am', 'MR107 (63)'], ['12      11-12pm', 'MR252 (30)'], ['17      10-11am', 'MR252 (30)'], ['18      11-12pm', 'MR252 (30)'], ['19      10-11am', 'MR252 (30)'], ['19      11-12pm', 'MR265 (24)'], ['20      10-11am', 'CB203 (26)'], ['20      11-12pm', 'MR252 (30)'], ['27      10-11am', 'MR252 (30)'], ['28      11-12pm', 'MR252 (30)'], ['29      10-11am', 'MR252 (30)'], ['42      11-12pm', 'MR252 (30)'], ['42       2-4pm                MA ONLY', 'MR252 (30)'], ['43      10-11am', 'MR252 (30)'], ['44      10-11am', ''], ['44      11-12pm', 'MR252 (30)']]

打印新房[3]照片......" [' 9 11-12pm',' MR252(30)']"

使用re.sub删除列表后的空格

[['4 11-12pm', 'MR252 (30)'], ['5 10.30-12pm', 'MR252 (30)'], ['8 10-11am', 'MR252 (30)'], ['9 11-12pm', 'MR252 (30)'], ['10 10-11am', 'MR252 (30)'], ['10 11-12pm', 'MR251 (22)'], ['12 10-11am', 'MR107 (63)'], ['12 11-12pm', 'MR252 (30)'], ['17 10-11am', 'MR252 (30)'], ['18 11-12pm', 'MR252 (30)'], ['19 10-11am', 'MR252 (30)'], ['19 11-12pm', 'MR265 (24)'], ['20 10-11am', 'CB203 (26)'], ['20 11-12pm', 'MR252 (30)'], ['27 10-11am', 'MR252 (30)'], ['28 11-12pm', 'MR252 (30)'], ['29 10-11am', 'MR252 (30)'], ['42 11-12pm', 'MR252 (30)'], ['42 2-4pm MA ONLY', 'MR252 (30)'], ['43 10-11am', 'MR252 (30)'], ['44 10-11am', ''], ['44 11-12pm', 'MR252 (30)']]

它只是相同(减去空格)但现在=

打印新房[3]打印......" 4"

这里的所有代码=

print newrooms[3]
print newrooms
newrooms = re.sub(r'\s+', " ", str(newrooms))
print newrooms[3]
print newrooms

为什么列表现在不像列表一样?

好的家伙,我知道,我正在将整个列表转换成带有str(新房)的字符串,我应该做的是......

 print newrooms[3]
    print newrooms
    for obj in newrooms:
        obj[0] = re.sub(r'\s+', " ", (obj[0]))
    print newrooms[3]
    print newrooms

5 个答案:

答案 0 :(得分:4)

newrooms = re.sub(r'\s+', " ", str(newrooms))

newrooms,以前是list(),变成了一个字符串。

print newrooms[3]

打印该字符串中的第4个字符。 Python是变形函数,因此每个变量都可以灵活地适应您存储的变量。

答案 1 :(得分:1)

您将list newrooms转换为此行中的单个字符串:

newrooms = re.sub(r'\s+', " ", str(newrooms))

所以它只是一个字符串而不再是列表。 你想要做的是在列表的单个元素上应用替换:

newrooms = [
    [re.sub(r'\s+', " ", elem) for elem in sublist]
    for sublist in newrooms
]

这导致:

>>> newrooms[3]
['9 11-12pm', 'MR252 (30)']

答案 2 :(得分:1)

你想要的是用列表列表中的每个字符串用一个空格替换重复空格的序列。

您实际执行的操作是将列表转换为字符串,然后执行替换操作。

以下是发生的情况 - 为了便于阅读,我将使用原始列表的缩短版本:

>>> import re
>>> newrooms = [['4      11-12pm', 'MR252 (30)'], ['5      10.30-12pm', 'MR252 (30)']]
>>> newrooms_str = str(newrooms)
>>> newrooms_str
"[['4      11-12pm', 'MR252 (30)'], ['5      10.30-12pm', 'MR252 (30)']]"
>>> newrooms_str = re.sub(r'\s+', " ", newrooms_str)
>>> newrooms_str
"[['4 11-12pm', 'MR252 (30)'], ['5 10.30-12pm', 'MR252 (30)']]"
>>> newrooms_str[3]
'4'

如您所见,您将字符串传递给re.sub,后者返回一个字符串。该字符串的第四个字符是您'4'时看到的字符newrooms_str[3]

为了获得所需的结果,您需要对列表列表中的个人字符串进行操作:

>>> newrooms
[['4      11-12pm', 'MR252 (30)'], ['5      10.30-12pm', 'MR252 (30)']]
>>> newrooms = [[re.sub(r'\s+', " ", string) for string in sublist] for sublist in newrooms]
>>> newrooms
[['4 11-12pm', 'MR252 (30)'], ['5 10.30-12pm', 'MR252 (30)']]
>>> newrooms[1]
['5 10.30-12pm', 'MR252 (30)']

答案 3 :(得分:1)

您可以对每个子列表中的每个字符串使用str.joinstr.split,而不是将列表转换为字符串:

l = [['4      11-12pm', 'MR252 (30)'], ['5      10.30-12pm', 'MR252 (30)'], ['8      10-11am', 'MR252 (30)'], ['9      11-12pm', 'MR252 (30)'], ['10      10-11am', 'MR252 (30)'], ['10      11-12pm', 'MR251 (22)'], ['12      10-11am', 'MR107 (63)'], ['12      11-12pm', 'MR252 (30)'], ['17      10-11am', 'MR252 (30)'], ['18      11-12pm', 'MR252 (30)'], ['19      10-11am', 'MR252 (30)'], ['19      11-12pm', 'MR265 (24)'], ['20      10-11am', 'CB203 (26)'], ['20      11-12pm', 'MR252 (30)'], ['27      10-11am', 'MR252 (30)'], ['28      11-12pm', 'MR252 (30)'], ['29      10-11am', 'MR252 (30)'], ['42      11-12pm', 'MR252 (30)'], ['42       2-4pm                MA ONLY', 'MR252 (30)'], ['43      10-11am', 'MR252 (30)'], ['44      10-11am', ''], ['44      11-12pm', 'MR252 (30)']]

l[:] = [[" ".join(s.split()) for s in sub] for sub in l]

from pprint import  pprint as pp

输出将是一个列表:

[['4 11-12pm', 'MR252 (30)'],
 ['5 10.30-12pm', 'MR252 (30)'],
 ['8 10-11am', 'MR252 (30)'],
 ['9 11-12pm', 'MR252 (30)'],
 ['10 10-11am', 'MR252 (30)'],
 ['10 11-12pm', 'MR251 (22)'],
 ['12 10-11am', 'MR107 (63)'],
 ['12 11-12pm', 'MR252 (30)'],
 ['17 10-11am', 'MR252 (30)'],
 ['18 11-12pm', 'MR252 (30)'],
 ['19 10-11am', 'MR252 (30)'],
 ['19 11-12pm', 'MR265 (24)'],
 ['20 10-11am', 'CB203 (26)'],
 ['20 11-12pm', 'MR252 (30)'],
 ['27 10-11am', 'MR252 (30)'],
 ['28 11-12pm', 'MR252 (30)'],
 ['29 10-11am', 'MR252 (30)'],
 ['42 11-12pm', 'MR252 (30)'],
 ['42 2-4pm MA ONLY', 'MR252 (30)'],
 ['43 10-11am', 'MR252 (30)'],
 ['44 10-11am', ''],
 ['44 11-12pm', 'MR252 (30)']]

答案 4 :(得分:0)

它会返回意外的结果,因为您在替换之前将list转换为字符串。试试这个:

import re
newrooms = [['4      11-12pm', 'MR252 (30)'], ['5      10.30-12pm', 'MR252 (30)'], ['8      10-11am', 'MR252 (30)'], ['9      11-12pm', 'MR252 (30)'], ['10      10-11am', 'MR252 (30)'], ['10      11-12pm', 'MR251 (22)'], ['12      10-11am', 'MR107 (63)'], ['12      11-12pm', 'MR252 (30)'], ['17      10-11am', 'MR252 (30)'], ['18      11-12pm', 'MR252 (30)'], ['19      10-11am', 'MR252 (30)'], ['19      11-12pm', 'MR265 (24)'], ['20      10-11am', 'CB203 (26)'], ['20      11-12pm', 'MR252 (30)'], ['27      10-11am', 'MR252 (30)'], ['28      11-12pm', 'MR252 (30)'], ['29      10-11am', 'MR252 (30)'], ['42      11-12pm', 'MR252 (30)'], ['42       2-4pm                MA ONLY', 'MR252 (30)'], ['43      10-11am', 'MR252 (30)'], ['44      10-11am', ''], ['44      11-12pm', 'MR252 (30)']]

newrooms = [[re.sub(r'\s+', " ", room) for room in rooms] for rooms in newrooms]
print newrooms[3]