怎样在获得键盘输入的整型数的列表

怎样在用户那里得到输入的列表呢?

因为Python中声明列表的时候是不用指定列表是什么类型的,所以呢,在从用户的数据中得到消息的时候怎样把得到的元素转化成自己想要的类型并且存储进来呢?看个例子:

1
2
3
4
5
6
7
8
9
10
11
12
>>> nodelist = []
>>> mystr = input("Please input the elements that would be sorted, please divided elements with space :")
Please input the elements that would be sorted, please divided elements with space :3 4 5 1 6 8 0 9 34 11
>>> print(mystr)
3 4 5 1 6 8 0 9 34 11
>>> print(mystr.split())
['3', '4', '5', '1', '6', '8', '0', '9', '34', '11']
>>> for i in mystr.split():
... nodelist.append(int(i))
...
>>> print(nodelist)
[3, 4, 5, 1, 6, 8, 0, 9, 34, 11]

这里呢首先定义了一个空的列表,然后使用input函数得到用户的输入,因为input()函数得到的内容都是字符串,所以只能得到一个字符串以后再转换成自己想要的类型,比如我们想得到int类型的元素,但是python内置的函数int()的参数只能输字符或者字符串。这是python中的int()函数的说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>>help(int)
Help on class int in module builtins:
class int(object)
| int(x=0) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). For floating point
| numbers, this truncates towards zero.
|
| If x is not a number or if base is given, then x must be a string,
| bytes, or bytearray instance representing an integer literal in the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
| >>> int('0b100', base=0)
| 4

这时候就用到了一个神奇的函数split()(或者说是str.split(),因为还有os.psth.split(),那个函数有其他的作用),我称呼它为切片函数,或者分裂函数。它的作用就是拆分字符串,按照指定的分隔符对字符串进行切片。语法是:

1
str.split(str = "",num = string.count(str))

第一个参数是指定的分隔符,默认类型是所有的空字符,包括空格,换行(\n),制表符(\t)等等。如果空着的话就是默认类型的。

第二个参数指的是将字符串分成几部分,如果说指定的num有值,那么就把字符串分割成num个子字符串。

所以可以这样得到整形数的列表:

1
2
>>> for i in mystr.split():
nodelist.append(int(i))

现在遇到了这样的一个问题,就是排序二叉树的节点的值是不能重复的,否则会出现错误,那怎么样去判断一个列表中的元素是否有重复的内容呢?这时候就会用到set()函数,这个函数可以将一个列表中的重复的元素过滤掉,当一个列表中的元素重复很多次的时候我们只留其中的一个,如果列表的长度和set()函数之后的长度一样的时候,我们认为这个列表中没有重复的元素的。

接下来还有一个问题,就是Python中是不支持goto语句的,所以需要写一个循环函数,来执行goto语句做的事情,看例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> while True:
... nodelist = []
... mystr = input("please input:")
... for i in mystr.split():
... nodelist.append(int(i))
... print(nodelist)
... if len(nodelist) != len(set(nodelist)):
... print("again!")
... continue
... else:
... break
...
please input:1 2 3 4 5 5
[1, 2, 3, 4, 5, 5]
again!
please input:1 23 4 5 67 7 8 7
[1, 23, 4, 5, 67, 7, 8, 7]
again!
please input:1 2 3 4 56
[1, 2, 3, 4, 56]
>>>

这样就完成了一个从键盘得到一个整形数的列表,并且保证列表中没有重复的元素。

在严谨一点,就得判断得到的列表是否是空的列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> while True:
... nodelist = []
... mystr = input("please input:")
... for i in mystr.split():
... nodelist.append(int(i))
... print(nodelist)
... if len(nodelist) == 0:
... print("Error, List is empty! Try again:")
... continue
... elif len(nodelist) != len(set(nodelist)):
... print("again!")
... continue
... else:
... break
...
please input:1 2 3 4 5 5
[1, 2, 3, 4, 5, 5]
again!
please input:1 23 4 5 67 7 8 7
[1, 23, 4, 5, 67, 7, 8, 7]
again!
please input:1 2 3 4 56
[1, 2, 3, 4, 56]
>>>