如何使用rstrip和lower()处理元素列表

时间:2016-12-09 01:42:34

标签: python python-3.x python-3.5

我使用此代码:

test_list = ['A','a','word','Word','If,','As:']

for a in test_list:
    str(a)
    a.rstrip('.' or '?' or '!' or '"' or ":" or ',' or ';')
    a.lower()
    print(a)
print(test_list)

我得到了这样的结果:

A
a
word
Word
If,
As:
['A', 'a', 'word', 'Word', 'If,', 'As:']

我正在寻找类似的东西:

a
a
word
word
if
as
['a','a','word','word','if','as']

我想转换列表中的所有元素并删除所有标记,所以只有在我处理这个词的时候。

1 个答案:

答案 0 :(得分:2)

以下内容应该使用生成器表达式执行您请求的所有内容:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Collect available scripts in an array:
set /A "INDEX=0"
for %%J in ("%~dp0*.bat" "%~dp0*.cmd") do (
    if /I not "%%~nxJ"=="%~nx0" (
        set "ITEM=%%~fJ"
        call set "$BATCH[%%INDEX%%]=%%ITEM%%"
        set /A "INDEX+=1"
    )
)

rem // Execute scripts simultaneously, write exit codes to individual log files:
for /F "tokens=1,* delims==" %%I in ('set $BATCH[') do (
    start "" /MIN cmd /C rem/ ^& "%%~fJ" ^& ^> "%%~dpnJ.log" call echo %%^^ErrorLevel%%
)

rem // Deplete summary log file:
> "%~dpn0.log" rem/

rem // Polling loop to check whether individual log files are available:
:POLLING
rem // Give processor some idle time:
> nul timeout /T 1 /NOBREAK
rem /* Loop through all available array elements; for every accomplished script,
rem    so its log file is availabe, the related array element becomes deleted,
rem    so finally, there should not be any more array elements defined: */
for /F "tokens=1,* delims==" %%I in ('set $BATCH[') do (
    rem // Suppress error message in case log file is not yet available:
    2> nul (
        rem // Read exid code from log file:
        set "ERRLEV=" & set "FILE="
        < "%%~dpnJ.log" set /P ERRLEV=""
        if defined ERRLEV (
            rem // Copy the read exit code to the summary log file:
            set "NAME=%%~nxJ"
            >> "%~dpn0.log" call echo(%%ERRLEV%%    "%%NAME%%"
            rem // Undefine related array element:
            set "%%I="
            rem // Store log file path for later deletion:
            set "FILE=%%~dpnJ.log"
        )
        rem // Delete individual log file finally:
        if defined FILE call del "%%FILE%%"
    )
)
rem // Jump to polling loop in case there are still array elements:
> nul 2>&1 set $BATCH[ && goto :POLLING

rem // Check individual exit codes and return first non-zero value, if any:
set "ERRALL="
for /F "usebackq" %%I in ("%~dpn0.log") do (
    if not defined ERRALL if %%I neq 0 set "ERRALL=%%I"
)
if not defined ERRALL set "ERRALL=0"

endlocal & exit /B %ERRALL%

输出:

# Test List
test_list = ['A', 'a', 'word', 'Word', 'If,', 'As:']

# Remove certain characters and convert characters to lower case in test list
test_list = [str(a).strip('.?!":,;').lower() for a in test_list]

# Print test list
print(test_list)