Work with a list of list and make some comparisons Python

Work with a list of list and make some comparisons Python



I don't know how compare 2 different lists of list and write what I need:
I have a list of list. I need to check 3 things.
First if the item[1], item[2] of este_mes item are in the list resto and add the complete item to changes.
Second if the item[1], item[2] of este_mes item are not in the resto, add to news and for the item[1], item[2] of resto item are not este_mes, add to lost


item[1], item[2]


este_mes


item


resto


item


changes


item[1], item[2]


este_mes


item


resto


news


the item[1], item[2]


resto


item


este_mes


lost


este_mes = [
['1', 'A', 'a', '300'],
['1', 'B', 'b', '30'],
['1', 'C', 'c', '100'],
['1', 'D', 'd', '4500']]

resto = [
['2', 'A', 'a', '3'],
['2', 'B', 'b', '302'],
['2', 'X', 'x', '98'],
['2', 'Z', 'z', '276'],
['3', 'A', 'a', '54'],
['3', 'B', 'b', '65'],
['3', 'F', 'f', '76'],
['3', 'Y', 'y', '99']]

# I need something like this but I don't know how can I do it!

changes =
news =
lost =

for item in este_mes:
if item[1] and item[2] are in some item of resto:
changes.append(item_resto)
if item[1] and item[2] are not in some item of resto:
news.append(item)
for item in resto:
if item[1] and item[2] are not in item of este_mes:
lost.append(item_resto)



the answers should be:


news = [['1', 'C', 'c', '100'],
['1', 'D', 'd', '4500']]
lost = [['2', 'X', 'x', '98'],
['2', 'Z', 'z', '276'],
['3', 'F', 'f', '76'],
['3', 'Y', 'y', '99']]
changes = [['2', 'A', 'a', '3'],
['2', 'B', 'b', '302'],
['3', 'A', 'a', '54'],
['3', 'B', 'b', '65']]



This is the answer:


este_mes =
resto =
changes =

for item in este_mes:
for rest in resto:
if (item[1] == rest[1] and item[2] == rest[2]):
rest = [rest[0]] + [item[1]] + [item[2]] + ([float(item[3]) - float(rest[3])])
changes.append(rest)
resto.remove(rest)





What is the question? What problems are you having?
– wwii
Aug 30 at 19:47





I don't know how can write the lines below the # in python @wwii
– Martin Bouhier
Aug 30 at 19:48






In that case you should be able to make this question vastly shorter. Narrow it down to your actual problem.
– Denziloe
Aug 30 at 19:55





Your "answer" does not produce the output that you list in your problem statement. You've left out the arithmetic on the final field, and it fails to build the news list at all.
– Prune
Aug 30 at 20:21


news




3 Answers
3



I have commented the code so hopefully you can follow the logic.


#initialise our lists
news =
changes =
lost =
#iterate over each list in `este_mes`
for l in este_mes:
#matches marks whether we have matched in `resto`
matched = False
for ll in resto:
if l[1] == ll[1] and l[2] == ll[2]:
#we matched, so append list from `resto` to changes
changes.append(ll)
matched = True
#if there were no matches, append the `este_mes` list to `news`
if not matched:
news.append(l)

#iterate over lists in `resto` to look for ones to add to `lost`
for l in resto:
#check to see if there are any matches in `este_mes`
for ll in este_mes:
if l[1] == ll[1] and l[2] == ll[2]:
break
else:
#this `else` clause is run if there was no `break` -
#indicates that no matches were found so add to `lost`.
lost.append(l)



which outputs the correct lists:


>>> news
[['1', 'C', 'c', '100'], ['1', 'D', 'd', '4500']]
>>> lost
[['2', 'X', 'x', '98'], ['2', 'Z', 'z', '276'], ['3', 'F', 'f', '76'], ['3', 'Y', 'y', '99']]
>>> changes
[['2', 'A', 'a', '3'], ['3', 'A', 'a', '54'], ['2', 'B', 'b', '302'], ['3', 'B', 'b', '65']]



Use the any operator to see whether there is any occurrence of a particular condition within a list. In this case, I compare two-letter sequences for each new list.


any



For each of these, make sure to grab the item from the proper list (este_mes or resto). That list has to be the outer for in the list comprehension + iterable combination. That's why the first of these has este in the outer part, while the other two have rest in the outer part.


for


este


rest


news = [este for este in este_mes
if not any((rest[1], rest[2]) == (este[1], este[2])
for rest in resto)]

changes = [rest for rest in resto
if any((rest[1], rest[2]) == (este[1], este[2])
for este in este_mes)]

lost = [rest for rest in resto
if not any((rest[1], rest[2]) == (este[1], este[2])
for este in este_mes)]

print(news)
print(lost)
print(changes)



Output:


[['1', 'C', 'c', '100'], ['1', 'D', 'd', '4500']]
[['2', 'A', 'a', '3'], ['2', 'B', 'b', '302'], ['3', 'A', 'a', '54'], ['3', 'B', 'b', '65']]
[['2', 'X', 'x', '98'], ['2', 'Z', 'z', '276'], ['3', 'F', 'f', '76'], ['3', 'Y', 'y', '99']]





I would change the condition to any(rest[1] == este[1] and rest[2] == este[2] for rest in resto) or possibly (rest[1], rest[2]) == (este[1], este[2]). Those can work with any data types and avoid problems like 'A'+'bc'=='Ab'+'c'.
– Matthias Fripp
Aug 30 at 21:44



any(rest[1] == este[1] and rest[2] == este[2] for rest in resto)


(rest[1], rest[2]) == (este[1], este[2])


'A'+'bc'=='Ab'+'c'





Nice idea; I spaced on the tuple technique. Will edit the code.
– Prune
Aug 30 at 21:45




As far as I can tell, your actual question is simply "how do I check whether something is in an item of resto?"


resto



Here's a nice way:


def in_item(items, value):
return any(value in item for item in items)

# For example
in_item([[1, 3], [5]], 1)
Out: True
in_item([[1, 3], [5]], 2)
Out: False



Then you can do if in_item(resto, item[1]) and in_item(resto, item[2]): and so on.


if in_item(resto, item[1]) and in_item(resto, item[2]):





OP needs the two columns to match in the same row; your logic assumes that matching in different rows is okay.
– Prune
Aug 30 at 20:24





Are you sure? I don't think it's in their description of the problem.
– Denziloe
Aug 30 at 20:32





"if item[1] and item[2] are in some item of resto". The resto item is singular.
– Prune
Aug 30 at 20:33


resto





It's ambiguous. And relying on shades of grammatical meaning from a questioner whose first sentence talks about a "list of list" is ridiculous.
– Denziloe
Aug 30 at 20:36



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế