4

Total newb to Python here. I'm working on CodeAbbey's problems using Python 3, and I'd like help to make the code for user input shorter.

Let's say I want to get this input from the user:

3
2 3
4 5
6 7

First line is number of cases, and each of the following lines are the cases themselves with 2 parameters. I've figured out to do it in this way so far:

N=int(input('How many cases will you calculate?\n'))
print('Input parameters separated by spaces:')
entr = [list(int(x) for x in input().split()) for i in range(N)]

The thing is I'd rather to ask all the input in the list comprehension, and then assign N=entr[0]. But how do I get the list comprehension to break the input into lines without using range(N)?

I tried:

entr = [list(int(x) for x in input().split()) for x in input()]

but it doesn't work.

MPadilla
  • 69
  • 1
  • 1
  • 7
  • 2
    Don’t use list comprehensions when they make it harder for you to make it work. Use normal loops. – poke Jul 26 '15 at 22:30
  • *“The thing is I'd rather to ask all the input in the list comprehension”* Why? Shorter isn’t always better. What you have already is clearer. – Ry- Jul 26 '15 at 22:33
  • @ minitech♦ I don't know, well... I just suppose that for the user it would be more comfortable to just paste the input in 1 go instead of: Ask length, ask cases. – MPadilla Jul 26 '15 at 22:38
  • @poke How would you use a normal loop here? – MPadilla Jul 26 '15 at 22:43
  • Just loop while getting input from the user and collect the numbers in a list. Don't make it complicated but just simple. – poke Jul 26 '15 at 22:52

4 Answers4

7

I don't see the benefit of doing this in a list comprehension, but here is a solution that allows all data to be copy-pasted in:

entr = [list(int(x) for x in input().split())
        for i in range(int(input()))]
N = len(entr)

Your solution was pretty close. The outer iteration just needed to be given something to iterate on (using range()) rather than a single number.

vinod
  • 2,358
  • 19
  • 26
  • What does 'for i in range(int(input()))' do? I mean I understand that range helmakes iteration possible but what does int(input()) do with the data? By the way, thank you some much for this piece of code. Thank you. – MPadilla Jul 26 '15 at 23:50
  • That bit was in your original code as well, to obtain `N`. `input()` prompts the user to enter a line of input. `int()` converts the data the user entered into a number (so `"3"` becomes `3`). `range(n)` creates a sequence of numbers from `0` to `n-1` (see [here](http://stackoverflow.com/a/30081318/827437) for more info on `range()` magic). – vinod Jul 27 '15 at 00:14
0

Yeah you can try this in the list comprehension

cases = [input().split() for _ in range(int(input()))]
flatList = [int(item) for elem in cases for item in elem]
print(flatList)
Darkstar Dream
  • 1,649
  • 1
  • 12
  • 23
0

I simply used this for python list input

n = int(input())
a = [list(map(input().split())) for i in range(n)]
print(a)
'''
3
2 3
4 5
6 7
[[2, 3],[4, 5], [6,7]]
'''

arr = []
for i in range(int(input())):
    a = list(map(int,input().split()))
    arr.append(a)
print(arr)

'''
3
2 3
4 5
6 7
[[2, 3],[4, 5], [6,7]]'''

''' or in single list '''
arr = []
for i in range(int(input())):
    a,b = map(int,input().split())
    arr += [a,b]
print(arr)

'''
3
2 3
4 5
6 7
[2,3,4,5,6,7]'''
    

Im newbie, how can i use this editor for python output console?

BitchBark
  • 3
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 08 '22 at 17:48
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30767571) – PKo Jan 11 '22 at 09:14
-1

Here input is taken as int . But you can change it acc. To your need of input. And 4 will be the no. Of inputs you insert that can also edit acc. To your need

enter = list(i for i in (int(input() for i in range(4)))
print(enter)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 14 '22 at 12:00