Python 自动将单词分成字母?
所以我有这个代码:Python 自动将单词分成字母?,python,dictionary,argv,sorted,sys,Python,Dictionary,Argv,Sorted,Sys,所以我有这个代码: import sys ## The 'sys' module lets us read command line arguments words1 = open(sys.argv[2],'r') ##sys.argv[2] is your dictionary text file words = str((words1.read())) def main(): # Get the dictionary to search if (len(sy
import sys ## The 'sys' module lets us read command line arguments
words1 = open(sys.argv[2],'r') ##sys.argv[2] is your dictionary text file
words = str((words1.read()))
def main():
# Get the dictionary to search
if (len(sys.argv) != 3) :
print("Proper format: python filename.py scrambledword filename.txt")
exit(1) ## the non-zero return code indicates an error
scrambled = sys.argv[1]
print(sys.argv[1])
unscrambled = sorted(scrambled)
print(unscrambled)
for line in words:
print(line)
当我打印单词时,它会打印字典中的单词,一次打印一个单词,这很好。但是,只要我试着用这些单词做任何事情,就像我的最后两行一样,它就会自动地将单词分成字母,并在每行每个单词上打印一个字母。有没有办法把这些词放在一起?我的最终目标是执行ordered=sorted(line),然后执行if(ordered==unclombled),让它打印字典中的原始单词?您的单词是
str
的一个实例。您应该使用split
迭代单词:
for word in words.split():
print(word)
您的单词是
str
的一个实例。您应该使用split
迭代单词:
for word in words.split():
print(word)
您的单词是
str
的一个实例。您应该使用split
迭代单词:
for word in words.split():
print(word)
您的单词是
str
的一个实例。您应该使用split
迭代单词:
for word in words.split():
print(word)
for
-循环每次从您传递的“序列”中获取一个元素。您已将文件内容读入单个字符串,因此python将其视为字母序列。您需要的是自己将其转换为列表:将其拆分为任意大的字符串列表:
lines = words.splitlines() # Makes a list of lines
for line in lines:
....
或
for
-循环每次从您传递的“序列”中获取一个元素。您已将文件内容读入单个字符串,因此python将其视为字母序列。您需要的是自己将其转换为列表:将其拆分为任意大的字符串列表:
lines = words.splitlines() # Makes a list of lines
for line in lines:
....
或
for
-循环每次从您传递的“序列”中获取一个元素。您已将文件内容读入单个字符串,因此python将其视为字母序列。您需要的是自己将其转换为列表:将其拆分为任意大的字符串列表:
lines = words.splitlines() # Makes a list of lines
for line in lines:
....
或
for
-循环每次从您传递的“序列”中获取一个元素。您已将文件内容读入单个字符串,因此python将其视为字母序列。您需要的是自己将其转换为列表:将其拆分为任意大的字符串列表:
lines = words.splitlines() # Makes a list of lines
for line in lines:
....
或
什么是单词?你能给我看一下你得到的示例输出和期望的输出吗?好吧,它会打印整个字典,所以它每行打印一个单词:aardvark abacus Account adore,但在命令模块中,每个单词都在自己的行中。我拍了一张截图。什么是单词?你能给我看一下你得到的示例输出和期望的输出吗?好吧,它会打印整个字典,所以它每行打印一个单词:aardvark abacus Account adore,但在命令模块中,每个单词都在自己的行中。我拍了一张截图。什么是单词?你能给我看一下你得到的示例输出和期望的输出吗?好吧,它会打印整个字典,所以它每行打印一个单词:aardvark abacus Account adore,但在命令模块中,每个单词都在自己的行中。我拍了一张截图。什么是单词?你能给我看一下你得到的示例输出和期望的输出吗?好吧,它会打印整个字典,所以它每行打印一个单词:aardvark abacus Account adore,但在命令模块中,每个单词都在自己的行中。我拍了一张截图。非常感谢你。你是个救生员,非常感谢。你是个救生员,非常感谢。你是个救生员,非常感谢。你是个救生员。