Array list from the user - python-2.7

i'm newbie here. And, i still python beginner.
I'm really stuck for answer one "Quiz" at my study place.
Maybe has been 2 weeks, I still failed answer this quiz.
Here we go..
"Make a script loop and produce array list from the user...
Like this.
$ python blablabla.py
input: 3
[1, 2, 3]
input: 2
[2, 4, 3]
input: 6
[3, 6, 6, 4, 5, 6]
input: 1
[4, 6, 6, 4, 5, 6]
input:1
[5, 6, 6, 4, 5, 6]
Can explain that? that list not created by myself. Must produce by the user.
Thanks before master.
Note: I'm using Python 2.7

something like this might work?
#! /usr/bin/python
list = []
while True:
num = raw_input('please enter your number: ')
print("You've entered", num)
if len(list) < int(num):
for i in range(0, len(list)):
list[i]=list[i]+(i+1)
for i in range(len(list), int(num)):
list.append(i+1)
else:
for i in range(0, int(num)):
list[i]=list[i]+(i+1)
print list

Related

two list convert into one ndarray

Here is my problem,
import numpy
lst1 = [[1,2,3],[7,8,9]]
lst2 = [4,5,6]
lst1.extend(lst2)
print(len(lst1))
print(len(lst1[0]))
NewArr = numpy.asarray(lst1)
print(NewArr.shape)
print ("List:", lst1)
print ("Array: ", NewArr)
run this code, it print result :
5
3
(5,)
List: [[1, 2, 3], [7, 8, 9], 4, 5, 6]
Array: [list([1, 2, 3]) list([7, 8, 9]) 4 5 6]
but I want the result look like this:
5
3
(5,3)
List: [[1,2,3,4,5,6],[7,8,9]]
Array: [list([1,2,3,4,5,6]) list([7,8,9])]
could someone help me please?
Your are extending the whole list, not the first element of the list.
You should write:
lst1[0].extend(lst2)
Which gives you your result:
list([list([1,2,3,4,5,6]) list([7,8,9])])
However, your first 2 print-statements should also be:
print(len(lst1[0])) # 5
print(len(lst1[1])) # 3

How to print out lists in a list

Note: code created in python 2.7
I am taking a robotics class in school and we are learning python.
The task was to take
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
and print it out as such
1
2
3
4
5
6
7
8
9
When I tried my code out I got the error message
I tried looking at what this means online but I am just starting to learn python and I didn't understand any of the answers that I found.
What am I doing wrong? How do I fix my code?
First Post sorry about the mess
Reason why you get an error:
You get the error in the line
results.append(lists[numbers[each_list]])
because numbers is an integer and numbers[each_list] isn't a valid function. So instead use the square brackets correctly:
results.append(lists[numbers][each_list])
Other Methods:
You don't really need to use range function:
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
for i in n:
for j in i:
print(j)
or a one liner:
print('\n'.join(str(j) for i in n for j in i))
Or if you are flattening a nested list:
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flat(lis):
res = []
for i in lis:
for j in i:
res.append(j)
return res
flat_n = flat(n)
or a one-liner:
>>> n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
>>> flat = lambda x: [j for i in x for j in i]
>>> flat(n)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
or just simply:
flat_n = [j for i in n for j in i]
n=[[1,2,3],[4,5,6,7,8,9]]
def flatten(lists):
results=[]
for lists in n:
for numbers in lists:
results.append(numbers)
return results
new_list =(flatten(n))
for num in new_list:
print (num)
When Executed
1
2
3
4
5
6
7
8
9
>>>

Create a function genSet() that takes in a list of numbers and returns a sorted set in python

I am new to python and trying to solve this example on pyschool
I need to write a function,
a) that takes a list of numbers
b) removes duplicates from the list
c) returns a sorted set:
In python, example :
>>> genSet([5,4,8,4,9,8])
[4, 5, 8, 9 ]
>>> genSet([3,-2,-1,-1,3,-2,0])
[-2, -1, 0, 3 ]
Removing Duplicates:
>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> list(set(t))
[1, 2, 3, 5, 6, 7, 8]
>>> s = [1, 2, 3]
>>> list(set(t) - set(s))
[8, 5, 6, 7]
Sort the List:
>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
Now you could write your custom function, that removes duplicates and sort.
The following code snippet works:
def genSet(clist):
t = list(set(clist))
return sorted(t)
if __name__ == "__main__":
print genSet([5,4,8,4,9,8])
print genSet([3,-2,-1,-1,3,-2,0])
If you want to iterate over multiple list try this:
a = [
[5,4,8,4,9,8],
[3,-2,-1,-1,3,-2,0]
]
for aa in a:
print genSet(aa)
set will automatically remove duplicates and sorted will sort the list.
def genSet(l):
return (sorted(set(l)))

How to get the following two list?

Hello I am working with some lists I have the following list:
a = [1,2,3,4,5,6]
I would like to get the these two lists from a:
b = [2,3,4,5,6]
c = [1,2,3,4,5]
I would like to get the firt one removing the first element of a and the second one removing the last element of a, I tried:
b = a
c = a
b.pop(0)
c.pop(len(a)-1)
print(b)
print(c)
print(a)
However the output is:
[2, 3, 4, 5]
[2, 3, 4, 5]
[2, 3, 4, 5]
that is affecting my fist list, I am not sure about what I am doing, I would like to appreciate support with this.
You should not modify the original list - it's simpler than you think, just slice the input list passing the right indexes. Try this:
a = [1, 2, 3, 4, 5, 6]
b = a[1:]
c = a[:-1]
a
=> [1, 2, 3, 4, 5, 6]
b
=> [2, 3, 4, 5, 6]
c
=> [1, 2, 3, 4, 5]
I think slicing would be ok:
b =a[1:];
c =a[:-1]

Splitting Lists In Python

How can I go about splitting a list into 2 separate lists based on every 5 numbers. This is what im trying to get it to look like.
list = [a,a,a,a,a,b,b,b,b,b,c,c,c,c,c,d,d,d,d,d]
newlista = [a,a,a,a,a,c,c,c,c,c]
newlistb = [b,b,b,b,b,d,d,d,d,d]
Ive been looking at itertools, not sure if im on the right path.
You can do this with list comprehension and slices:
In [1]: a, b, c, d, = 1, 2, 3, 4
In [2]: l = [a,a,a,a,a,b,b,b,b,b,c,c,c,c,c,d,d,d,d,d]
In [3]: [l[i:i+5] for i in range(0,15,10)]
Out[3]: [[1, 1, 1, 1, 1], [3, 3, 3, 3, 3]]
In [4]: [l[i:i+5] for i in range(5,20,10)]
Out[4]: [[2, 2, 2, 2, 2], [4, 4, 4, 4, 4]]