Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过用户输入创建字符串数组,并用Python按字典顺序打印它们?_Python_Arrays_String_List_Sorting - Fatal编程技术网

如何通过用户输入创建字符串数组,并用Python按字典顺序打印它们?

如何通过用户输入创建字符串数组,并用Python按字典顺序打印它们?,python,arrays,string,list,sorting,Python,Arrays,String,List,Sorting,我正在尝试创建一个小程序,提示用户输入3个单词,然后将字符串输入放入数组,然后按字典顺序对数组进行排序,并将数组打印为字符串列表 我尝试了.sort函数,但该函数不起作用。我正在从事的项目不需要我还没有很多经验的循环知识 a = [] first = input("Type a word: ") second = input("Type another word: ") third = input("Type the last word: ") a +=

我正在尝试创建一个小程序,提示用户输入3个单词,然后将字符串输入放入数组,然后按字典顺序对数组进行排序,并将数组打印为字符串列表

我尝试了.sort函数,但该函数不起作用。我正在从事的项目不需要我还没有很多经验的循环知识

    a = []
    first = input("Type a word: ")
    second = input("Type another word: ")
    third = input("Type the last word: ")
    a += first
    a += second
    a += third

    a = sorted(a)

    print(a)
我希望打印的结果是由逗号分隔的3个单词

 Apple, Banana, Egg
相反,我的代码会打印出来

 ['A', 'B', 'E', 'a', 'a', 'a', 'e', 'g', 'g', 'l', 'n', 'n', 'p', 'p']
问题是列表上的+=是两个列表的串联。。因此python将字符串Apple解释为未打包的列表['A','p','p','l','e']

两种不同的解决方案:

1.将输入列成一个单字列表:

a = []
first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")
a += [first]
a += [second]
a += [third]

a = sorted(a)

print(a)

2只需使用append方法,该方法需要一个元素

a = []
first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")
a.append(first)
a.append(second)
a.append(third)

a = sorted(a)

print(a)
问题是列表上的+=是两个列表的串联。。因此python将字符串Apple解释为未打包的列表['A','p','p','l','e']

两种不同的解决方案:

1.将输入列成一个单字列表:

a = []
first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")
a += [first]
a += [second]
a += [third]

a = sorted(a)

print(a)

2只需使用append方法,该方法需要一个元素

a = []
first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")
a.append(first)
a.append(second)
a.append(third)

a = sorted(a)

print(a)

添加到列表的最佳方法是使用.append

在你的情况下,我只想:

a = []

first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")

a.append(first)
a.append(second)
a.append(third)

print(sorted(a))

在python中将数字添加到名为列表的数组后,只需使用排序方法对单词进行字典排序

添加到列表的最佳方法是使用.append

在你的情况下,我只想:

a = []

first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")

a.append(first)
a.append(second)
a.append(third)

print(sorted(a))

在python中将数字添加到名为列表的数组后,只需使用排序方法对单词进行字典排序

不应将输入词添加到列表中,而应将其追加。当您将字符串添加到列表中时,它会将字符串分解为每个字符,然后添加它。因为不能将一种类型的数据添加到另一种类型的数据,就像不能添加1+3一样,除非它是JS,但它是完全不同的故事

因此,您应该附加单词,然后使用{}.sort方法对列表进行排序,并将其连接到字符串中

a = []

first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")

a.append(first)
a.append(second)
a.append(third)

a.sort()
finalString = ','.join(a)

print(finalString)

您不应该向列表中添加输入词,而应该将其追加。当您将字符串添加到列表中时,它会将字符串分解为每个字符,然后添加它。因为不能将一种类型的数据添加到另一种类型的数据,就像不能添加1+3一样,除非它是JS,但它是完全不同的故事

因此,您应该附加单词,然后使用{}.sort方法对列表进行排序,并将其连接到字符串中

a = []

first = input("Type a word: ")
second = input("Type another word: ")
third = input("Type the last word: ")

a.append(first)
a.append(second)
a.append(third)

a.sort()
finalString = ','.join(a)

print(finalString)