-1

こんにちは皆さん、Python でちょっとした課題をやっています。次のことをする必要があります。レベルなので、番号を受け取り、そのレベルにあるすべてのフォルダーを印刷するメソッドを作成する必要があります。

これまでのところ、リストを作成しましたが、キーボードを介して追加された文字列のすべての「/」を数えて保存し、レベルを知り、それらを数値と比較する方法がわかりませんプログラムが受け取ります。

どこから始めればよいか、少なくとも基本的なアイデアを教えてくれる人がいれば、とても助かります。前もって感謝します

こんなことをしたい(疑似コード)

example of some strings:
/home/user/music
/home/user/photos
/home/user/research
/home/user/music/rock

n=raw_input
if n is equal to a specific amount of "/":
print "every string that has the same amount of "/"

これまでの完全なコード

print "Please add paths"
l1=raw_input("> ")
l2=raw_input("> ")
while True:
        if l2 == "//":
            break
        else:
            l1=l1+"\n"
            l1=l1+l2
            l2=raw_input("> ")
l1=l1.split("\n")
print "Your paths are"
print "\n"
print "\n" .join(l1)
print "\n"
print l1[1].count("/")

while True:
    print "Choose an option "
    print "1. Amount of sub folders of a path"
    print "2. Amount of folders at a specified level"
    print "3. Max depth between all the folders"
    op=raw_input()
    if op == "1":
        print "Ingrese directorio"
        d=raw_input()
    if op == "2":
        print "Ingrese nivel"
        n=raw_input()
        compararnivel(n)

raw_input()
4

3 に答える 3

2

ユーザーが完全なパス名を入力し、相対パスなどがないことが確実な場合は、次のようにstr.countを単純に使用できます。

>>> str.count('/home/usr/music', '/')
3

ただし、最初にパスを完全なパス形式にするためにパスを操作する必要がある場合、および/またはパスの検証を行う必要がある場合は、os.pathで提供される関数を参照してください。

わかりましたので、ここに私のコメントを説明するいくつかのコードがあります:

  1. raw_input データを文字列に結合して次のように分割する代わりに、リストに追加できます。

    l1 = [] # This makes an empty list
    l2 = raw_input("> ") # Do this statement in a loop if you like
    l1.append(l2) # This appends l2 to the list
    
  2. while ループは、次のように表現できます。

    while l2!='//':
      append l2 to the list
      prompt the user for more input
    
  3. おそらく、ユーザーに終了を求める 4 番目のプロンプトを追加して、2 番目の while ループに終了条件を追加する必要があります。

これらすべてをまとめると、次のようになります(私のコメントに注意してください。役立つはずです)

print "Please add paths, enter // to stop" # It is a good idea to tell the user how to stop the input process

l1 = [] # Create empty list
l2 = raw_input('> ') # Ideally, you should verify that the user entered a valid path (eg: are '/' allowed at the end of a path?)

while l2 != '//':
    l1.append(l2)
    l2 = raw_input('> ')

print "Your paths are: "
for path in l1:
    print path

# I am not sure what you are trying to achieve with "print l1[1].count("/")" so leaving it out

# I would suggest to print the options outside the loop, it's a matter of taste, 
# but do you really want to keep printing them out?
print "Choose an option: \n\
       1. Number of subfolders of path\n\
       2. Number of folders at a specific path\n\
       3. Max depth between all folders\n\
       4. Quit\n"

choice = raw_input('Enter choice: ')

while choice!='4':
    if choice == '1':
        path = raw_input('Which path are you looking for?')
            # Calculate and print the output here

    # This is the question you asked, so I am answering how to process this step    
    if choice == '2':
        level = raw_input('Which level? ') # Ideally, you should verify that the user indeed enters an int here

        # Since your python does not seem to be very advanced, let's use a simple for loop:
        num=0
        for path in l1:
            if str.count(path, '/')==int(level):
                num = num+1

        print 'Number of such paths is: ', num

    if choice == '3':
        print 'this is not yet implemented'
        # Do something

    choice = raw_input('Enter choice: ')
于 2012-05-27T14:11:38.817 に答える
1

を使用str.count(substring)して、シーケンスが文字列に出現する回数をカウントします。

paths = [
    '/home/user/music',
    '/home/user/photos',
    '/home/user/research',
    '/home/user/music/rock'
]

level = 3

wantedPaths = [path for path in paths if path.count('/') == level]
print wantedPaths 
于 2012-05-27T14:00:10.573 に答える
0

これはあなたを助けるはずです:

strings = [
    '/home/user/music',
    '/home/user/photos',
    '/home/user/research',
    '/home/user/music/rock',
]

def slash_counter(n):
    return lambda x: x.count('/') == n

n = int(raw_input('Gimme n: '))

for path in filter(slash_counter(n), strings):
    print path
于 2012-05-27T14:20:41.277 に答える