В интерпретаторе Python возвратитесь без “'”

Я пытался найти способ сделать это с find, но это, кажется, не имеет ничего как -breadth опция. За исключением записи патча для него, попробуйте следующее колдовство оболочки (за удар):

LIST="$(find . -mindepth 1 -maxdepth 1 -type d)";
while test -n "$LIST"; do
    for F in $LIST; do
        echo $F;
        test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)";
    done;
    LIST=$NLIST;
    NLIST="";
done

я сортирую споткнувшихся на это случайно, таким образом, я не знаю, работает ли это в целом (я тестировал его только на определенной структуре каталогов, которую Вы спрашивали о)

, Если Вы хотите ограничить глубину, поместить переменную счетчика во внешний цикл, как так (я также добавляю комментарии к этому):

# initialize the list of subdirectories being processed
LIST="$(find . -mindepth 1 -maxdepth 1 -type d)";
# initialize the depth counter to 0
let i=0;
# as long as there are more subdirectories to process and we haven't hit the max depth
while test "$i" -lt 2 -a -n "$LIST"; do
    # increment the depth counter
    let i++;
    # for each subdirectory in the current list
    for F in $LIST; do
        # print it
        echo $F;
        # double-check that it is indeed a directory, and if so
        # append its contents to the list for the next level
        test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)";
    done;
    # set the current list equal to the next level's list
    LIST=$NLIST;
    # clear the next level's list
    NLIST="";
done

(заменяют 2 в -lt 2 с глубиной)

В основном это реализует стандартный алгоритм поиска в ширину с помощью $LIST и $NLIST как очередь имен каталогов. Вот последний подход как острота для легкой копии-и-вставки:

LIST="$(find . -mindepth 1 -maxdepth 1 -type d)"; let i=0; while test "$i" -lt 2 -a -n "$LIST"; do let i++; for F in $LIST; do echo $F; test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)"; done; LIST=$NLIST; NLIST=""; done
15
задан smci 3 December 2016 в 18:30
поделиться

2 ответа

В интерактивной подсказке Python, если вы вернете строку, она будет отображаться в кавычках, в основном для того, чтобы вы знали, что это строка.

Если вы просто напечатаете строку, она не будет отображаться в кавычках (если только строка не содержит кавычек).

>>> 1 # just a number, so no quotes
1
>>> "hi" # just a string, displayed with quotes
'hi'
>>> print("hi") # being *printed* to the screen, so do not show quotes
hi
>>> "'hello'" # string with embedded single quotes
"'hello'"
>>> print("'hello'") # *printing* a string with embedded single quotes
'hello'

Если вы действительно сделаете , необходимо удалить начальные / конечные кавычки, используйте метод .strip строки для удаления одинарных и / или двойных кавычек:

>>> print("""'"hello"'""")
'"hello"'
>>> print("""'"hello"'""".strip('"\''))
hello
31
ответ дан 1 December 2019 в 02:10
поделиться

Don't set DYLD_LIBRARY_PATH. Because of this env var, the dynamic linker dyld, is finding /opt/local/lib/libjpeg.dylib etc. instead of the different /System/Library/Frameworks//ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Resources/libJPEG.dylib that it needs.

def remove2(x):
    return x[1:-1]
-1
ответ дан 1 December 2019 в 02:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: