Print different size lists side by side [duplicate] - python

This question already has answers here:
Is there a zip-like function that pads to longest length?
(8 answers)
Zipping lists of unequal size
(5 answers)
Closed 6 years ago.
I want to print different size lists side by side.
I'm using
In: for n,g in zip(ten_pos,real_pos):
print (n + "\t\t\t\t\t\t" + g)
But If one list has 5 item and the other one has 20, It only prints 5 and 5 and I want 5 and 20.
Is there an easy way to solve this?

Check out itertools.zip_longest.
import itertools
for a, b in itertools.zip_longest([1,2], [3,4,5]):
print(a, b)
# outputs
1 3
2 4
None 5
You can change the value used to fill with the kwarg fillvalue.

Related

How to split a list created by a for loop into two separate lists based on a condition? [duplicate]

This question already has answers here:
List comprehension with condition
(2 answers)
How can I partition (split up, divide) a list based on a condition?
(41 answers)
Closed 21 days ago.
I used this code to make a list of float values ranging from 0 to 1:
whole_list = [random.random() for n in range(count)]
I want to group them into two lists big_list and small_list, so that big_list contains the values from whole_list that are greater than 0.5, and small_list contains the values less than 0.5
How can I do this in Python?
UPDATE: Solution is as follows:
for n in whole_list:
if n > cutoff:
big_list.append(n)
else:
small_list.append(n)
print(big_list)
print(small_list)

Can I use end=' ' in input() to take an input with spaces but not a new line? [duplicate]

This question already has answers here:
How to stop the input function from inserting a new line?
(9 answers)
How to convert N space separated numbers into in array in Python? [closed]
(2 answers)
Closed 11 months ago.
I want the input to be like
2 3 4 5
and not like
2
3
4
5
Is there any way to do it by adding end='' or some other way in the input() function?
This is part of a recursive function called makearray(). I want it to take spaced inputs and not on new lines.
l = int(float(input("Enter number in array 1:"end='')))
if (l > 0):
global p
p = np.append(p, l * l)
else:
pass
makearray(b - 1)

total length of list by summing length of each element [duplicate]

This question already has answers here:
Python: Sum string lengths
(8 answers)
Closed 3 years ago.
I have a small question.
I am trying to find the the amount of letters in my list.
list = ['Chris', 'jan']
len(list)
would give me then 2, because there are 2 elements in my list.
Is their a possibility to calculate the amount of letters in my list? So the outcome would be 8 (length of the first element + length of the second element)?
Thank you!
You can just map the len function over the list to apply it to each element and then sum the resulting lengths' list
a = ['Chris', 'jan']
>>> sum(map(len, a))
8
Alternatively, you can also join the list of strings into a single string and get its length
>>> len(''.join(a))
8
You can sum the length of each word.
sum(len(w) for w in l)
You could also do:
len("".join(list))

find the index i of a sorted array y[:], such that y[i-1] <= x <y[i] [duplicate]

This question already has answers here:
Check between which floats in a list a given float falls
(2 answers)
Closed 4 years ago.
getindex(x,y)
Input: a value x, and a sorted array y[:] (no repeat element)
Output: the index i, such that y[i-1] <= x <y[i]
The time complexity should be O(log(N))
Is there a Python/Numpy function we can use?
For example:
y[0]=-0.2
y[1]=1.5
y[2]=1.9
y[3]=3.2
Then
getindex(-4.0,y) returns 0
getindex(0.5,y) returns 1
getindex(6.0,y) returns 4
numpy.searchsorted
I find it here:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.searchsorted.html

Pythonic way to check if at least one number of a list is in a tuple [duplicate]

This question already has answers here:
How to check if one of the following items is in a list?
(14 answers)
Closed 6 years ago.
I have one tuple and one list:
t = (1, 2, 9)
l = range(2, 8)
and I want to check if there's at least one element of the list is in the tuple. I tried to use:
if (2 or 3 or 4 or 5 or 6 or 7) in l:
return True
but it works only for the number 2.
Now I'm using:
if 2 in l or 3 in l or 4 in l or 5 in l or 6 in l or 7 in l:
return True
but I think that's not the best way to do it.
Is there a way to shrink this piece of code to make it more elegant?
You simply want:
any(e in t for e in l)

Categories

Resources