Can I avoid creating multiple loops for populating "c" as listed in the code below and instead shorten the length of the code? (Maybe through list comprehensions, or other means)
n,m = input().split()
a = [input().split() for i in range(0,int(n))]
b = [input().split() for i in range(0,int(m))]
c = []
for i in b:
if i in a:
c.append(list((y+1) for y, e in enumerate(a) if e == i))
else:c.append([-1])
for i in c:
print(*i)
sample input --> ("5 2" and then separated lines)
5 2
a
a
b
a
b
a
b
Shorter code but I'm not sure its easier to understand. Anyway, here it goes:
n,m = input().split()
A = [input().strip() for _ in range(int(n))]
B = [input().strip() for _ in range(int(m))]
C = [ [(idx + 1) for idx, s_B in enumerate(A) if s_B == s] if s in A else [-1] for s in B ]
for lst in C:
print(*lst)
Related
For example, I have 2 lists:
list1 = [6,6,6,6,6,6,6]
list2 = [0,2,4]
If there are the same indexes in the list1 and list2, I need to remove these indexes from the list1, because I should sum the unique indexes from the list1, for example:
a = [1,2,3,4,5]
b = [0,2,4]
x = [a.index(i) for i in a]
y = [b.index(j) for j in b]
for idx in y:
if idx in x:
x.remove(idx)
print(sum(x))
printed is >> 7
I tried this but did not work if there are the same values in list1
a = [6,6,6,6,6,6,6]
b = [0,2,4]
x = [a.index(i) for i in a]
y = [b.index(j) for j in b]
for idx in y:
if idx in x:
x.remove(idx)
printed is >> 0
Indexes and values are different. There will never be the same index twice in one list. You get their index by their value, however index(value) function gives you the first index which matches your value. Have a look at:
a, b, x = [1,2,3,4,5,6,7], [1,2,3], 0
c, d = len(a), len(b)
if d < c:
d, c = len(a), len(b)
for i in range(c, d):
x += i
print(x)
Your question is not very clear, so here are two answers:
If you want to sum the elements from the first list that do not appear in the second list, here is a way to do it:
a = [1,2,3,4,5]
b = [0,2,4]
# We create a set in order to have O(1) operations to check if an element is in b
b_set = set(b)
# We sum on the values of a that are not in b
res = sum(x for x in a if x not in b_set)
print(res)
>>> 9
If you want to sum the elements of the first list that do not have their rank/index in the second list, a way to do that could be:
a = [1,2,3,4,5]
b = [0,2,4]
# We create a set in order to have O(1) operations to check if an element is in b
b_set = set(b)
# We sum on the values of a that don't have their rank/index in b
res = sum(x for (i, x) in enumerate(a) if i not in b_set)
print(res)
>>> 6
I'm trying to solve problem 31 in ProjectEuler.net and the solution I came up with was to make a list of all possible combinations and then only count the ones that add up to £2. To make a list of all possible combinations this is the code I came up with:
c1p = []
for x in range(201):
c1p.append(x)
c2p = []
for x in range(101):
c2p.append(x * 2)
c5p = []
for x in range(41):
c5p.append(x * 5)
c10p = []
for x in range(21):
c10p.append(x * 10)
c20p = []
for x in range(11):
c20p.append(x * 20)
c50p = []
for x in range(5):
c50p.append(x * 50)
c100p = []
for x in range(3):
c100p.append(x * 100)
counter = 1 #As a single £2 coin is already equal to £2
total_combs = [[a, b, c, d, e, f, g] for a in c1p for b in c2p for c in c5p for d in c10p for e in c20p for f in c50p for g in c100p
The way this is currently written, combinations over £2 will keep getting added to the list even though they're not necessary, and stopping this may help it so I don't run out of memory but I can't figure out how to do it.
k = list(map(str,input().split(' ')))
n,m =k
l = 0
narray = list(map(str,input().split(' ')))
narray1 = list(map(str,input().split(' ')))
narray2 = list(map(str,input().split(' ')))
for i in range(0,len(narray1)):
if (narray1[i] in narray):
l=l+1
for i in range(0,len(narray2)):
if (narray2[i] in narray):
l=l-1
print(l)
The above code just does the work bare minimum but neither it is efficient nor it is working with full capacity.
How can I rebuild it efficiently?
You can alternatively use set to check membership in O(1)
A = [1,5,3]
B = {3, 1}
C = {5, 7}
commonFromB = len([i for i in A if i in B])
commonFromC = len([i for i in A if i in C])
print(commonFromB - commonFromC)
Consider using sets as pointed out by #shubham-tambere.
Also you can iterate over a map object instead of creating a list from it and then iterating over the list. For instance:
l = 0
narray = list(map(str,input().split(' ')))
narray1 = map(str,input().split(' '))
for item in narray1:
if item in narray:
l = l + 1
I have 3 variables of array so i created
Indexlist = range(0, 9)
For i in indexlist:
A = [i for i in indexlist]
B = [i for i in indexlist]
C = [i for i in indexlist]
So is there any simple way to declare?
You can also make a copy of your list:
from copy import copy
Indexlist = list(range(0, 9))
A = copy(Indexlist)
B = copy(Indexlist)
C = copy(Indexlist)
Doing this, modifications on one of these 3 lists will not affect the other ones
Use this code. A, B, C = [range(1, 10) for x in range(3)]
Note that range(1, 10) is not list but generator.
If you want the list, use this.
A, B, C = [list(range(1, 10)) for x in range(3)]
you can use:
A, B, C = [list(range(9)) for _ in range(3)]
or:
A = list(range(9))
B = list(range(9))
C = list(range(9))
I'm trying to create a list (b) that is list (a) rotating a's members k times to the left. I came up with this on Python 3:
n = 5
k = 4
a = [1,2,3,4,5]
b = []
for i in a:
if (i + k) <= (n - 1):
b.append(a[i+k])
elif (i+k-n) < (n-1):
b.append(a[i+k-n])
print(b)
But for some reason, it doesn't work since when I tell print(b) it returns a list that is exactly like list a
What am I missing here?
A simple solution:
k = k % len(a) #we don't care about shifting x*len(a) times since it does not have any effect
b = a[k:] + a[:k]
Thanks to #RemcoGerlich for helping me see my mistake! Here's how I quickly fixed my code:
n = 5
k = 4
a = [1,2,3,4,5]
b = []
i = 0
while (i < n):
if (i + k) <= (n - 1):
b.append(a[i+k])
elif (i+k-n) < (n-1):
b.append(a[i+k-n])
i = i+1
print(b)