2014-01-31 16 views
-1

我有以下代碼:檢查字符串相等,其中順序並不重要

#Write the lines back which do NOT match the command 
for line in lines: 
    if line != command: 
     file_writer.write(line) 

兩個例子字符串我是:

lines = [ 
    """user_operations.add_user("url",531,{u'Username': u'TEST123567', u'Status': u'Enabled', u'AccessTypes': [u'APN'], u'Auth': u'ServicePassword', u'ID': 7400, u'PasswordInfo': None, u'SSOInfo': None, u'Email': u'', u'AccountID': 531},False,headers)""", 
    """user_operations.add_user("url",531,{u'Username': u'TEST123567', u'Status': u'Enabled', u'Email': u'', u'PasswordInfo': None, u'AccessTypes': [u'APN'], u'AccountID': 531, u'ID': 7400, u'Auth': u'ServicePassword', u'SSOInfo': None},False,headers)""" 
] 

是否有任何快速「N」髒功能檢查它們是否包含相同的數據而不管命令如何?

謝謝!

+0

線條的實施例似乎是Python命令。你是否將'lines'中的數據作爲字符串或python命令獲取? –

+0

您的問題需要澄清。例如 - 我不知道這是否是最好的問題 - 輸入行總是以'user_operations.add_user'開始,在這種情況下真正的問題是括號內的數據是否相同?或者輸入行有多種不同的形式? – FMc

+0

這兩行是字符串(它們實際上是python命令,但是不相關)。我需要一些代碼,它執行與我發佈的代碼相同的代碼,但會匹配子字符串順序無關緊要的字符串。 – TomSelleck

回答

2

假設你在lines得到字符串列表,下面的代碼應該工作:

#Write the lines back which do NOT match the command 
sorted_command = sorted(command) 
for line in lines: 
    if sorted(line) != sorted_command: 
     file_writer.write(line) 
+0

完美,謝謝! – TomSelleck

+0

如何獲取字符串列表,你能告訴我那個嗎?如果工作請給我看完整的代碼片段,這將不起作用。 –

+0

你有沒有引號的字符串,就像在Python中無法使用的頭文件一樣。你將如何照顧這一點。 –

相關問題