output not providing accurate results python - python

I am comparing a list with a list comprised of lists to find the most similar list and to use the string in the identified list in an if statement. I tried to compile the right code but it doesn't seem to provide me with the results i am looking for.
This is the code:
listA = [['1', '1', '1', '1', '1', '0', 'up', 423], ['1', '0', '1', '0', '1', '1', 'up', 430], ['0', '0', '1', '1', '0', '1', 'up', 398], ['0', '0', '0', '0', '1', '0', 'down', 507], ['1', '1', '0', '0', '1', '1', 'up', 392], ['1', '1', '1', '1', '0', '0', 'down', 169], ['1', '1', '0', '1', '1', '0', 'down', 149], ['0', '1', '0', '0', '1', '0', 'down', 397], ['1', '0', '1', '1', '1', '0', 'up', 387], ['1', '0', '1', '0', '1', '0', 'down', 448], ['1', '1', '1', '1', '0', '1', 'up', 433], ['0', '1', '1', '1', '0', '0', 'up', 355], ['0', '0', '0', '0', '0', '0', 'down', 477], ['1', '0', '0', '0', '0', '0', 'down', 484], ['0', '1', '0', '0', '0', '0', 'down', 520], ['0', '1', '1', '1', '0', '1', 'up', 500], ['0', '0', '0', '0', '1', '1', 'up', 167], ['0', '0', '1', '0', '1', '1', 'up', 373], ['1', '1', '1', '1', '1', '1', 'up', 328], ['0', '0', '1', '0', '0', '0', 'down', 557], ['0', '1', '0', '1', '0', '0', 'up', 203], ['0', '1', '0', '1', '1', '1', 'up', 519], ['1', '1', '1', '0', '1', '1', 'down', 15], ['1', '0', '1', '0', '0', '0', 'down', 498], ['0', '1', '1', '1', '1', '0', 'up', 481], ['1', '1', '0', '1', '0', '0', 'down', 359], ['0', '1', '1', '0', '1', '0', 'up', 414], ['1', '0', '0', '1', '0', '0', 'down', 510], ['0', '1', '0', '1', '0', '0', 'down', 403], ['0', '1', '1', '0', '0', '0', 'down', 360], ['0', '1', '1', '0', '1', '1', 'up', 494], ['1', '1', '1', '0', '1', '0', 'up', 374], ['0', '0', '1', '0', '0', '1', 'up', 149], ['0', '1', '1', '1', '0', '0', 'down', 131], ['0', '0', '0', '0', '1', '1', 'down', 326], ['1', '0', '1', '1', '1', '0', 'down', 179], ['1', '0', '0', '0', '1', '0', 'down', 530], ['1', '0', '1', '0', '0', '1', 'up', 163], ['0', '0', '0', '1', '1', '1', 'up', 338], ['1', '1', '0', '1', '1', '1', 'up', 427], ['1', '0', '0', '1', '0', '1', 'down', 422], ['1', '0', '0', '1', '1', '1', 'down', 134], ['0', '0', '1', '1', '1', '1', 'up', 443], ['1', '0', '1', '1', '0', '0', 'down', 386], ['1', '0', '1', '1', '0', '0', 'up', 144], ['1', '1', '1', '0', '0', '1', 'up', 356], ['0', '0', '0', '1', '0', '0', 'down', 513], ['0', '0', '0', '0', '0', '1', 'down', 497], ['0', '1', '0', '1', '0', '1', 'up', 424], ['1', '0', '1', '0', '1', '0', 'up', 200], ['1', '1', '0', '1', '0', '1', 'up', 369], ['1', '1', '0', '1', '0', '0', 'up', 149], ['1', '1', '0', '0', '0', '0', 'down', 492], ['0', '1', '0', '1', '0', '1', 'down', 164], ['1', '1', '0', '0', '1', '0', 'down', 413], ['1', '0', '0', '0', '1', '1', 'up', 144], ['0', '1', '0', '1', '1', '0', 'up', 399], ['0', '1', '1', '1', '0', '1', 'down', 16], ['0', '1', '1', '0', '0', '1', 'up', 385], ['1', '0', '0', '1', '0', '0', 'up', 28], ['0', '0', '1', '0', '1', '0', 'down', 406], ['1', '0', '0', '1', '1', '0', 'down', 393], ['1', '1', '0', '1', '0', '1', 'down', 156], ['0', '0', '1', '1', '0', '1', 'down', 157], ['0', '0', '1', '1', '0', '0', 'down', 383], ['0', '1', '1', '0', '1', '0', 'down', 163], ['1', '0', '0', '1', '1', '1', 'up', 347], ['0', '0', '0', '1', '0', '1', 'down', 372], ['1', '0', '0', '0', '1', '1', 'down', 385], ['1', '1', '0', '0', '1', '1', 'down', 143], ['0', '0', '1', '0', '0', '1', 'down', 355], ['0', '1', '1', '0', '0', '1', 'down', 111], ['1', '1', '0', '1', '1', '0', 'up', 367], ['0', '1', '0', '0', '1', '1', 'up', 386], ['1', '0', '1', '1', '1', '1', 'down', 20], ['1', '0', '0', '0', '0', '1', 'down', 494], ['0', '0', '0', '1', '1', '1', 'down', 162], ['0', '1', '1', '1', '1', '1', 'up', 430], ['1', '0', '0', '1', '1', '0', 'up', 152], ['0', '0', '0', '1', '1', '0', 'down', 358], ['1', '1', '1', '0', '1', '1', 'up', 393], ['1', '1', '0', '0', '0', '1', 'down', 357], ['0', '0', '1', '0', '0', '0', 'up', 28], ['0', '0', '1', '0', '1', '1', 'down', 134], ['0', '0', '1', '0', '1', '0', 'up', 179], ['0', '0', '1', '1', '1', '1', 'down', 22], ['1', '0', '1', '1', '0', '1', 'up', 377], ['0', '0', '1', '1', '1', '0', 'down', 146], ['1', '1', '0', '0', '0', '1', 'up', 158], ['1', '1', '1', '0', '0', '1', 'down', 134], ['1', '0', '1', '0', '0', '1', 'down', 407], ['1', '1', '1', '0', '0', '0', 'up', 167], ['0', '1', '0', '1', '1', '0', 'down', 154], ['0', '1', '0', '0', '1', '0', 'up', 190], ['1', '1', '0', '0', '1', '0', 'up', 184], ['0', '0', '1', '1', '1', '0', 'up', 354], ['1', '1', '1', '0', '0', '0', 'down', 316], ['0', '1', '0', '0', '1', '1', 'down', 145], ['0', '1', '0', '0', '0', '1', 'down', 367], ['1', '0', '1', '1', '0', '1', 'down', 170], ['0', '1', '0', '0', '0', '1', 'up', 175], ['1', '0', '1', '1', '1', '1', 'up', 410], ['1', '1', '1', '1', '0', '0', 'up', 304], ['0', '1', '1', '0', '0', '0', 'up', 166], ['1', '0', '0', '0', '1', '0', 'up', 27], ['1', '0', '0', '1', '0', '1', 'up', 153], ['1', '0', '1', '0', '1', '1', 'down', 146], ['0', '0', '0', '1', '0', '0', 'up', 30], ['0', '0', '0', '0', '1', '0', 'up', 29], ['0', '0', '1', '1', '0', '0', 'up', 163], ['0', '0', '0', '1', '0', '1', 'up', 152], ['1', '1', '0', '1', '1', '1', 'down', 30], ['0', '0', '0', '0', '0', '1', 'up', 38], ['1', '1', '1', '0', '1', '0', 'down', 157], ['0', '1', '0', '0', '0', '0', 'up', 30], ['0', '1', '1', '1', '1', '0', 'down', 13], ['0', '0', '0', '1', '1', '0', 'up', 176], ['1', '0', '0', '0', '0', '1', 'up', 24], ['1', '1', '1', '1', '1', '0', 'down', 27], ['1', '0', '1', '0', '0', '0', 'up', 17], ['1', '1', '0', '0', '0', '0', 'up', 35], ['0', '1', '0', '1', '1', '1', 'down', 21], ['0', '1', '1', '0', '1', '1', 'down', 22], ['1', '1', '1', '1', '0', '1', 'down', 20], ['1', '1', '1', '1', '1', '1', 'down', 1], ['1', '0', 'down', 1]]
listB = ['0', '0', '0', '0', '1', '0']
def fl(list1, list2):
index = -1
occ = 0
for i, l in enumerate(list2):
if l[:len(list1)] == list1:
if l[-1] > occ:
index = i
occ = l[-1]
if index == -1:
return "The 1st list doesn't appear in the 2nd one."
else:
print(f"The 1st list appears in the 2nd one at index {index} with a number of occurrences equal to {occ}.")
u = "up"
y = "down"
look_up = [(''.join(a[:6]), str(a[6])) if len(a) >= 7 else ('', '') for a in list2]
if (''.join(list1), u) in look_up:
print("up")
elif (''.join(list1), y) in look_up:
print("down")
print(fl(listB, listA))
This is the output:
The 1st list appears in the 2nd one at index 3 with a number of occurrences equal to 507.
up
None
The issue in this is that instead of up it should have written down because of the string element located in listA which is the similar list identified. I want it to be so that if the similar list identified in listA contains the string element up or down, the output must print the string element in accordance.
Please help, I'm still a novice.

You can use difflib.get_close_matches() to compare a list of lists against a singular list and get the most similar elements.
https://docs.python.org/3/library/difflib.html#difflib.get_close_matches

Related

IndexError: list index out of range python

So I have tried to get solutions on this but I haven't really gotten the help I require...I have code to identify a similar list in a list comprised of lists and then it has to identify the string in the identified similar list so that I can use it in an if statement. Whenever I try to get that result however, I get an error and I truly cant seem to get over it.
This is the code:
listA = [['1', '1', '1', '1', '1', '0', 'up', 423], ['1', '0', '1', '0', '1', '1', 'up', 430], ['0', '0', '1', '1', '0', '1', 'up', 398], ['0', '0', '0', '0', '1', '0', 'down', 507], ['1', '1', '0', '0', '1', '1', 'up', 392], ['1', '1', '1', '1', '0', '0', 'down', 169], ['1', '1', '0', '1', '1', '0', 'down', 149], ['0', '1', '0', '0', '1', '0', 'down', 397], ['1', '0', '1', '1', '1', '0', 'up', 387], ['1', '0', '1', '0', '1', '0', 'down', 448], ['1', '1', '1', '1', '0', '1', 'up', 433], ['0', '1', '1', '1', '0', '0', 'up', 355], ['0', '0', '0', '0', '0', '0', 'down', 477], ['1', '0', '0', '0', '0', '0', 'down', 484], ['0', '1', '0', '0', '0', '0', 'down', 520], ['0', '1', '1', '1', '0', '1', 'up', 500], ['0', '0', '0', '0', '1', '1', 'up', 167], ['0', '0', '1', '0', '1', '1', 'up', 373], ['1', '1', '1', '1', '1', '1', 'up', 328], ['0', '0', '1', '0', '0', '0', 'down', 557], ['0', '1', '0', '1', '0', '0', 'up', 203], ['0', '1', '0', '1', '1', '1', 'up', 519], ['1', '1', '1', '0', '1', '1', 'down', 15], ['1', '0', '1', '0', '0', '0', 'down', 498], ['0', '1', '1', '1', '1', '0', 'up', 481], ['1', '1', '0', '1', '0', '0', 'down', 359], ['0', '1', '1', '0', '1', '0', 'up', 414], ['1', '0', '0', '1', '0', '0', 'down', 510], ['0', '1', '0', '1', '0', '0', 'down', 403], ['0', '1', '1', '0', '0', '0', 'down', 360], ['0', '1', '1', '0', '1', '1', 'up', 494], ['1', '1', '1', '0', '1', '0', 'up', 374], ['0', '0', '1', '0', '0', '1', 'up', 149], ['0', '1', '1', '1', '0', '0', 'down', 131], ['0', '0', '0', '0', '1', '1', 'down', 326], ['1', '0', '1', '1', '1', '0', 'down', 179], ['1', '0', '0', '0', '1', '0', 'down', 530], ['1', '0', '1', '0', '0', '1', 'up', 163], ['0', '0', '0', '1', '1', '1', 'up', 338], ['1', '1', '0', '1', '1', '1', 'up', 427], ['1', '0', '0', '1', '0', '1', 'down', 422], ['1', '0', '0', '1', '1', '1', 'down', 134], ['0', '0', '1', '1', '1', '1', 'up', 443], ['1', '0', '1', '1', '0', '0', 'down', 386], ['1', '0', '1', '1', '0', '0', 'up', 144], ['1', '1', '1', '0', '0', '1', 'up', 356], ['0', '0', '0', '1', '0', '0', 'down', 513], ['0', '0', '0', '0', '0', '1', 'down', 497], ['0', '1', '0', '1', '0', '1', 'up', 424], ['1', '0', '1', '0', '1', '0', 'up', 200], ['1', '1', '0', '1', '0', '1', 'up', 369], ['1', '1', '0', '1', '0', '0', 'up', 149], ['1', '1', '0', '0', '0', '0', 'down', 492], ['0', '1', '0', '1', '0', '1', 'down', 164], ['1', '1', '0', '0', '1', '0', 'down', 413], ['1', '0', '0', '0', '1', '1', 'up', 144], ['0', '1', '0', '1', '1', '0', 'up', 399], ['0', '1', '1', '1', '0', '1', 'down', 16], ['0', '1', '1', '0', '0', '1', 'up', 385], ['1', '0', '0', '1', '0', '0', 'up', 28], ['0', '0', '1', '0', '1', '0', 'down', 406], ['1', '0', '0', '1', '1', '0', 'down', 393], ['1', '1', '0', '1', '0', '1', 'down', 156], ['0', '0', '1', '1', '0', '1', 'down', 157], ['0', '0', '1', '1', '0', '0', 'down', 383], ['0', '1', '1', '0', '1', '0', 'down', 163], ['1', '0', '0', '1', '1', '1', 'up', 347], ['0', '0', '0', '1', '0', '1', 'down', 372], ['1', '0', '0', '0', '1', '1', 'down', 385], ['1', '1', '0', '0', '1', '1', 'down', 143], ['0', '0', '1', '0', '0', '1', 'down', 355], ['0', '1', '1', '0', '0', '1', 'down', 111], ['1', '1', '0', '1', '1', '0', 'up', 367], ['0', '1', '0', '0', '1', '1', 'up', 386], ['1', '0', '1', '1', '1', '1', 'down', 20], ['1', '0', '0', '0', '0', '1', 'down', 494], ['0', '0', '0', '1', '1', '1', 'down', 162], ['0', '1', '1', '1', '1', '1', 'up', 430], ['1', '0', '0', '1', '1', '0', 'up', 152], ['0', '0', '0', '1', '1', '0', 'down', 358], ['1', '1', '1', '0', '1', '1', 'up', 393], ['1', '1', '0', '0', '0', '1', 'down', 357], ['0', '0', '1', '0', '0', '0', 'up', 28], ['0', '0', '1', '0', '1', '1', 'down', 134], ['0', '0', '1', '0', '1', '0', 'up', 179], ['0', '0', '1', '1', '1', '1', 'down', 22], ['1', '0', '1', '1', '0', '1', 'up', 377], ['0', '0', '1', '1', '1', '0', 'down', 146], ['1', '1', '0', '0', '0', '1', 'up', 158], ['1', '1', '1', '0', '0', '1', 'down', 134], ['1', '0', '1', '0', '0', '1', 'down', 407], ['1', '1', '1', '0', '0', '0', 'up', 167], ['0', '1', '0', '1', '1', '0', 'down', 154], ['0', '1', '0', '0', '1', '0', 'up', 190], ['1', '1', '0', '0', '1', '0', 'up', 184], ['0', '0', '1', '1', '1', '0', 'up', 354], ['1', '1', '1', '0', '0', '0', 'down', 316], ['0', '1', '0', '0', '1', '1', 'down', 145], ['0', '1', '0', '0', '0', '1', 'down', 367], ['1', '0', '1', '1', '0', '1', 'down', 170], ['0', '1', '0', '0', '0', '1', 'up', 175], ['1', '0', '1', '1', '1', '1', 'up', 410], ['1', '1', '1', '1', '0', '0', 'up', 304], ['0', '1', '1', '0', '0', '0', 'up', 166], ['1', '0', '0', '0', '1', '0', 'up', 27], ['1', '0', '0', '1', '0', '1', 'up', 153], ['1', '0', '1', '0', '1', '1', 'down', 146], ['0', '0', '0', '1', '0', '0', 'up', 30], ['0', '0', '0', '0', '1', '0', 'up', 29], ['0', '0', '1', '1', '0', '0', 'up', 163], ['0', '0', '0', '1', '0', '1', 'up', 152], ['1', '1', '0', '1', '1', '1', 'down', 30], ['0', '0', '0', '0', '0', '1', 'up', 38], ['1', '1', '1', '0', '1', '0', 'down', 157], ['0', '1', '0', '0', '0', '0', 'up', 30], ['0', '1', '1', '1', '1', '0', 'down', 13], ['0', '0', '0', '1', '1', '0', 'up', 176], ['1', '0', '0', '0', '0', '1', 'up', 24], ['1', '1', '1', '1', '1', '0', 'down', 27], ['1', '0', '1', '0', '0', '0', 'up', 17], ['1', '1', '0', '0', '0', '0', 'up', 35], ['0', '1', '0', '1', '1', '1', 'down', 21], ['0', '1', '1', '0', '1', '1', 'down', 22], ['1', '1', '1', '1', '0', '1', 'down', 20], ['1', '1', '1', '1', '1', '1', 'down', 1], ['1', '0', 'down', 1]]
listB = ['1', '1', '1', '1', '1', '0']
def fl(list1, list2):
index = -1
occ = 0
for i, l in enumerate(list2):
if l[:len(list1)] == list1:
if l[-1] > occ:
index = i
occ = l[-1]
if index == -1:
return "The 1st list doesn't appear in the 2nd one."
else:
print(f"The 1st list appears in the 2nd one at index {index} with a number of occurrences equal to {occ}.")
u = "up"
y = "down"
look_up = [(''.join(str(a[:6])), str(a[6])) for a in list2]
if (''.join(list1), u) in look_up:
print("up")
elif (''.join(list1), y) in look_up:
print("down")
print(fl(listB, listA))
This is the output:
The 1st list appears in the 2nd one at index 0 with a number of occurrences equal to 423.
Traceback (most recent call last):
File "C:\Users\nkosi\PycharmProjects\study 6.1\study7.py", line 29, in <module>
print(fl(listB, listA))
File "C:\Users\nkosi\PycharmProjects\study 6.1\study7.py", line 22, in fl
look_up = [(''.join(str(a[:6])), str(a[6])) for a in list2]
File "C:\Users\nkosi\PycharmProjects\study 6.1\study7.py", line 22, in <listcomp>
look_up = [(''.join(str(a[:6])), str(a[6])) for a in list2]
IndexError: list index out of range
This is the output I want:
The 1st list appears in the 2nd one at index 0 with a number of occurrences equal to 423.
up
I'm sure I've even asked this question one too many times but I am really getting nowhere...please help
For avoid IndexError, you should add if-else statement in list comprehension.
look_up = [(''.join(str(a[:6])), str(a[6])) if len(a) >= 7 else ('', '') for a in list2]
When you change this line, output look likes this.
The 1st list appears in the 2nd one at index 0 with a number of occurrences equal to 423.
Furthermore, the contents within look_up are incorrect. Your look_up is not successfully generating a string from the list. The following successfully displayed up. Please try this.
look_up = [(''.join(a[:6]), str(a[6])) if len(a) >= 7 else ('', '') for a in list2]

How to get the height for each column in Python?

This is a nested list:
matrix = [['0', '0', '0', '0', '0', '1', '1', '0', '0', '0'],
['0', '1', '1', '1', '1', '1', '0', '0', '0', '0'],
['0', '0', '1', '1', '0', '1', '0', '1', '1', '0'],
['0', '1', '0', '0', '0', '0', '1', '0', '0', '1'],
['0', '0', '0', '1', '0', '0', '1', '0', '1', '0'],
['0', '0', '0', '0', '1', '0', '1', '1', '1', '0'],
['0', '1', '1', '1', '1', '0', '1', '1', '1', '1'],
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']]
#output should be [1,2,2,2,3,1,5,3,4,2]
Height = 8
Width = 10
I want to know how I can get the height of each column. And then each height, I want to put it in a list.
We counting 1's and they only count for the height if they are adjoint 1's.
We start with counting below and then go up.
Output should be [1,2,2,2,3,1,5,3,4,1]
I only want to use build in Python functions.
I tried with a for loop and if, else statements.
For loop iterate through the list.
like if i == '1' add 1 to counter.
if i == '0' reset counter and add the last value from counter to counter1, but only if counter is greater then counter1.
You can use itertools.takewhile on the reversed, zipped matrix:
from itertools import takewhile
out = [len(list(takewhile(lambda x: x=='1', reversed(l)))) for l in zip(*matrix)]
output:
[1, 2, 2, 2, 3, 1, 5, 3, 4, 2]
If you don't want to import takewhile, use the recipe:
def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable:
if predicate(x):
yield x
else:
break
How it works:
zip rotates the matrix:
>>> list(zip(*matrix))
[('0', '0', '0', '0', '0', '0', '0', '1'),
('0', '1', '0', '1', '0', '0', '1', '1'),
('0', '1', '1', '0', '0', '0', '1', '1'),
('0', '1', '1', '0', '1', '0', '1', '1'),
('0', '1', '0', '0', '0', '1', '1', '1'),
('1', '1', '1', '0', '0', '0', '0', '1'),
('1', '0', '0', '1', '1', '1', '1', '1'),
('0', '0', '1', '0', '0', '1', '1', '1'),
('0', '0', '1', '0', '1', '1', '1', '1'),
('0', '0', '0', '1', '0', '0', '1', '1')]
The list comprehension with reversed rotates the other way around (actually inverses each row):
>>> [list(reversed(l)) for l in zip(*matrix)]
[['1', '0', '0', '0', '0', '0', '0', '0'],
['1', '1', '0', '0', '1', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1', '0'],
['1', '1', '0', '1', '0', '1', '1', '0'],
['1', '1', '1', '0', '0', '0', '1', '0'],
['1', '0', '0', '0', '0', '1', '1', '1'],
['1', '1', '1', '1', '1', '0', '0', '1'],
['1', '1', '1', '0', '0', '1', '0', '0'],
['1', '1', '1', '1', '0', '1', '0', '0'],
['1', '1', '0', '0', '1', '0', '0', '0']]
takewhile keepd the elements while the condition is True, here while the items are '1' (lambda x: x=='1'), and len gets the length of the output:
>>> l = ['1', '1', '1', '0', '0', '0', '1', '0']
>>> list(takewhile(lambda x: x=='1', l))
['1', '1', '1']
>>> len(list(takewhile(lambda x: x=='1', l)))
3
NB. functions like zip, reversed, takewhile are generators, they don't produce output unless something consumes it, that's why I used list(generator(…)) in the exammples
solution with classical python loops:
out = []
for l in zip(*matrix):
counter = 0
for elem in reversed(l):
if elem == '1':
counter +=1
else:
break
out.append(counter)

append key if matches specific index and keyword?

titleValues = {'Movie 1 (1998)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 'Movie 2 (1994)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']}
categories={'unknown': '0', 'Action': '1', 'Adventure': '2', 'Animation': '3', "Children's": '4', 'Comedy': '5', 'Crime': '6', 'Documentary': '7', 'Drama': '8', 'Fantasy': '9', 'Film-Noir': '10', 'Horror': '11', 'Musical': '12', 'Mystery': '13', 'Romance': '14', 'Sci-Fi': '15', 'Thriller': '16', 'War': '17', 'Western': '18'}
selectedCol = 1
titles=[]
for key, value in titleValues.items():
for num in value:
if num == '1':
valIdx = value.index(num)
if valIdx == selectedCol:
titles.append(key)
else:
continue
print(titles)
output:
['Movie 1 (1998)', 'Movie 1 (1998)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)']
I think it appears 6 times because of the six '1' occurrences. However, how can I only obtain two names as for both lists '1' appears at index 1.
['Movie 1 (1998)', 'Movie 2 (1994)']
only when titleValues contains a one, put key once in a list:
titles = [k for k,v in zip(titleValues.keys(),titleValues.values()) if '1' in v]
result will be
print(titles)
# ['Movie 1 (1998)', 'Movie 2 (1994)']
Explanation
Create two iterables (keys, values)
print(titleValues.keys())
#dict_keys(['Movie 1 (1998)', 'Movie 2 (1994)'])
print(titleValues.values())
#dict_values([['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']])
zip, is a function to iterate element-wise and simultanouesly over two iterables
print(list(zip(titleValues.keys(),titleValues.values())))
# [('Movie 1 (1998)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']), ('Movie 2 (1994)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0'])]
in a for-comprehension you can access elements of both iterables (specified variable name: k for element in first iterable and v for element in second)

Iterate over a list of list to create a new list of list

I think I understand what need to be done. However I can't get the results right. I have a list of lists with several items. I want to create a new list(with more elements, based on a criteria) but keeping length of the original list of lists. Let's exemplify:
test = [[2, 3, 5, 6, 9, 10, 11, 13, 14, 16, 18, 20, 23, 24, 25], [1, 4, 5, 6, 7, 9, 11, 12, 13, 15, 16, 19, 20, 23, 24]] # there are hundreds of lists in the original list
list_num_sor = []
new_list = []
for row in test:
for i in range(1, 26):
if i in row:
new_list.append('1')
else:
new_list.append('0')
list_num_sor.append(new_list)
What I'm trying to do is, for each list in test, I want to validate if in a range 1-25, this number is in the list, returning 1, otherwise, 0. The original list has 15 elements, the new one should have 25.
The result should be:
list_num_sor = [['0', '1', '1', '0', '1', '1', '0', '0', '1', '1', '1', '0', '1', '1', '0', '1', '0', '1', '0', '1', '0', '0', '1', '1', '1'],
['1', '0', '0', '1', '1', '1', '1', '0', '1', '0', '1', '1', '1', '0', '1', '1', '0', '0', '1', '1', '0', '0', '1', '1', '0']]
But what I'm getting is a list of lists, where each element has the results of all the items from the first list of list:
[['0', '1', '1', '0', '1', '1', '0', '0', '1', '1', '1', '0', '1', '1', '0', '1', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '0', '0', '1', '1', '1', '1', '0', '1', '0', '1', '1', '1', '0', '1', '1', '0', '0', '1', '1', '0', '0', '1', '1', '0'],
['0', '1', '1', '0', '1', '1', '0', '0', '1', '1', '1', '0', '1', '1', '0', '1', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '0', '0', '1', '1', '1', '1', '0', '1', '0', '1', '1', '1', '0', '1', '1', '0', '0', '1', '1', '0', '0', '1', '1', '0']]
Hope I could make it understandable. I need it to be a list of lists to create a DF afterwards. I don't think processing time would be a problem as it is not something big enough for it to become a problem.
Hope you people could help me out.
Thanks
​
So you want a new list for every row in your old list:
for row in test:
new_list.append([])
for i in range(1, 26):
if i in row:
new_list[-1].append('1')
else:
new_list[-1].append('0')
Every iteration add a list to your list of lists, and then append the 0/1 values to the last list added ([-1]). More concisely:
new_list = [[1 if i in row else 0 for i in range(1, 26)] for row in test]
This has the added benefit you can just put it in your call to the DataFrame constructor directly.

how to append items vertically python?

I have this list :
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
And I need to also 9 vertically list of items
My code :
vertical = []
x = []
n = 0
for List in List_of_all:
for num in List[n]:
x.append(num)
vertical.append(x)
x = []
n += 1
if n == 9:
break
My out put :
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]
Why this iteration dont't work true ?!
How should I define this to get
Bold assumption: you are looking for the following transposition:
>>> list(map(list, zip(*List_of_all)))
[['3', '5', '0', '0', '9', '0', '1', '0', '0'],
['0', '2', '8', '0', '0', '5', '3', '0', '0'],
['6', '0', '7', '3', '0', '0', '0', '0', '5'],
['5', '0', '0', '0', '8', '0', '0', '0', '2'],
['0', '0', '0', '1', '6', '9', '0', '0', '0'],
['8', '0', '0', '0', '3', '0', '0', '0', '6'],
['4', '0', '0', '0', '0', '6', '2', '0', '3'],
['0', '0', '3', '8', '0', '0', '5', '7', '0'],
['0', '0', '1', '0', '5', '0', '0', '4', '0']]
The zip(*...) idiom is the simplest way to go about that in pure Python. Without the nested list casting, it will be an iterator over tuples.
Basically, you are taking the diagonal array, you can do:
import numpy as np
vertical = np.diag(List_of_all)
print(vertical)
array(['3', '2', '7', '0', '6', '0', '2', '7', '0'], dtype='<U1')
# to put each element in a list do this
vertical = [[x] for x in np.diag(List_of_all)]
print(vertical)
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
vertical=[]
for i in range(9):
print([x[i] for x in List_of_all])#for visual appeal only
vertical.append([x[i] for x in List_of_all])
print()
for line_slicer in range(0,9,3):
count=0
for line in vertical:
count+=1
print(line[line_slicer:line_slicer+3])
if count%3==0:
print()
Output:
['3', '5', '0', '0', '9', '0', '1', '0', '0']
['0', '2', '8', '0', '0', '5', '3', '0', '0']
['6', '0', '7', '3', '0', '0', '0', '0', '5']
['5', '0', '0', '0', '8', '0', '0', '0', '2']
['0', '0', '0', '1', '6', '9', '0', '0', '0']
['8', '0', '0', '0', '3', '0', '0', '0', '6']
['4', '0', '0', '0', '0', '6', '2', '0', '3']
['0', '0', '3', '8', '0', '0', '5', '7', '0']
['0', '0', '1', '0', '5', '0', '0', '4', '0']
['3', '5', '0']
['0', '2', '8']
['6', '0', '7']
['5', '0', '0']
['0', '0', '0']
['8', '0', '0']
['4', '0', '0']
['0', '0', '3']
['0', '0', '1']
['0', '9', '0']
['0', '0', '5']
['3', '0', '0']
['0', '8', '0']
['1', '6', '9']
['0', '3', '0']
['0', '0', '6']
['8', '0', '0']
['0', '5', '0']
['1', '0', '0']
['3', '0', '0']
['0', '0', '5']
['0', '0', '2']
['0', '0', '0']
['0', '0', '6']
['2', '0', '3']
['5', '7', '0']
['0', '4', '0']
If you want a list of the first elements in all of your lists:
vertical=[i[0] for i in List_of_all]
this would be the same as
vertical= ['3','5','0','0','9','0','1','0','0']
You can get the columns as folows:
from collections import defaultdict
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
if __name__ == '__main__':
columns = defaultdict(list)
for row in List_of_all:
for j, item in enumerate(row):
columns[j].append(item)
print(columns[0]) # ['3', '5', '0', '0', '9', '0', '1', '0', '0']
It is unclear what you expect of output. If you want to get all the columns in the 2D list, one option is to use numpy:
>>> import numpy as np
>>> items = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> items
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> columns = items.T
>>> columns
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
If you transpose the numpy array, the columns will be rows and vice versa.
Another way to do this is with numpy indexing:
>>> columns = [items[:, i] for i in range(len(items[0]))]
>>> columns
[array([1, 4, 7]), array([2, 5, 8]), array([3, 6, 9])]
>>>
If you simply want to use loops, you should iterate column-wise and not row-wise:
>>> columns = []
>>> for j in range(len(items[0])):
... column = []
... for i in range(len(items)):
... column.append(items[i, j])
... columns.append(column)
...
>>> columns
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
A more pythonic way for the latter solution would be with list comprehensions:
>>> [[items[i][j] for i in range(len(items))] for j in range(len(items[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
That said, the solution provided by #schwobaseggl is a good way to do it too, very pythonic.
One-liner stolen from GeeksforGeeks:
t = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]

Categories

Resources