predict the outcome numbers using a list [closed] - python

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])

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.

Count how many sentences there are in each row within a pandas column [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
I am trying to determine how many sentences there are in each row.
Sent
I went out for a walk.
I don't know. I think you're right!
so boring!!!
WTF?
Nothing
I created a list of punctuation symbols I am interested in for determining the number of sentences per each row:
Output
1
2
1
1
1
In order to get this result, I first considered to split each row whether I met a symbol (for instance . or ! or ?). But I do not know how to get the count.
My code is
import re
def sentence(sent):
return re.findall('[\w][\.!\?]', sent)
df['Sent'] = df['Sent'].apply(sentence)
Could you please give my advice on how to get it?
One idea if dont need last value like 1 use Series.str.count with regex for match letter with escaped .!?:
df['Output'] = df['Sent'].str.count('[\w][\.!\?]')
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 0
If need replace 0 by 1:
df['Output'] = df['Sent'].str.count('[\w][\.!\?]').clip(lower=1)
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 1
Another idea is use textstat lib:
import textstat
df['Output'] = df['Sent'].apply(textstat.sentence_count)
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 1

How to iterate through two dictionary objects (both key,value) using two nested for loops in python? [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
def multpoly(d1,d2):
d3=[]
d1=dict(d1)
d2=dict(d2)
for key,value in list(d1.items()):
for key2,value2 in list(d2.items()):
#print(key,value)
print(key2,value2)
print(key,value)
d1={1:1,-1:0}
d2={1:2,1:1,1:0}
wrong output:
1 0
1 1
1 0
-1 0
Expected:
1 2
1,1
1,0
1,1
-1,0
I am getting wrong output with this code?
Can anyone help me what is wrong with this code?
This is wrong with your code: dicts can only contain each key once.
If you specify
d1={1:1,-1:0}
d2={1:2,1:1,1:0}
You get dict d1 with contains keys 1 and -1 with its values and you get dict d2 wich contains only 1 as key (with value of 0 as this one was specified last).
Test:
d1={1:1,-1:0}
d2={1:2,1:1,1:0}
print(d1) # {1: 1, -1: 0}
print(d2) # {1: 0} - both of 1:2,1:1 were overwritten by the last key 1:0 spec

Printing multiple lists, vertically and formatted, 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 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

Python compare rows and columns of a table [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 8 years ago.
Improve this question
I am looking for an efficient way to compare rows and columns of a table against each other (>= gets 1, otherwise 0) and store the result.
Example:
0.3642286 0.7945753 0.3527125
0.3642286 1 1 0
0.7945753 0 1 0
0.3527125 1 1 1
I have 21 tables with 480*480 rows and columns. What would be a proper way of generating and storing such a matrix?
All you really need is two loops.
def compare(first, second):
result = []
for x in first:
result.append([])
for y in second:
result[-1].append(1 if x >= y else 0)
result = [list(i) for i in zip(*result)]
return result
You might consider NumPy (1) if you are regularly handling large multi-dimensional arrays.

Categories