Bottom Page

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?
Quote
#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

Quote
#3
What should I enter for key?
Quote
#4
What do you think? Look at the sample output dictionary in your original post.
Quote
#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.
micseydel likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#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?
Quote
#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

Quote
#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)?
Quote
#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))
ichabod801 likes this post
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
Quote
#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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Question about doc strings and tuple separator ? mcgrim 1 58 Mar-20-2019, 12:57 PM
Last Post: Yoriz
  Dictionary adds an unexpected list erina 1 110 Mar-14-2019, 01:13 PM
Last Post: ichabod801
  TypeError: 'tuple' object is not callable marlinayakno 1 81 Mar-13-2019, 11:20 AM
Last Post: stullis
  Converting list elements and sublists from int to str iMuny 5 148 Mar-10-2019, 09:05 PM
Last Post: iMuny
  Converting R code to python mcva 2 108 Mar-09-2019, 04:01 PM
Last Post: mcva
  Adding a line number to an lxml Element vindy 0 69 Mar-08-2019, 08:34 PM
Last Post: vindy
  Value error when converting hex value to bytearray shubhamjainj 3 79 Mar-01-2019, 08:29 AM
Last Post: DeaD_EyE
  tuple veysel 4 155 Feb-21-2019, 10:42 AM
Last Post: veysel
  Unable to locate element no such element gahhon 6 198 Feb-18-2019, 02:09 PM
Last Post: gahhon
  tuple and formating problem darktitan 7 161 Feb-17-2019, 07:37 PM
Last Post: marienbad

Forum Jump:


Users browsing this thread: 1 Guest(s)