我是python的新手,我想在python3中清理并重新格式化列表 来自:
[['', '\xa0', '', ''], ['First Standard', 'First Flex', 'Business
Standard','Business Flex', 'Economy standard', 'Economy Flex', 'Economy
Saver', 'Economy Superdeal'], ['Class', 'P/F', 'A', 'C/D', ' Z/J', 'W/Y/B/M
', 'H/K/L', 'Q/G/V/E', 'S/T/U/N'], ['Change Fee\nIn']]
到
['First Standard', 'First Flex', 'Business Standard', 'Business Flex',
'Economy standard', 'Economy Flex', 'Economy Saver', 'Economy Superdeal'],
['Class','P/F', 'A', 'C/D', ' Z/J', 'W/Y/B/M ', 'H/K/L', 'Q/G/V/E',
'S/T/U/N']
答案 0 :(得分:0)
这是一种不是特别干净但不言自明的方式:
old = [['', '\xa0', '', ''], ['First Standard', 'First Flex', 'Business Standard', 'Business Flex', 'Economy standard', 'Economy Flex', 'Economy Saver', 'Economy Superdeal'], ['Class', 'P/F', 'A', 'C/D', ' Z/J', 'W/Y/B/M ', 'H/K/L', 'Q/G/V/E', 'S/T/U/N'], ['Change Fee\nIn']]
new = []
for old_element in old:
new_element = []
for string in old_element:
stripped = string.strip()
if stripped:
new_element.append(stripped)
if new_element:
new.append(new_element)
print new
修改强>
仍然在等待对问题的更好解释,但是如果你只想删除第一个和最后一个元素,你可以像@salparadise那样说,或者像下面这样的东西,这有点笼统:< / p>
old = [['', '\xa0', '', ''], ['First Standard', 'First Flex', 'Business Standard', 'Business Flex', 'Economy standard', 'Economy Flex', 'Economy Saver', 'Economy Superdeal'], ['Class', 'P/F', 'A', 'C/D', ' Z/J', 'W/Y/B/M ', 'H/K/L', 'Q/G/V/E', 'S/T/U/N'], ['Change Fee\nIn']]
# 1 means the second element of the list, because 0 is the first.
# -1 means "one element back from the end of the list".
new = old[1:-1]
答案 1 :(得分:0)
假设您需要两个单独的列表,并且您的第一个列表是:
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>${masterThougth.version}</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<checkBuildResult>false</checkBuildResult>
<projectName>${project.artifactId}</projectName>
<buildNumber>${project.build}</buildNumber>
<parallelTesting>true</parallelTesting>
<outputDirectory>target/cucumber-report/</outputDirectory>
<cucumberOutput>target/cucumber-report/</cucumberOutput>
</configuration>
</execution>
</executions>
</plugin>
您可以使用python的解包魔法来获取内部列表并将其转储到两个不同的列表中:
[['', '\xa0', '', ''],
['First Standard',
'First Flex',
'Business Standard',
'Business Flex',
'Economy standard',
'Economy Flex',
'Economy Saver',
'Economy Superdeal'],
['Class',
'P/F',
'A',
'C/D',
' Z/J',
'W/Y/B/M ',
'H/K/L',
'Q/G/V/E',
'S/T/U/N'],
['Change Fee\nIn']]