Hey all,

I've been having some problem with python - i'm pretty awful at programming but have been soldiering on with this for some time now! What I need to do is take one list and compare it to another. Both lists are presented in rows, and I need to check each row individually from list 1 against all the rows in list 2.


#List 1 contains data like this:
#List 2 contains data like this:
0, 0800, MON
1, 0830, MON
2, 0900, MON
3, 0930, MON
4, 1000, MON

I want to compare each line of list 1 against all the lines of list 2, so the result should be:

4, 1000, MON
3, 0930, MON
1, 0830, MON
3, 0930, MON
2, 0900, MON
1, 0830, MON

At the end of all this i'll have to put it in with some other data and put it all into a separate .csv for each row.

So far i've imported both sets of data into python but from here i'm a bit stumped.

Could anyone help? I'd be very appreciative!


list1 = [4, 3, 1, 3, 2, 1] 

list2 = [[0, '0800', 'MON'],
         [1, '0830', 'MON'],
         [2, '0900', 'MON'],
         [3, '0930', 'MON'],
         [4, '1000', 'MON']]

for i in range(len(list1)):
    for item in list2:
        if item[0] == list1[i]:
            print item

Cheers and Happy coding

Oh that's awesome and works very well, thank you so much!

I do however have a tangential problem which is that my list, for some reason, is appearing in the following format:

list 1 = '1', '2', '3', '4' (etc)

Which is a problem, because '1' != 1

Do you have a way to remove everything that isn't a number or a letter from a list? Quite soon i'll also be needing to remove brackets as well as these apostrophes!

You seem to have a tuple of numeric strings. Change it to a list this way ...

data_tuple = '4', '3', '1', '3', '2', '1'
data_list = [int(item) for item in data_tuple]
print(data_list)  # [4, 3, 1, 3, 2, 1]

Sorry to be such a pain, but i'm getting this:

reference_list = [int(item) for item in reference]
ValueError: invalid literal for int() with base 10: ','

What does that mean? =/

Aah! Problem solved on that front!

For anyone that's interested, I did the following to sort it:

import csv
reference = csv.reader (open('reference.txt', 'rb'), delimiter=',', quotechar='|')
reference_list = []

However as usual with my life, one problem down and another arises.

I've realised that the way I was getting data for my "list 1" was a complete cheat, because I was getting it like this:

for data in query_list:

All well and good, but doesn't really work for three-digit or single-digit values...

The data is presented in rows like this:

LUBS5200M01/SMR 1/02<S1> wks 2-11,"((10 11 12 13 14 15 16 17 18 19) (34))",2

And the number i'm interested in is the last bracketed number... (34). Any ideas how to go about getting this?

I'm so sorry i'm asking so many questions and thank you for helping me!

That means that you have a typo, or your reference list is somenthing like:

reference_list = '1, 2, 3, 4'

and for that:

reference_list = [int(item.strip()) for item in reference_list.split(',')]

Cheers and Happy coding

import re

data = 'LUBS5200M01/SMR 1/02<S1> wks 2-11,"((10 11 12 13 14 15 16 17 18 19) (34))",2'

data = data.split(',')[1]

match = re.findall(r'\(\d+\)', data)

print match[0][1:-1]

Cheers and Happy coding

It worked!

Thank you very very very much for this - it's been days since I last had more than a couple of hours sleep (told you I was terrible at coding!)

For those interested, what I did was changed the value from "[-5:-3]" to "[-6:-3]" which lets it grab 5 characters instead of 4.

for data in query_list:

Which meant the second line here was needed, to cope with the extra blank space on the single-digit values. The first line took away that annoying little bracket.

scheduled_periods = [str(item.strip('(')) for item in scheduled_periods]
scheduled_periods = [str(item.strip(' (')) for item in scheduled_periods]

It means you are processing string, not list or tuple. Try to chance to end of line to

in reference.split.(',')]
This question has already been answered. Start a new discussion instead.