Python Forum
Converting List of 3 Element Tuple to Dictionary
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting List of 3 Element Tuple to Dictionary
#1
I have one 3d list of tuple

list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
I want to add in a dictionary like this:

{ (0,1,6): 2,
  (4,5,1): 2,
  (3,2,1): 1 }
I want to count the similar tuples.

Any suggestions?
Reply
#2
What have you tried?

If this is homework you would probably use the get method of the dictionary with 0 as the default, otherwise use collections.Counter.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
What should I enter for key?
Reply
#4
What do you think? Look at the sample output dictionary in your original post.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
If you don't have to implement your own Counter, you can use the Counter from the collections module.
As mentioned before, tuples are immutable and hashable objects, which can be used as keys for a dict.

In your case, it's just Counter(your_list).
Btw. you've used list as a name for the list. list is a type and you've overwritten it with your list.
Give it another name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
mylist = [(x,y,z) for x in range(1,7) for y in range(1,7) for z in range(1,7)]
count=0
combs=[]
counts = dict()
for data in mylist:
            if data not in counts:
                counts[data] =1
            else :
                counts[data]=counts[data]+1

print(counts)
The problem here is that all the data take value:1 and it is not increment?
Reply
#7
(Jan-11-2019, 02:38 AM)fooikonomou Wrote: The problem here is that all the data take value:1 and it is not increment?

All the tuples in mylist are unique, so all of the values would be 1. It looks like you want all of the sorted combinations, so you need to sort them before checking/incrementing counts.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
And how this can be done?
Let's say I have this list:
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 1), (1, 4, 2), (1, 4, 3), (1, 4, 4), (1, 4, 5), (1, 4, 6), (1, 5, 1), (1, 5, 2), (1, 5, 3), (1, 5, 4), (1, 5, 5), (1, 5, 6), (1, 6, 1), (1, 6, 2), (1, 6, 3), (1, 6, 4)....]

how to sort the tuple values form ex: (6,1,5) --> to (1,5,6)?
Reply
#9
(Jan-11-2019, 01:13 PM)fooikonomou Wrote: how to sort the tuple values form ex: (6,1,5) --> to (1,5,6)?

Like with built-in function sorted()? Note, that it will return list, not tuple (tuples are immutable).

>>> t = (6, 1, 5)
>>> sorted(t)
[1, 5, 6]
It you need tuple, then you can just convert it back tuple(sorted(t))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#10
The function sorted can sort also a list of lists / list of tuples.

If you want to sort the content of the tuples, you can make a new list. You can use a list comprehension.

The first example sorts the content of the list.
The second example keeps the order of the list and sorts the content of the tuples.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 401 Feb-05-2024, 01:18 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 494 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Searche each element of each tuple based 3 numbes zinho 8 779 Dec-11-2023, 05:14 PM
Last Post: zinho
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  list in dicitonary element problem jacksfrustration 3 625 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  Find (each) element from a list in a file tester_V 3 1,155 Nov-15-2022, 08:40 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020