list= [[5, 4, 6], [6, 4, 5], [7, 1, 2]]
I have the above list, I want to compare every list index like [5,4,6] with [6,4,5] and [7,1,2] i.e. with every other list index
and for output: if there exist any common elements between 2 indexes in comparision then I want to output in a format
"the first element of each index" along with any common elements in the index.
answer for this iteration would be [5,4,6] as 5 is the first element of the index in comparison, 6 is the first element of the index in comparison, 4 is the common element.
next compare [6, 4, 5] with [5,4,6] and [7,1,2] and the answer would be [6,5,4]
next compare [7,1,2] with [5,4,6] and [6, 4, 5] and the answer would be [7]
Please help, I have been trying for really long.
basically I want every list index to check for common elements with every other list index and if 2 list indexes have anything in common I want to get a new output list with the first element of both list indexes and common elements
final output= [[5,6,4],[6,5,4],[7]]
myL = [[5, 4, 6], [6, 4, 5], [7, 1, 2]]
newLi = []
for i in range(len(myL)):
tmpLi = []
firstList = myL[i]
for a in range(len(myL)):
if a != i:
secondList = myL[a]
inCommon = set(firstList).intersection(secondList)
if len(inCommon) != 0:
tmpLi.append(firstList[0])
tmpLi.append(secondList[0])
for b in inCommon:
if b not in tmpLi:
tmpLi.append(b)
if len(tmpLi) == 0:
tmpLi.append(firstList[0])
newLi.append(tmpLi)
print(newLi)
Each index needs to appear only once. So I want to check whether a value of 2 exists already in the second column.
my_list = [[0, 0],
[1, 3],
[2, 5],
[3, 4]]
It should return False in this case if I check for "2".
If you mean the second rank is the second index then,
my_list = [[0, 0], [1, 3], [2, 5], [3, 4]]
for index in range(len(my_list)):
if my_list[index][1] == 2:
print("True")
else:
print("False")
First thing I would do is isolate the second elements:
seconds = [x for (y,x) in my_list ]
then I would check if 2 exists in this list with 2 in seconds or in a single step:
>>> 2 in [x for (y,x) in my_list ]
False
>>> 3 in [x for (y,x) in my_list ]
True
>>>
Finally, from #Faibbus comment, in python if you are not interested in a value (you can ignore it) we tend to use _ so the above becomes:
2 in [x for (_,x) in my_list ]
I have to replace every element in the list by sum of all other elements in the list.
[1, 2, 3] => [5, 4, 3]
[1] => [0]
[2, 7, 9] => [16, 11, 9]
I have done so far:
for i in range(len(numbers)):
numbers[i] = sum[numbers[:i]] + sum[numbers[i+1:]]
return numbers
But I'm keep getting TypeError
Elegant way to achieve this will be:
>>> my_list = [1, 2, 3]
>>> [sum(my_list)-x for x in my_list]
[5, 4, 3]
OR, even better to calculate sum outside the list comprehension so that you won't have to calculate it each time (as pointed by #Jean):
>>> my_sum = sum(my_list)
>>> [my_sum-x for x in my_list]
[5, 4, 3]
Issue with your code: You are not making call () to sum, instead trying to access it's index using [..] resulting in TypeError. Also, you are modifying the original list while iterating which will result in different result (which is unexpected result for you). You should be placing these values in separate list.
I have data similar to this:
[[1, 1, 4]], [[1, 2, 4], [1, 7, 4]], [[2, 2, 4]]
I am iterating like this:
count = 0
for list in lists:
for sublist in list:
if sublist[2] == 4:
count += 1
print(count)
The output always ends up being 4.
I want to iterate through all 3 lists of sublists, count the time that "4" is in the third index of a sublist. However, in the second list I only want it counted once, not twice. So the value returned should be 3, not 4.
You can simply fix it by breaking out of the innermost loop if you found one sublist that has a 4 in the third position. That basically checks if any sublist fulfills your condition:
count = 0
for lst in lists:
for sublist in lst:
if sublist[2] == 4:
count += 1
break # just added that line
print(count)
# 3
I also replaced the variable name list with lst because it would otherwise shadow the built-in name list.
This could also be written as:
>>> sum(any(sublist[2] == 4 for sublist in lst) for lst in lists)
3
Or in case you want to check that all sublists fulfill the condition replace any with all:
>>> sum(all(sublist[2] == 4 for sublist in lst) for lst in lists)
3
def has4(lol):
for lst in lol:
if lst[2] == 4:
return True
return False
lists = [[1, 1, 4]], [[1, 2, 4], [1, 7, 4]], [[2, 2, 4]]
count = sum(map(has4, lists))
First we define a predicate which returns True for matched items. Then we use map() to transform the input into a boolean sequence of matched/not-matched. Then we sum, taking advantage of the fact that True counts as 1 and False is 0.
I've been tasked with converting some python code over to Java. I came across some notation I'm not familiar with and can't seem to find any information on. I'm guessing this is a lack of keywords on my part.
I've sanitized the code and hard coded some basic values for simplicity.
index_type = c_int * 1000 #size of int, basically 1000 integers?
indexes = index_type() # not entirely sure what this does
indexes[:] = range(2000, 3000)[:] # no idea
# c_int equals 4
The logic doesn't really matter to me, I'm just trying to figure out what's going on in terms of datatypes and converting to Java.
This is called "slicing". See the tutorial sections on Strings and Lists for a good description of how it works. (In your example, it's actually a ctypes array that's being sliced, not a list, but they work the same way. So, for simplicity, let's talk about lists.)
The notation indexes[:] is a slice of the entire list. So, you can do this:
a = [1, 2, 3]
b = a[:]
… to get a copy of the whole list, and this:
a[:] = [4, 5, 6]
… to replace the contents of the whole list.
You may wonder how this is different from just using a itself. The difference is that in the first case, b = a doesn't copy the list, it just makes another reference to the same list, and in the second case, a = [4, 5, 6] doesn't mutate the list, it rebinds a to refer to a new list. Here's an example that shows the difference:
>>> a = [1, 2, 3]
>>> b = a # b is now the same list as a
>>> a[0] = 10 # so changing that list is visible to b
>>> b
[10, 2, 3]
>>> a = [1, 2, 3]
>>> b = a[:] # b is now a new list, with a copy of a
>>> a[0] = 10 # so changing the original list doesn't affect b
>>> b
[1, 2, 3]
>>> a = [1, 2, 3]
>>> b = a # b is now the same list as a
>>> a = [4, 5] # but now a is a different list
>>> a[0] = 10
>>> b
[1, 2, 3]
>>> a = [1, 2, 3]
>>> b = a # b is now the same list as a
>>> a[:] = [4, 5] # and we've replaced the contents
>>> b
[4, 5]
You may wonder why anyone would use range(2000, 3000)[:] on the right side of the assignment.
Well, I wonder the same thing.
If this is Python 2.x, range(2000, 3000) is a brand-new list. You replace the contents of indexes with the contents of that range list, then give up your only reference to the range list. There is no way anyone could possibly end up sharing it, so there is no good reason to make an extra copy of it, unless you're worried that your computer has too much CPU and too much RAM and might be getting bored.
If this is Python 3.x, range(2000, 3000) is a smart range object. And range(2000, 3000)[:] is a new and equal range object. The copying this time is a lot cheaper, but it's exactly as unnecessary.
The other answers are talking about what x[:] means as an expression. But when this notation is used as the target of an assignment (i.e., on the left side of the =), it means something different. It is still a slice of the object, but it doesn't create a copy; rather, it assigns the given value (the right hand side) to the specified "part" of the object. If you use [:], the slice is the whole object, so its contents will be replaced by what you pass.
An example:
>>> x = [1, 2, 3]
>>> x[:] = [8, 8]
>>> x
[8, 8]
Notice that you can replace the contents by new contents of different length.
If you use a partial slice, only part of the contents will be replaced:
>>> x = [1, 2, 3, 4]
>>> x[1:3] = [8, 8, 88]
>>> x
[1, 8, 8, 88, 4]
The notation:
[a:b]
Is a range that starts at a and ends at b. When you leave one of them blank, it counts as "beginning" and "end", respectively.
Basically, your indexes is a list of values, each one has a number to associate with it that shows its order inside indexes. It starts at 0 and progresses until the end. The line of code:
indexes[:]
Simply means
"first value of indexes to last value of indexes"
I hope this helps,
happy coding!
It means getting a COPY of the original list, since lists are mutable in python, for example
>>a = [1,2,3]
>>b = a
>>a[0] = 3
>>a
[3, 2, 3]
>>b
[3, 2, 3]
>>b = a[:]
>>a[0] = 0
>>a
[0, 2, 3]
>>b
[3, 2, 3]
[:] is an example of slice notation, Python's way to specify any number of items in a sequence. [:] specifies the entire sequence. The notation is succinct and powerful in what it can express. Some other possible forms are:
sequence[n] #an index to one item (not a slice)
sequence[n:] #the nth item to the end of the sequence
sequence[:n] #all items until the nth item (exclusive)
sequence[m:n] #the mth item until the nth item (exclusive)
sequence[:] #all items
sequence[::2] #every other item
sequence[::-1] #all items in reverse order
When slicing is used to modify items in a sequence, the item(s) are modified:
>>> a = [1, 2, 3]
>>> a[0:1] = [4] #same as a[0] = 4
>>> a
[4, 2, 3]
>>> a[:] = [1, 2, 3, 4] #the sequence can be made larger
>>> a
[1, 2, 3, 4]
>>> a[:] = [] #or smaller
>>> a
[]
When slicing is used to read items in a sequence, a shallow copy of the item(s) are returned instead of a reference to the item. The term shallow copy means that if a copied item is itself a sequence, the item will refer to the original sequence, not a copy of it:
For instance:
>>> a = [1, 2, 3]
>>> b = a[0]
>>> b #b has the same value as, but does not reference the same object as a[0]
1 #this is a copy of a[0]
>>> a = [1, 2, 3]
>>> b = a[:]
>>> b #b has the same values as, but does not reference the same object as a
[1, 2, 3] #this is a copy of list a
>>> a = [1, 2, 3]
>>> b = [a, 2, 3]
>>> c = b[:] #c is a hallow copy of b
>>> c
[[1, 2, 3], 2, 3]
>>> b[:] = [] # modifying b does not affect c
>>> c
[[1, 2, 3], 2, 3]
>>> a[0] = 4 # modifying a will affect a in c
>>> c
[[4, 2, 3], 2, 3]