how to input list [] dynamic in python

48 views
Skip to first unread message

paidjoo indo

unread,
May 31, 2021, 7:22:01 AM5/31/21
to django...@googlegroups.com
hello i having question, because i still learn python for me, so can help me 
the question is

how about if input like in below
1 ) input: 3 , return [1, 2, 3]

2) input: 2, return [2, 4, 3]

3) input: 6, return [3, 6, 6, 4, 5, 6]

4) input: 1, return [4, 6, 6, 4, 5, 6]

5) input: 1, return [5, 6, 6, 4, 5, 6]

thanks in advance

paidjoo indo

unread,
May 31, 2021, 7:34:51 AM5/31/21
to django...@googlegroups.com
how about if input like in below
1 ) input: 3 , return [1, 2, 3]
then continue dynamic

2) input: 2, return [2, 4, 3]
  then continue dynamic  

3) input: 6, return [3, 6, 6, 4, 5, 6]
  then continue dynamic  

4) input: 1, return [4, 6, 6, 4, 5, 6]
  then continue dynamic  

5) input: 1, return [5, 6, 6, 4, 5, 6]

thanks

Derek

unread,
May 31, 2021, 9:40:38 AM5/31/21
to Django users
This does look a programming exercise in a class...  if you ask these questions in future, be warned people here will probably ask to see your attempt at writing code first (otherwise you are not really learning to be a programmer).

But, OK, it seemed interesting to me,so here is one approach:

def elementswise_left_join(l1, l2):
    f_len = len(l1) - (len(l2) - 1)
    for i in range(0, len(l2), 1):
        if f_len - i > len(l1):
            break
        else:
            l1[i] = l1[i] + l2[i]
    return l1
    

def get_array(n):
    if n == 1:
        return [1,]
    r = [x for x in range(1, n + 1)]
    return r


curr = []
valid = True
while valid:
    num = input("Enter number: ")
    if num:
        new = get_array(int(num))
        if len(new) > len(curr):
            curr = elementswise_left_join(new, curr)
        else:
            curr = elementswise_left_join(curr, new)
        print(curr)
    else:
        valid = False

paidjoo indo

unread,
Jun 8, 2021, 4:30:02 AM6/8/21
to django...@googlegroups.com
btw thanks you very much, Mr Derek and all 

yes that is exercise basic in python, i'm newbie in python and i hope learn djanggo, before i have try code looping

lst = []

# number of elemetns as input
n = int(input("Enter number of elements : "))
 
# iterating till the range
for i in range(0, n):
    ele = int(input())
 
    lst.append(ele) # adding the element
     
print(lst)

test_list1 = []
n2 = int(input("Enter number of elements2 : "))

for i2 in range(0, n2):
    ele2 = int(input())
 
    test_list1.append(ele2) # adding the element

print(test_list1)

res_list = [lst[i] + test_list1[i2] for i in range(len(lst))]

print(res_list)


but still wrong, about exercise in below , how if without 

1) def elementswise_left_join(l1, l2) and 
2) def get_array(n):

can someone give me again ? thanks very much

the exercise same, in below here :
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3a5ee884-4d66-4fc3-8217-64658d7f585dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages