Printing multiple lists, vertically and formatted, in Python [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Let's say I have four lists:
l1=[3,5,6]
l2=[0,2]
l3=[3,4,3,1,2]
l4=[2,3,2]
And I want to print them like this:
2
1
6 3 2
5 2 4 3
3 0 3 2
Can anyone help me, please?

If you are using Python 2.x you can use izip_longest() from itertools:
for i in reversed([' '.join(map(str,i)) for i in izip_longest(l1, l2, l3, l4, fillvalue=' ')]):
print i
In Python 3.x you can use zip_longest() from itertools.
Output:
2
1
6 3 2
5 2 4 3
3 0 3 2

you may be able to solve this problem by using multi-dimensional arrays to store the lists. then cycling through them checking for values to display while using an if statement to print a space if there is no value currently residing there (aka initialize the array to none where no value resides). I believe that if you structure it like that you may have some success

Related

need to make this into a recursive function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
i need to convert this over to a recursive function. the program basically prints out all variations of a string.
def comb(L):
for i in range(3):
for j in range(3):
for k in range(3):
# check if the indexes are not
# same
if (i!=j and j!=k and i!=k):
print(L[i], L[j], L[k])
# Driver Code
comb([1, 2, 3])
output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
This might get you started:
Every permutation of a list can be made by picking a value from the list & putting it at the front of every permutation of what is left in the list after taking that value out. (Note that the "what is left" part is a smaller list than what you started with.)
When your list is small enough, it is the only permutation.

Is there any way to find special symbols like '?','*','NA' in a data frame containing ten thousand instances using python coding? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how do i find Special characters in a data frame using python code ..and also find locations of them. for this am using regular expressions in python but i didn't get anything..
Want to display text for each columns if it contains special characters.
considering the below as the input dataframe, I have performed the operations.
name val
0 a 1
1 * 2
2 b NaN
3 d 1
4 g ?
5 t 3
6 h 4
import numpy as np
import pandas as pd
f=pd.read_csv('data_file.csv')
f=pd.DataFrame(f)
#the below loop will provide you with the location of null values. i.e. "NaN"
for i,j in zip(*np.where(pd.isnull(f))):
print("{},{}".format(i,j))
o/p: 2,1
#similarly finding location of '*' and '?'
special=['*','?']
for k in special:
print(np.where(f.applymap(lambda x: x == k)),'location for ',k)
o/p:
(array([1], dtype=int64), array([0], dtype=int64)) location for *
(array([4], dtype=int64), array([1], dtype=int64)) location for ?
eg:
i/n: f.iloc[1,0]
o/p: '*'

What is the most effiecient way to get Factorial Sum? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
The code is to find the factorial of each individual value in an array and then find the sum of them together .An example would be [1,2,3,4], which will then be 1!+2!++3!+4!=33.A single integer equal to the desired sum, reduced modulo . The issue is that when it comes on to large numbers(just my assumption) it results in "Terminated due to timeout" status
At first I used a for loop to go through each value in the array. Thinking that it may be a search issue I used for in range to ensure it has a set range. Sadly that still hasn't solved the problem. I now assume that it has to be a problem with factorial since multiplication is
def factModSum(arr):
sum=0
for i in range (0,len(arr)):
sum=sum+factorial(arr[i])
return sum%(10**9)
Example 1:
Input: 1 2 3 4
output: 33
Expected output: 33
Example 2:
Input:2 3 5 7
Output:5168
Expected output: 33
Example 3:
Input:12 13 14
Output:884313600
Expected output: 33
At the core of it at the function works. But Im getting timeout error for some of my Test case , therefore assuming that the code is not able to process large numbers in a given time
If you are modding by 10 ** 9 you can try this:
def factModSum(arr):
return sum(factorial(i) for i in arr if i < 40) % 10**9
This is because n! with n >= 40 is congruent to 0 mod 10**9.

string in a sequence in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am solving a problem in codechef which is
Chef has a sequence of N numbers. He like a sequence better if the sequence contains his favorite sequence as a substring.
Given the sequence and his favorite sequence(F) check whether the favorite sequence is contained in the sequence
Input
The first line will contain the number of test cases and are followed
by the cases. Each test case consists of four lines: The length of
the sequence, the sequence N,the length of F and the sequence F
Output
Print "Yes" if the sequence contains the favourite sequence int it
otherwise print "No" Constraints
1<=T<=10
1 1
Input:
2
6
1 2 3 4 5 6
3
2 3 4
6
22 5 6 33 1 4
2
4 15
Output:
Yes
No
to this I wrote
`
for _ in xrange(int(raw_input())):
raw_input()
s = raw_input()
raw_input()
f = raw_input()
print "Yes" if f in s else "No"`
it returns correct result (as far as I have checked ) bu the grader returns wrong. why is this wrong ?
Imagine a scenario where the sequence is '12 3 4' and the subsequence is '2 3 4'. Your code will return True since '2 3 4' in '12 3 4' is True. You need to convert the sequence and subsequence to integers before doing the comparison.

predict the outcome numbers using a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 3 lists with 3 sets of integers. I want to predict the outcome numbers using a list or by whatever other means is out there.
List1 = [0 0 2 0 1 2]
List2 = [3 0 1 0 1 0]
List3 = [0 0 2 1 1 1]
Im working in the first column, I was thinking of using a double if statement something like:
if 3 is in spot 0 of any list
and 0 is in spot 0 of any list
print 2
The other thing is I want to do this for all the other columns as well.
I know this is not complete but its on my mind like this and ive been
searching around and found nothing to solve this problem.
I would greatly appeciate any response to this question.
You cant define a list delimited with space; you have to use a comma',' instead.
l1=[0,0,2,0,1,2]
l2=[3,0,1,0,1,0,]
l3=[0,0,2,1,1,1]
spot0_list = zip(l1,l2,l3)[0]
if 3 in spot0_list and 0 in spot0_list:
print 2
And, if there are too many lists, you can use set:
spot0_list = set(zip(l1,l2,l3)[0])

Categories