2014-08-31 38 views
0

我已阅读并重新阅读列表解析和循环,我想这些不是我认为的那些。用于删除列表中的多个元素的用户输入

我试图通过将垃圾变成列表,排序,删除垃圾,按照用户的定义,在一堆垃圾中找到信息。用户输入一个字符,所有匹配的字符应该被删除。重复。

但是,它只能删除其中一个匹配字符,而不是全部。我相信问题在于我打电话重新开始功能的方式,但我想功能(和类呼叫这个问题)不像我认为的那样工作

这里离我已经很近了小时:(我完全小白在此之后,这样下去容易对我好吗谢谢

junk = """ 
(+*]_}%+(%[{)]({#[+$&&[^@{&#[email protected]%)!+&}[email protected][email protected][+&_*!$(+#*%[email protected]{{^**{)(]*$(}+ 
!$%}$$}[email protected]^$$%{}{#!(%[]$!*}+(]!%{(^{&^{$[$)]&&^+{+%!#[([%^!{]]#^@!{#(&]@_$*_&!%(! 
+_+}%@#{_}^#*)%*(*}*![}[%_[[^@$&%)([*{_${)$^^_!+}{)!)@_[*$_}*}$#[+}{]*+!^])}&{+# 
[email protected]!^*@}!}&{]*#^@}_[)}#@%!_*#!$}!)[(${+^&!{[&&&*[{}+*+(#+_[}{$$)#([*!)%@^%_]#%$$ 
(++^+&)}*_&%@#[^^+^&@_%]+$%$#$*)@!(]*[email protected]]}%$$}$(#$&^(%[*([&]*^&}(!#{&_}^(*{(+$#}} 
(&_+][&[email protected])$&$&^[_$(++$^}]&^^*(+*!&#_$]*[email protected]!]+{%^_*+!&}@$!#^{+_#([[email protected](((*+)[()__}(^ 
@)](+[$*_(]*$[[&@^(_*#(*&!^{+]_%)_)^[}@]#]%#@+^+[%{_*{!)}#[email protected]#)_$!_(!*+#}%%}+$&$[ 
%&]!{{%*_!*}&)}$**_{*!#%[[#]!](^^$![#[[*}%(_#^^!%))!_^@)@**@}}(%%{#*%@(((]^%^![& 
}!)$]&($)@](+(#{$)_%^%_^^#][{*[)%}+[##(##^{$}^]#&(&*{)%)&][&{]&#]}[[^^&[!#}${@_(
#@}&$[[%]_&$+)$!%{(}$^$}* 
""" 

junklist = list(junk) 


def find(): 
    junklist.sort() 
    print junklist 
    x = str(raw_input("what do you want to remove from the list?> ")) 
    return x 

def deleteChars(x): 
    while len(junklist) > 0: 
     if x in junklist: 
      junklist.remove(x) 
      find() 
     else: 
      print "not in junklist" 
      find() 

x =find() 

回答

0

我修改了程序来删除所有输入的字符,现在只需修改它就可以了递归到你的愿望。如果您需要更多帮助,请告诉我。

junk = """ 
(+*]_}%+(%[{)]({#[+$&&[^@{&#[email protected]%)!+&}[email protected][email protected][+&_*!$(+#*%[email protected]{{^**{)(]*$(}+ 
!$%}$$}[email protected]^$$%{}{#!(%[]$!*}+(]!%{(^{&^{$[$)]&&^+{+%!#[([%^!{]]#^@!{#(&]@_$*_&!%(! 
+_+}%@#{_}^#*)%*(*}*![}[%_[[^@$&%)([*{_${)$^^_!+}{)!)@_[*$_}*}$#[+}{]*+!^])}&{+# 
[email protected]!^*@}!}&{]*#^@}_[)}#@%!_*#!$}!)[(${+^&!{[&&&*[{}+*+(#+_[}{$$)#([*!)%@^%_]#%$$ 
(++^+&)}*_&%@#[^^+^&@_%]+$%$#$*)@!(]*[email protected]]}%$$}$(#$&^(%[*([&]*^&}(!#{&_}^(*{(+$#}} 
(&_+][&[email protected])$&$&^[_$(++$^}]&^^*(+*!&#_$]*[email protected]!]+{%^_*+!&}@$!#^{+_#([[email protected](((*+)[()__}(^ 
@)](+[$*_(]*$[[&@^(_*#(*&!^{+]_%)_)^[}@]#]%#@+^+[%{_*{!)}#[email protected]#)_$!_(!*+#}%%}+$&$[ 
%&]!{{%*_!*}&)}$**_{*!#%[[#]!](^^$![#[[*}%(_#^^!%))!_^@)@**@}}(%%{#*%@(((]^%^![& 
}!)$]&($)@](+(#{$)_%^%_^^#][{*[)%}+[##(##^{$}^]#&(&*{)%)&][&{]&#]}[[^^&[!#}${@_(
#@}&$[[%]_&$+)$!%{(}$^$}* 
""" 

junklist = list(junk) 


def find(): 
    junklist.sort() 
    print junklist 
    x = str(raw_input("what do you want to remove from the list?> ")) 
    deleteChars(x) 
    return x 



def deleteChars(x): 
    if x in junklist: 
     a = [y for y in junklist if y != x] 
     print a 
    else: 
     print 'not in junklist' 


x=find() 
+0

我无法弄清楚在哪里把我的while循环做出这个递归。结果发生的是我继续经历同样的循环,所以当我们创建列表(a)时,我们基本上从垃圾列表中删除的字符都回来了......我想这是一个全新的问题?感谢您的帮助。 – shuashock 2014-09-03 06:46:20

0

更改你仿佛进入一个for语句 用于Y在junklist:。 然后,只需检查如果x ==年。如果这样删除的话。

+0

嗯我不能那么工作。你有没有我可以学习的例子? – shuashock 2014-09-01 23:41:32