Python-04题

2020-08-06  本文已影响0人  AoEliauk

Question:

>Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:34,67,55,33,12,98

>Then, the output should be:

['34', '67', '55', '33', '12', '98']

('34', '67', '55', '33', '12', '98')

Hints:

>In case of input data being supplied to the question, it should be assumed to be a console input.tuple() method can convert list to tuple

Solution:

split() 通过指定分隔符对字符串进行切片,语法:str.split(str="", num=string.count(str))

str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。

num -- 分割次数。默认为 -1, 即分隔所有。

1)由列表转成元组

program result

2)直接打印列表和元组

program result
上一篇下一篇

猜你喜欢

热点阅读