check if the values are prensent in a list of values

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

Matt Nordhoff a écrit :
(snip)
I'm not judging whether this is a good solution or not, but that's a
silly use of a dict.

Yeps, but a somewhat common one in code predating the apparition of sets
as builtin type.
 
F

flit

Hello All,

I will appreciate the help from the more skillfull pythonistas..

I have a small app that generates a sequence like

00341
01741
03254

This values I am putting in a list.

So I have a list = [00341,01741,03254]

after the programs find the sequence 03401 this sequence is "new" so
it appends on the list. But I want to avoid that as the values are
already on the first sequence of the list (00341).
If I try to use a "in" statement it will give false. as 00341 is
different from 00341 (but for my goal not..)


How can I check against this list and avoid to put "different"
sequences but same values?

as 34100 --> dont append on the list
14300 ---> dont append on the list
05321 --> append to the list.

Am I doing some conceptual error using lists?
There is a better approach?

Thanks
 
E

Emile van Sebille

flit said:
Hello All,

I will appreciate the help from the more skillfull pythonistas..

I have a small app that generates a sequence like

00341
01741
03254

Consider using a dict with sorted tuple keys, eg

d = {}

for seq in ['00341','01741','03254']:
ky = list(seq)
ky.sort()
d[tuple(ky)] = None


then d.keys() are the unique combinations.

HTH,

Emile


This values I am putting in a list.

So I have a list = [00341,01741,03254]

after the programs find the sequence 03401 this sequence is "new" so
it appends on the list. But I want to avoid that as the values are
already on the first sequence of the list (00341).
If I try to use a "in" statement it will give false. as 00341 is
different from 00341 (but for my goal not..)


How can I check against this list and avoid to put "different"
sequences but same values?

as 34100 --> dont append on the list
14300 ---> dont append on the list
05321 --> append to the list.

Am I doing some conceptual error using lists?
There is a better approach?

Thanks
 
G

George Sakkis

Hello All,

I will appreciate the help from the more skillfull pythonistas..

I have a small app that generates a sequence like

00341
01741
03254

This values I am putting in a list.

So I have a list = [00341,01741,03254]

after the programs find the sequence 03401 this sequence is "new" so
it appends on the list. But I want to avoid that as the values are
already on the first sequence of the list (00341).
If I try to use a "in" statement it will give false. as 00341 is
different from 00341 (but for my goal not..)

How can I check against this list and avoid to put "different"
sequences but same values?

as 34100 --> dont append on the list
14300 ---> dont append on the list
05321 --> append to the list.

Am I doing some conceptual error using lists?
There is a better approach?

Whenever you want to keep track of unique values, think of using a set
or dict, not list. Since you don't care about the character order
within each string, sort the characters so that two strings are
equivalent if and only if their sorted character lists are equal.

One more thing to bear in mind is that the elements of a set (or the
keys of a dict) have to be hashable. Lists are not hashable so there
needs to be an extra step to convert the sorted list into an
"equivalent" hashable object. A common choice that works for any list
[*] is to convert it to a tuple. An alternative that works for strings
only is to join() them into a single string:
set(['01235', '00134', '01147', '02345'])

HTH,
George

[*] Assuming that every element of the list is hashable.
 
M

Matt Nordhoff

Emile said:
flit said:
Hello All,

I will appreciate the help from the more skillfull pythonistas..

I have a small app that generates a sequence like

00341
01741
03254

Consider using a dict with sorted tuple keys, eg

d = {}

for seq in ['00341','01741','03254']:
ky = list(seq)
ky.sort()
d[tuple(ky)] = None


then d.keys() are the unique combinations.

HTH,

Emile

I'm not judging whether this is a good solution or not, but that's a
silly use of a dict. A set would be better.

s = set()
for seq in ['00341','01741','03254']:
s.add(tuple(sorted(ky)))

Then you just, well, access the set directly, instead of using d.keys()
or something.

(I also replaced the sorting with the sorted() function for brevity.
This all assumes you have at least Python 2.4...)
This values I am putting in a list.

So I have a list = [00341,01741,03254]

after the programs find the sequence 03401 this sequence is "new" so
it appends on the list. But I want to avoid that as the values are
already on the first sequence of the list (00341).
If I try to use a "in" statement it will give false. as 00341 is
different from 00341 (but for my goal not..)


How can I check against this list and avoid to put "different"
sequences but same values?

as 34100 --> dont append on the list
14300 ---> dont append on the list
05321 --> append to the list.

Am I doing some conceptual error using lists?
There is a better approach?

Thanks
--
 
F

flit

Matt Nordhoff a écrit :
(snip)


Yeps, but a somewhat common one in code predating the apparition of sets
as builtin type.

Thanks for all contributions, sure I learn a lot with all samples.
Very good thread and answers.

Thank you all
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top