lst = [1, 4, 5, 9, 6]
How to write a code to get some missing values which must return [2, 3, 7, 8]?
Here is my code
new_list = []
for i in range(1, len(lst1)):
if i not in lst1:
new_list.append(i)
print(new_list)
You can convert them to sets and use "-" to calculate the difference. Note that the order is important.
You need to define what you want to compare your list to if you want to find the missing elements. You can automatically do this if it is simple like "the numbers from 1 to 10" using the code:
list(range(1, 10))
The comparison code is:
lst1 = [1, 4, 5, 9, 6]
lst2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
out = list(set(lst2) - set(lst1))
print(out)
which returns
[8, 2, 3, 7]
It isn't clear exactly what you want from your question, if you give a more detail it will be easier to help.
Related
I am trying to get 25 Numbers to put them in a 2D list/array but whenever i try to get the index of the item, I'm always facing a valueError.
What i tried.
I have tried using a for loop with the enumerate() function to get the specfic Item which is (1).
I have also tried using the .index() method but also with that I was faced with a ValueError: 1 is not in the list. Which made me confused as to why the code isn't working.
mylist = [list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),]
print(mylist)
print(mylist.index(1))
Here is one approach to consider:
myList = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [12, 3, 5, 6, 7], [2, 4, 5, 6, 7], [2, 4, 3, 6, 2]]
def index_2d(input_list, value):
for i, row in enumerate(input_list):
try:
return (i, row.index(value))
except ValueError:
continue
# You can also raise ValueError here instead of implicit return (un-comment the next line)
# raise ValueError(f'{value} is not in the list')
Output:
>>> print(myList)
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [12, 3, 5, 6, 7], [2, 4, 5, 6, 7], [2, 4, 3, 6, 2]]
>>> print(index_2d(myList, 12))
(2, 0)
>>> print(index_2d(myList, 11))
None
Explanation:
We accept the 2d-list and a value we are trying to check. Then use enumerate (to keep track of the index of the main array). We then use index() at each row (list in the main array) to check if the value belongs there, if not (ValueError exception got raised) we simply move to the next row and repeat. But if index() resolves, we return a tuple of the index of the list inside the main list (from the enumerate()) and the index inside the second list or row.
I seem to be struggling a little with this issue. Let's say you have a list of various lengths i.e:
first_list = [8, 7, 5, 1, 2, 4]
or
second_list = [6, 8, 4, 1]
regardless of how long or short the list is how would you be able to add the elements of each list such that all the elements get added together? I.e First > first + second > first + second + third > ... etc
first_list = [8, 8+7, 8+7+5, 8+7+5+1, 8+7+5+1+2, 8+7+5+1+2+4]
second_list = [6, 6+8, 6+8+4, 6+8+4+1]
I have tried a few different things but just don't seem to be able to do this :-/
Try this:
for i in range(1,len(myList)):
myList[i] += myList[i-1]
You can also use a list comprehension:
>>> my_list = [8, 7, 5, 1, 2, 4]
>>> [sum(my_list[:i+1]) for i in range(len(my_list))]
[8, 15, 20, 21, 23, 27]
Note that even though this looks more compact, #MarkSnyder's answer is actually more performant as it has a time complexity of O(n) while this one is O(n^2).
Edit: If you are using Python 3.8 or higher, you can use the walrus operator to have a list comprehension with O(n) complexity:
>>> my_list = [8, 7, 5, 1, 2, 4]
>>> s = 0
>>> [s := s + i for i in my_list]
[8, 15, 20, 21, 23, 27]
I am confused about what does lists[outer_index][inner_index] do? I thought that when two lists are next to each other, it means the first list is the selected list and the second list indicates the index of the first list. However, that doesn't seem to be the case here.
def flatten(lists):
results = []
for outer_index in range(len(lists)): # outer index = 0, 1
for inner_index in range(len(lists[outer_index])): # inner_index = [0, 1, 2, 0, 1, 2, 3, 4, 5]
results.append(lists[outer_index][inner_index])
return results
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
print(flatten(n))
You are creating an list of lists (basically a table).
n = [[1, 2, 3],
[4, 5, 6, 7, 8, 9]]
If I do n[0][1] I am saying go to row 0 and grab the element in column 1.
Its better to think of it this way.
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
s = n[0] # Now s = [1,2,3], the first element in n
s[1] = 2 # Because I just grabbed the second element in [1,2,3]
# This is the same as
n[0][1]
I want to take input of 2 numbers: the number of rows and the number of columns. I then want to use these to output a matrix numbered sequentially. I want to do this using a list comprehension. The following is a possible output.
>>>> my_matrix = matrix_fill(3, 4)
>>>> my_matrix
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
I am using the following code to output a sequentially numbered list:
def matrix_fill(num_rows, num_col):
list=[i for i in range(num_col)]
return (list)
I cannot, however, figure out how to make the sequential list of numbers break into the separate lists as shown in the output based on num_rows.
I don't think you need itertools for that. The range function can take a step as a parameter. Like this:
def matrix_fill(rows,cols):
return [[x for x in range(1,rows*cols+1)][i:i+cols] for i in range(0,rows*cols,cols)]
And then it works as expected.
>>> matrix_fill(3,4)
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Let's break this down a little bit and understand what's happening.
>>> [x for x in range(1,3*4+1)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
So what we want to do is to get a new slice every four elements.
>>> [x for x in range(1,3*4+1)][0:4]
[1, 2, 3, 4]
>>> [x for x in range(1,3*4+1)][4:8]
[5, 6, 7, 8]
>>> [x for x in range(1,3*4+1)][8:12]
[9, 10, 11, 12]
So we want to iterate over the elements of the list[x for x in range(1,3*4+1)] of length "rows*cols" ( 3 * 4 ), create a new slice every "cols" number of elements, and group these slices under a single list. Therefore, [[x for x in range(1,rows*cols+1)][i:i+cols] for i in range(0,rows*cols,cols)] is a suitable expression.
Nest a list comprehension inside another one, use itertools.count() to generate the sequence:
import itertools
rows = 3
cols = 4
count_gen = itertools.count() # pass start=1 if you need the sequence to start at 1
my_matrix = [[next(count_gen) for c in range(cols)] for r in range(rows)]
print(my_matrix)
# prints: [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
# As a function
def matrix_fill(rows, cols):
count_gen = itertools.count()
return [[next(count_gen) for c in range(cols)] for r in range(rows)]
If you used the numpy module, the method is extremely simple, with no list comprehension needed.
my_matrix = np.arange(1, 13).reshape(3,4)
Printing the variable my_matrix shows
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
I have a list of number:
a=[2,3,4,5,1,3,2,4,5,6,2,6,7,5,2,7,5,6,2]
I want the longest sequence that not contain 2, so the answer is:
[3,4,5,1,3]
How can I do this in python?
Thanks for helping me,
You can use itertools.groupby():
from itertools import groupby
a = [2, 3, 4, 5, 1, 3, 2, 4, 5, 6, 2, 6, 7, 5, 2, 7, 5, 6, 2]
# get the subsequences not containing 2
subsequences = (list(it)
for contains_two, it in groupby(a, lambda x: x == 2)
if not contains_two)
# find the longest one among them
print(max(subsequences, key=len))
prints
[3, 4, 5, 1, 3]