Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can Python enter multiple numbers per line and store them in the list

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Python how to enter multiple numbers per line, and deposit them in the list", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "Python how to enter multiple numbers in a row, and deposit them in the list" article.

How to enter multiple numbers in a row and store them in the list

In python, if you only use input (), enter the contents of a line and store the contents of that line in a variable as a string, but if you want to enter multiple numbers in a line, you cannot use int (input ()).

Note: errors may be reported when using the python2 version

Related introduction: the method of converting list elements to numbers in Python

Method one

Split () is a method that splits a string into multiple strings marked by a blank character. If you want to mark the interval with a comma, put a comma in it, that is, split (',')

It is important to note that the keyboard input value obtained using input () is of type string.

So the type in the list is still a string, and if you need to operate, you need to switch to int, float, and so on.

During the conversion, I found that I can't use for i in lis to convert directly, but I need to use index / subscript to change it.

# author: Fox # enter lis = list (input (). Split ()) # display type for i in lis: print (type (I)) print () print (lis) print () # list element converted to int type for i in range (len (lis): lis [I] = int (lisi) # display type for i in lis: print (type (I) print () print (lis) method 2

This is based on method 1, using the map () function to convert the segmented character sequence into an integer sequence, and then using the list () function to convert the integer sequence into a list.

# author: Fox # enter lis = list (map (int,input (). Split () # display type for i in lis: print (type (I)) print () print (lis) print () enter multiple numbers at the same time with input ()

With map (), split () and input (), you can input multiple numbers at the same time with input ().

1.map ()

The map () function takes two arguments, one is the function and the other is the sequence. Map acts the passed function on each element of the sequence in turn and returns the result as a new list.

2.split ()

Split the string. Slice the string by specifying the delimiter and return the list of segmented strings (list)

3.map (function,input ("separated by spaces") .split ()

Because input () outputs strings separated by spaces, split () splits the values and puts them into the list. At this time, the values in the list are strings, and if you want to use int () or float () in map (), you must use processing such as int () or float () to assign values. If all the values you need to assign are strings, there is no need to use the map function.

The sample code is as follows

Split () # at this time, split b is str.aMagne b = map (int,input ('enter ameme b separated by spaces:'). Split () # at this time, the above type of split b is about the content of the article "how to enter multiple numbers in one line and store them in the list". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report