How to take list input in Python in single line | Example code
文章推薦指數: 80 %
To take list input in Python in a single line use input() function and split() function. Where input() function accepts a string, integer, ... HowtotakelistinputinPythoninsingleline|ExamplecodebyRohitDecember11,2021December11,2021 TotakelistinputinPythoninasinglelineuseinput()functionandsplit()function.Whereinput()functionacceptsastring,integer,andcharacterinputfromauserandsplit()functiontosplitaninputstringbyspace. ExampletakelistinputinPythoninasingleline Simpleexamplecode. lst=input('Typeseparatedbyspace:').split() #printlist print('list:',lst) Output: Howtoreadanarrayofintegersfromasinglelineofinputinpython3? Answer:Usemap()functionalongwithinputandsplitfunction.UsinginttoReadanarrayofintegersonly. Thefollowingsnippetwillmapthesinglelineinputseparatedbywhitespaceintoalistofintegers. lst=list(map(int,input("Typenumberwithspace:").split())) #printlist print('list:',lst) Output: Typenumberwithspace:123list:[1,2,3] DocommentifyouhaveanydoubtsorsuggestionsonthisPythonlistinputtopic Note:IDE: PyCharm 2021.3(CommunityEdition)Windows10Python3.10.1All PythonExamples arein Python 3,soMaybeitsdifferentfrompython2orupgradedversions. RohitDegreeinComputerScienceandEngineer:AppDeveloperandhasmultipleProgramminglanguagesexperience.Enthusiasmfortechnology&likelearningtechnical. Sharethis:FacebookWhatsAppLinkedInMoreTwitterPrintRedditTumblrPinterestPocketTelegramSkypeEmail Related Tags:PythoninputPythonList LeaveaReplyCancelreplyYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Name* Email* Website Comment*Notifymeoffollow-upcommentsbyemail.Notifymeofnewpostsbyemail. Δ ThissiteusesAkismettoreducespam.Learnhowyourcommentdataisprocessed. SearchTopics Searchfor... RecentPosts javascriptvoid(0)inhref VoidfunctionreturnvalueisusedinJavaScript JavaScriptvoidoperator FormactionJavaScriptvoid FormactionJavaScript|attribute Categories Android AndroidInterviewQuestions AndroidKotlin AndroidUI C CSS Datastructure HTML Info Java JavaScript Kotlin MaterialDesign Memes Python Tool TypeScript Archives Archives SelectMonth August2022 July2022 June2022 May2022 April2022 March2022 February2022 January2022 December2021 November2021 October2021 September2021 August2021 July2021 June2021 May2021 April2021 March2021 February2021 January2021 December2020 November2020 October2020 September2020 August2020 July2020 June2020 May2020 April2020 March2020 February2020 January2020 December2019 November2019 October2019 September2019 August2019 July2019 June2019 May2019 April2019 March2019 February2019 January2019 December2018 November2018 October2018 September2018 August2018 July2018 June2018 May2018 September2016 August2016 April2016 March2016 December2015 Subscribe EmailAddress Subscribe LoadingComments... WriteaComment... Email(Required) Name(Required) Website
延伸文章資訊
- 1How To Input A List In Python - Edureka
This article will introduce you to different ways to input a list in Python and give you a detail...
- 2新手python關於input不定個數的list的問題- 課程板 - Dcard
初學python,練作業遇到背包問題(最佳化問題) 題目會給定不定行數的list做比較。 想請問一下該如何使用迴圈(while或for)input不定行數的list並將 ...
- 3Python Accept List as a input From User - PYnative
- 4Python Get a list as input from user - Tutorialspoint
We can also use input function twice so that we can create a list of lists. Use the range functio...
- 5Get a list of numbers as input from the user - python
In Python 3.x, use this. a = [int(x) for x in input().split()]. Example. >>> a = [int(x) for x in...