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.
Below is the Database model:
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('book.db')
db.open()
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("card")
self.model.select()
For example:
a = "name"
b = 30
c = "M"
data_row = [a, b, c]
r = self.model.record()
r.setValue("name", "name")
r.setValue("age", 30)
r.setValue("gender", "M")
self.model.insertRecord(-1, r)
self.model.select()
The database have 3 columns that are 'name', 'age','gender'.
My question is how to insert row in to Sqlite table using QSqlTableModel using with for-loop?
I tried below code That's worked for me :
Used "for-loop" to count-columns and zip() Function for Parallel Iteration.
a = "name"
b = 30
c = "M"
data_row = [a, b, c]
columns = []
for col in range(self.model.columnCount()):
columns.append(col)
r = self.model.record()
for col, rec in zip(columns, data_row):
r.setValue(col, rec)
self.model.insertRecord(-1, r)
self.model.select()
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)
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)