[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
def build_max_dict( tups):
dict = {}
for (a,b) in tups:
if (a in dict):
if (b>dict[a]):
dict[a]=b
else:
dict[a]=b
return(sorted(dict.values()))
[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
[11, 12, 41]data = [(0,11), (1,1), (2,1), (0,1) , (1,41), (2,2), (0,9) , (1,3), (2,12)]
reshape = list(zip(*data))
result = sorted(reshape[1])[-3:]
result
On Fri, 11 Nov 2022 02:22:34 -0500, DFS <nospam@dfs.com> declaimed the following:
[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
Do they have to be IN THAT ORDER?
[11, 12, 41]data = [(0,11), (1,1), (2,1), (0,1) , (1,41), (2,2), (0,9) , (1,3), (2,12)]
reshape = list(zip(*data))
result = sorted(reshape[1])[-3:]
result
On 11/11/2022 12:49 PM, Dennis Lee Bieber wrote:
On Fri, 11 Nov 2022 02:22:34 -0500, DFS <nospam@dfs.com> declaimed the
following:
[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
Do they have to be IN THAT ORDER?
Yes.
Pancho <Pancho.Jones@proton.me> writes:
def build_max_dict( tups):
dict = {}
for (a,b) in tups:
if (a in dict):
if (b>dict[a]):
dict[a]=b
else:
dict[a]=b
return(sorted(dict.values()))
Or,
import itertools
import operator
def build_max_dict( tups ):
key = operator.itemgetter( 0 )
groups = itertools.groupby( sorted( tups, key=key ), key )
return set( map( lambda x: max( x[ 1 ])[ 1 ], groups ))
On 11/11/2022 18:53, DFS wrote:
On 11/11/2022 12:49 PM, Dennis Lee Bieber wrote:Sets aren't ordered, which is why I gave my answer as a list. A wrongly ordered list, but I thought it rude to point out my own error, as no one
On Fri, 11 Nov 2022 02:22:34 -0500, DFS <nospam@dfs.com> declaimed the
following:
[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
Do they have to be IN THAT ORDER?
Yes.
else had. :-)
Assuming you want numeric order of element[0], rather than first
occurrence order of the element[0] in the original tuple list. In this example, they are both the same.
Here is a corrected version
from collections import OrderedDict
def build_max_dict( tups):
dict = OrderedDict()
for (a,b) in tups:
if (a in dict):
if (b>dict[a]):
dict[a]=b
else:
dict[a]=b
return(dict.values())
This solution giving the answer as type odict_values. I'm not quite sure
what this type is, but it seems to be a sequence/iterable/enumerable
type, whatever the word is in Python.
Caveat: I know very little about Python.
On 11/11/2022 2:22 PM, Pancho via Python-list wrote:
On 11/11/2022 18:53, DFS wrote:
On 11/11/2022 12:49 PM, Dennis Lee Bieber wrote:Sets aren't ordered, which is why I gave my answer as a list. A
On Fri, 11 Nov 2022 02:22:34 -0500, DFS <nospam@dfs.com> declaimed the >>>> following:
[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
Do they have to be IN THAT ORDER?
Yes.
wrongly ordered list, but I thought it rude to point out my own error,
as no one else had. :-)
Assuming you want numeric order of element[0], rather than first
occurrence order of the element[0] in the original tuple list. In this
example, they are both the same.
Here is a corrected version
from collections import OrderedDict
def build_max_dict( tups):
dict = OrderedDict()
for (a,b) in tups:
if (a in dict):
if (b>dict[a]):
dict[a]=b
else:
dict[a]=b
return(dict.values())
This solution giving the answer as type odict_values. I'm not quite
sure what this type is, but it seems to be a
sequence/iterable/enumerable type, whatever the word is in Python.
Caveat: I know very little about Python.
Kindly do not use "dict" as a variable name, since that shadows the
system's built-in name for a dictionary type.
On 11/11/2022 18:53, DFS wrote:
On 11/11/2022 12:49 PM, Dennis Lee Bieber wrote:Sets aren't ordered, which is why I gave my answer as a list. A wrongly ordered list, but I thought it rude to point out my own error, as no one
On Fri, 11 Nov 2022 02:22:34 -0500, DFS <nospam@dfs.com> declaimed the
following:
[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
Do they have to be IN THAT ORDER?
Yes.
else had. :-)
Assuming you want numeric order of element[0], rather than first
occurrence order of the element[0] in the original tuple list. In this example, they are both the same.
Here is a corrected version
from collections import OrderedDict
def build_max_dict( tups):
dict = OrderedDict()
for (a,b) in tups:
if (a in dict):
if (b>dict[a]):
dict[a]=b
else:
dict[a]=b
return(dict.values())
This solution giving the answer as type odict_values. I'm not quite sure
what this type is, but it seems to be a sequence/iterable/enumerable
type, whatever the word is in Python.
Caveat: I know very little about Python.
[(0,11), (1,1), (2,1),
(0,1) , (1,41), (2,2),
(0,9) , (1,3), (2,12)]
The set of values in elements[0] is {0,1,2}
I want the set of max values in elements[1]: {11,41,12}
Thanks for looking at it. I'm trying to determine the maximum length of
each column result in a SQL query. Normally you can use the 3rd value
of the cursor.description object (see the DB-API spec), but apparently
not with my dbms (SQLite). The 'display_size' column is None with
SQLite. So I had to resort to another way.
On Fri, 11 Nov 2022 15:03:49 -0500, DFS <nospam@dfs.com> declaimed the following:
Thanks for looking at it. I'm trying to determine the maximum length of
each column result in a SQL query. Normally you can use the 3rd value
of the cursor.description object (see the DB-API spec), but apparently
not with my dbms (SQLite). The 'display_size' column is None with
SQLite. So I had to resort to another way.
Not really a surprise. SQLite doesn't really have column widths --
since any column can store data of any type; affinities just drive it into what may be the optimal storage for the column... That is, if a column is "INT", SQLite will attempt to convert whatever the data is into an integer
-- but if the data is not representable as an integer, it will be stored as the next best form.
123 => stored as integer
"123" => converted and stored as integer
123.0 => probably converted to integer
123.5 => likely stored as numeric/double
"one two three" => can't convert, store it as a string
We've not seen the SQL query in question,
but it might suffice to use a
second (first?) SQL query with aggregate (untested)
max(length(colname))
for each column in the main SQL query.
"""
length(X)
For a string value X, the length(X) function returns the number of characters (not bytes) in X prior to the first NUL character. Since SQLite strings do not normally contain NUL characters, the length(X) function will usually return the total number of characters in the string X. For a blob value X, length(X) returns the number of bytes in the blob. If X is NULL
then length(X) is NULL. If X is numeric then length(X) returns the length
of a string representation of X.
"""
Note the last sentence for numerics.
Types are available if you want to use
them. https://www.pythontutorial.net/python-basics/python-type-hints/
From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Pancho via Python-list <python-list@python.org>
Date: Friday, November 11, 2022 at 6:28 PM
To: python-list@python.org <python-list@python.org>
Subject: Re: Need max values in list of tuples, based on position
That was one of the things I didn't like about Python. Lack of types and subsequent loss of intellisense is the thing I find hardest to deal with.
Granted, this will NOT work with "select *" unless one does the select
*/fetchall first, AND extracts the names from the cursor description --
then run a loop to create the select max(length(col)), ... statement (which >is why I state the fetchall step, as unless one creates a second cursor,
the latter select will wipe out any unfetched data from the first).
<sqlite3.Cursor object at 0x0000022453703F80>import sqlite3 as db
con = db.connect("c:/users/wulfraed/documents/.localdatabases/solitaire-sqlite/solitaire.db")
cur = con.cursor()
cur.execute("select * from game")
['max(length(ID))', 'max(length(Variant))', 'max(length(Num_of_Decks))', 'max(length(Cards_in_Deck))', 'max(length(Num_Tableau_Cards))', 'max(length(Num_Reserve_Cards))', 'max(length(Foundation_Value))', 'max(length(Tableau_Value))', 'max(length(Reserve_Value))']columns = [ "max(length(%s))" % col[0] for col in cur.description ]
columns
<sqlite3.Cursor object at 0x0000022453703F80>sql = "select %s from game" % ", ".join(columns)
data = cur.fetchall()
cur.execute(sql)
(1, 16, 1, 2, 2, 2, 1, 2, 2)widths = cur.fetchone()
widths
['%1s', '%16s', '%1s', '%2s', '%2s', '%2s', '%1s', '%2s', '%2s']pformat = [ f'%{w}s' for w in widths ]
pformat
... print(pformat % row)pformat = " | ".join(pformat)
for row in data:
Yeah, I don't know why cursor.description doesn't work with SQLite; all
their columns are basically varchars.
The query is literally any SELECT, any type of data, including SELECT *.
The reason it works with SELECT * is the cursor.description against
SQLite DOES give the column names:
select * from timezone;
print(cur.description)
(
('TIMEZONE', None, None, None, None, None, None),
('TIMEZONEDESC', None, None, None, None, None, None),
('UTC_OFFSET', None, None, None, None, None, None)
)
(I lined up the data)
... max(length(num_reserve_cards)) from Game""")import sqlite3 as db
con = db.connect("c:/users/wulfraed/documents/.localdatabases/solitaire-sqlite/solitaire.db")
cur = con.cursor()
cur.execute("""select max(length(variant)), max(length(cards_in_deck)),
[(16, 2, 2)]widths = cur.fetchall()
widths
(16, 2, 2)
widths[0]
['%16s', '%2s', '%2s']pformat = [f'%{w}s' for w in widths[0] ]
pformat
'%16s\t%2s\t%2s'pformat = "\t".join(pformat)
pformat
... print(pformat % row)for row in cur.fetchall():
Edit: found a solution online: -----------------------------------------------------------------
x = [(11,1,1),(1,41,2),(9,3,12)]
maxvals = [0]*len(x[0])
for e in x:
maxvals = [max(w,int(c)) for w,c in zip(maxvals,e)]
print(maxvals)
[11,41,12]
-----------------------------------------------------------------
So now the challenge is making it a one-liner!
On 11/11/2022 19:56, DFS wrote:
Edit: found a solution online:
-----------------------------------------------------------------
x = [(11,1,1),(1,41,2),(9,3,12)]
maxvals = [0]*len(x[0])
for e in x:
maxvals = [max(w,int(c)) for w,c in zip(maxvals,e)]
print(maxvals)
[11,41,12]
-----------------------------------------------------------------
So now the challenge is making it a one-liner!
x = [(11,1,1),(1,41,2),(9,3,12)]
print(functools.reduce( lambda a,b : [max(w,c) for w,c in zip(a,b)],
x, [0]*len(x[0])))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 64:34:34 |
Calls: | 6,915 |
Files: | 12,379 |
Messages: | 5,431,611 |