David - 2 years ago 146
Python Question

# list(set) for nested lists?

I am currently working on a module that will compute the angle between a vector and the normal of a plane.

Anyhow to satisfy the conditions of computing the normal to the plane, I need 3 points in R3.

I have found a way to do this by converting the list elements to strings, however this complicates the code downstream. The following block of code aims to satisfy this condition. List t2 contains the strings and works well downstream, however I wish to work with a nested list (t1).

``````t1,t2=[],[] #B3
for i in range(0, len(s)):
t1.append([s[i],s[i]])

t2.append(s[i]+s[i])
u,j,k=list(set(t2)),[],[]
for item in u:
j.append(t2.count(item))
if len(u) == len(j):
for i in range(0, len(u)):
if j[i] >= 3:
k.append([u[i]])

List t1 looks as follows:
[['[7,', '158,'],
['[7,', '158,'],
['[51,', '158,'],
['[51,', '158,'],
['[51,', '158,'],
['[51,', '161,'],
['[51,', '161,'],
['[51,', '161,'],
['[298,', '114,'],
['[808,', '138,'],
['[808,', '138,']...
``````

So within t1 if there exists 3 or more lists containing equivalent elements (such as ['[51,', '158,']) I wish to append these. The problem is that the set() does not work with nested lists.

Is there a way around this?
Cheers niemmi

In case that you want to get all the lists with three or more instances and preserve the ordering you can use `OrderedDict` where keys are tuples and values are counts:

``````from collections import OrderedDict

t1 = [
['[7,', '158,'],
['[7,', '158,'],
['[51,', '158,'],
['[51,', '158,'],
['[51,', '158,'],
['[51,', '161,'],
['[51,', '161,'],
['[51,', '161,'],
['[298,', '114,'],
['[808,', '138,'],
['[808,', '138,']
]

d = OrderedDict()
for x in t1:
t = tuple(x)
d[t] = d.get(t, 0) + 1

[list(k) for k, v in d.items() if v >= 3] # [['[51,', '158,'], ['[51,', '161,']]
``````

If ordering is not important you could use `Counter`:

``````from collections import Counter
from itertools import takewhile

c = Counter(tuple(x) for x in t1)
[list(k) for k, v in takewhile(lambda x: x >= 3, c.most_common())]
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download
{"ft_lang_label":"__label__en","ft_lang_prob":0.6892566,"math_prob":0.9741477,"size":1772,"snap":"2019-13-2019-22","text_gpt3_token_len":574,"char_repetition_ratio":0.20531674,"word_repetition_ratio":0.125,"special_character_ratio":0.4819413,"punctuation_ratio":0.26153848,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99138457,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-21T10:01:51Z\",\"WARC-Record-ID\":\"<urn:uuid:ee939747-8ae9-45b7-bde7-8e9786a1a1ad>\",\"Content-Length\":\"46386\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e857b459-5c68-4525-a003-3a6dec6bd20c>\",\"WARC-Concurrent-To\":\"<urn:uuid:13eaa20f-32b6-43b9-b734-779debfa1f84>\",\"WARC-IP-Address\":\"104.18.60.77\",\"WARC-Target-URI\":\"https://codedump.io/share/BwVvNluuMRlh/1/listset-for-nested-lists\",\"WARC-Payload-Digest\":\"sha1:MMY756WSPUKEAPUDQCFJVXLIOSMHEB5Z\",\"WARC-Block-Digest\":\"sha1:LLKGQXALH2AYW2U6O5NBE76SC3OLQYHY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912202510.47_warc_CC-MAIN-20190321092320-20190321114320-00441.warc.gz\"}"}