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
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
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Tuple Unpacking HarshaliPatel 2 49 Yesterday, 02:40 PM
Last Post: buran
  Using xpath to get value of a nested element name using tag named xq1xq1xq1 3 74 Jan-18-2019, 07:13 PM
Last Post: Larz60+
  converting the items of a list in new lists clarablanes 2 77 Jan-13-2019, 12:54 PM
Last Post: clarablanes
  Python Selenium getting table element trengan 2 129 Dec-31-2018, 03:02 PM
Last Post: trengan
  Click Element if displayed using Selenium and Python giaco__mar 1 108 Dec-27-2018, 06:19 PM
Last Post: metulburr
  Need help with weird tuple syntax mwskz8 2 92 Dec-13-2018, 09:58 AM
Last Post: buran
  Converting Angle to X and Y Values: 90/180/270 deg qrani 1 151 Nov-21-2018, 06:41 PM
Last Post: woooee
  Replace element in a nested list nagymusic 4 222 Nov-19-2018, 08:03 PM
Last Post: nilamo
  AttributeError: 'tuple' object has no attribute 'interface' Kim6071 0 163 Nov-18-2018, 03:06 PM
Last Post: Kim6071
  dictionary: print key/value with list(dict) comprehension wardancer84 4 194 Nov-14-2018, 03:14 PM
Last Post: wardancer84

Forum Jump:


Users browsing this thread: 1 Guest(s)