0

Im writing a program that reads in a list, then displays the length of the list, and displays the list with one number displayed per line. This is what I have so far:

from List import *

def main():
   numbers = eval(input("Give me an list of integers: "))
   strings = ArrayToList(numbers)
   length = len(strings)
   print("The length of the list: ", length)

This is what Im expecting the result to be like:

Enter a list of integers: [3, [5, [1, [6, [7, None]]]]]
The length of the list:
5
The list:
3
5
1
6
7

Can anybody help me out? Im getting the length of the list to show as 2 instead of 5 which is what it should be.

  • 3
    Odd that the prompt changed when you ran your code ... ;) – FatalError May 01 '14 at 18:35
  • 4
    Do you come from the world of PROLOG? it's very odd to handle lists the way you do, in python – inspectorG4dget May 01 '14 at 18:35
  • 2
    this is the most bizzarre list notation I have seen ... – Joran Beasley May 01 '14 at 18:37
  • A list of those five integers would be written `[3, 5, 1, 6, 7]` in Python. What you've entered is a two-element list in which the second element is another two-element list etc. – jwodder May 01 '14 at 18:37
  • @JoranBeasley: it's PROLOG lingo. It's how list concatenation is handled, which is how larger lists are made. Interestingly, to port over PROLOG, that last `None` should be a `[]` – inspectorG4dget May 01 '14 at 18:38
  • 1
    a/ don't use eval. b/ don't use prolog notation c/ yes, your list has a size of 2, not 5, you'll have to flatten it first – njzk2 May 01 '14 at 18:41
  • 2
    What's ArrayToList? Is this another [CS 150](http://stackoverflow.com/questions/23006458/command-line-input-extreme-and-map-patterns-python) question? – Kevin May 01 '14 at 18:43
  • @inspectorG4dget why do you keep all-capsing Prolog? :) It seems to me to have its roots in a term algebra representation of a list, but it would make more sense with list(item1,list(item2,nil)) so it thus can be a straightforward representation of a list. – C.B. May 01 '14 at 18:47
  • @C.B.: I thought the all-capsing was an artifact of nomenclature. Apparently, I was wrong – inspectorG4dget May 01 '14 at 19:10
  • http://ideone.com/FNalet – Joran Beasley May 01 '14 at 20:40

4 Answers4

4

Without seeing the implementation of ArrayToList, this is just a guess.

That said, I imagine your problem is that you aren't entering a list of integers, you're entering a list composed of an integer and another list... which itself is a list containing an integer and another list, all the way down. Hence len(strings) is 2, because len is not recursive.

You're could try inputting the list like [1, 2, 3, 4, 5] instead.

Alternatively, you could build your list in a loop, asking for user input for each character until an "end of input" event (of your choosing) is hit. This would let you avoid eval entirely, which is often a good idea.

dckrooney
  • 3,041
  • 3
  • 22
  • 28
1
def main():
    numbers = input("Give me a list of space-separated integers: ").split()
    print("length of the list:", len(numbers))
    print("The list:", *numbers, sep='\n')

Output

In [14]: main()
Give me a list of space-separated integers: 3 5 1 6 7
length of the list: 5
The list:
3
5
1
6
7
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

I am assuming you are in Python3, because in Python2 you would not need the eval around input.

numbers = eval(input("Give me an list of integers: "))
length = len(numbers)
print("The length of the list: %d" % length)
print("The list: ")
for i in numbers:
  print(i)

Here, you would have to enter the list in standard Python fashion though:

[1,2,3,4,5]

The equivalent in Python2 would just be a one-line change.

numbers = input("Give me an list of integers: ")
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

In Python 2 I would do it like this:

def main():
    numbers = []
    integers = []
    numbers.append(raw_input("Give me an list of integers: "))
    for integer in numbers[0]:
        print integer
        if integer.isdigit():
            integers.append(integer)
    print ("The length of the list: ", len(integers))

main()
Maikflow
  • 191
  • 1
  • 8