How to input arrays of numbers in one line(not one input in each line) for a given number of n elements and make a list in Python? [duplicate] - python-3.x

This question already has answers here:
Get a list of numbers as input from the user
(11 answers)
Closed 4 years ago.
N=5
Input: 12345
Output: [1,2,3,4,5]

No need for map or lambda, you can just use the list() function or list comprehension;
an_array = input()
>>> 12345
print([int(x) for x in an_array])
>>> [1, 2, 3, 4, 5]

Iterate through input string (using For loops) and create array (using List).

Related

Query on Slicing a list in Python [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 days ago.
How do I print the reverse of a specific selection of a list ?
For example, I have a list,
a = [11,22,33,44,55,66,77,88]
I expect a[1:4:-1] to print [44,33,22], but it gives an empty list.
I have seen Understanding slicing, but I couldn't find an answer.
The python slicing syntax, when 3 parameters are provided, is [start:stop:step]
In order to get the value [44, 33, 22], you'd need to write the slice like this: a[3:0:-1]
This works because the slice starts at index 3, which has a value of 44, and the slice stops before index 0, meaning the last included index is 1 (with a value of 22), based on the step value of -1.

How to make program print a string for every item in a numerical list according to the value of the item in the list? [duplicate]

This question already has answers here:
Repeat string to certain length
(15 answers)
Closed 10 months ago.
numbers = [5, 2, 5, 2, 2]
for item in numbers:
print("...")
This just prints the string for each item in the list, but I don't know how to make it print said string by the number of times as the item in the list.
In python you are allowed to use multiplication (*) between an integer and a string which should do what you want.
numbers = [5, 2, 5, 2, 2]
for item in numbers:
print(item * "...")
Result is
...............
......
...............
......
......

Python 3: how to re-order sublists of nested list? [duplicate]

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 2 years ago.
I have the following nested list, and the number of sublist in the list:
l1 = [[['a','c','d'],['e','f','g'],[['a','b'],'d','1']], [['2','3','4'],['3','4','4'],[['1','2'],'3','4']], [['q1','3e','2e'],['r4','tt','t5'],[['t4','g4'],'r4','45g']]]
nb_sub = 3
I want to re-order the sublists by 'index', so the 1st sublist of each sublist, then 2nd sublist of each sublist, etc. The ouput I want is:
output = [[['a','c','d'],['2','3','4'],['q1','3e','2e']], [['e','f','g'],['3','4','4'],['r4','tt','t5']], [[['a','b'],'d','1'],[['1','2'],'3','4'],[['t4','g4'],'r4','45g']]]
zip seems to be the perfect tool for the job:
output = [x for x in zip(*l1)]

Sorting a list of strings items from an array [duplicate]

This question already has answers here:
Is there a built in function for string natural sort?
(23 answers)
Closed 3 years ago.
I'm new to python automation and wrote a script to get some port handles from Ixia and store into a list. I;m trynig to sort that port-handle where I see a problem.
I tried using the sort method but doesn;t work
>>> a
['1/1/11', '1/1/6']
>>> a.sort()
>>> a
['1/1/11', '1/1/6']
>>> d = a.sort()
>>> print(d)
None
>>>
Am i missing anything here .. kindly clarify
I want the output in the following format
1/1/6 1/1/11
Explanation
You are trying to sort a list of strings. Strings are naturally sorted in lexicographical_order, i.e. "10" < "11" < "2" < "5" < ..., so Python executes correctly what you want it to do. This being said, you need to transform your data into something that will be sorted as you want.
Solution
>>> a = ['1/1/11', '1/1/6']
>>> a
['1/1/11', '1/1/6']
>>> def to_tuple(string_representation):
... return tuple(int(i) for i in string_representation.split('/'))
...
>>> b = [to_tuple(element) for element in a]
>>> b.sort()
>>> b
[(1, 1, 6), (1, 1, 11)]
>>> a.sort(key=to_tuple)
>>> a
['1/1/6', '1/1/11']
Here we use the fact that tuple is sorted by default exactly how we want it to be sorted in your case (actually, it is also a lexicographical order, but now 11 is one element of a sequence and not two).
List b contains a transformed list, where each element is a tuple. And now sort will work as you want.
The second option, will be using a custom key operator. It is a function that returns a key to compare different elements of your list. In this case, key will be a corresponding tuple of integers and will be compared as you want.
Note 1
The second approach (with the key operator) will create an additional overhead during sorting as it will be called O(NlogN) times.
Note 2
You also tried using the result of sort function as a value, but it changes the given list in-place. If you want a sorted copy of your list, use sorted.

query on Python list comprehension [duplicate]

This question already has answers here:
Modify a list while iterating [duplicate]
(4 answers)
Can't modify list elements in a loop [duplicate]
(5 answers)
Closed 4 years ago.
I tried:
a = [0,1, 2, 3, 4, 5]
this works fine:
>>> for q in a:
... print(q)
...
0
1
2
3
4
But, if I try to change the (mutable) list objects like so, it doesn't work:
>>> for q in a:
... q = 0
...
>>> a
[0, 1, 2, 3, 4]
It seems to me that each time through the loop, q refers to a[n], as n ranges from 0 to 4. So, it would seem, setting q = 0 ought to make the list all value zero.
Any suggestions that are quite Pythonic?
Updating iterating variable inside loop does not itself update the iterator you iterate through.
for q in a:
q = 0
You are just modifying value of q, but do not touch the original list a.
To make all elements in list to 0, use a list-comprehension:
a = [0 for _ in a]

Resources