Python Forum
Take the biggest value from array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Take the biggest value from array
#1
Hi,
I am new user on python, and I am new user here.
so, can you please help me to fix my python problem...

this table called tbl_a
===================
id | nama | num |
===================
1 | A | 1,2 |
2 | B | 1,3 |
3 | C | 2 |
4 | D | 4,2,1 |
5 | E | 5,3 |
===================

i want to get the biggest value from that table, in that case should be 5 as the biggest value.
I have no idea to write a code.
thanks
Reply
#2
Moved to Homework because it looks like homework.

I know you said you have no idea how to write the code, but we don't write code for you. Try something, and if you get an error, show us your code and tell us exactly what the error is. We'll help you fix it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-14-2017, 02:44 AM)ichabod801 Wrote: Moved to Homework because it looks like homework.

I know you said you have no idea how to write the code, but we don't write code for you. Try something, and if you get an error, show us your code and tell us exactly what the error is. We'll help you fix it.

I'll try to write some code like what i want, but if I got stuck please help me..
I am very new on python..
Thanks for your respons
Reply
#4
def aFunc(arg1):
   base.execute("select num from tbl_a where KodeForm=%s",(arg1))
   hsl = base.fetchall()
   for dta in hsl:
       a = dta[0].split(",")
       b = ",".join(a)
       print(b)
       for dtb in a:
           b = dtb.split(",")
it's turns

Output:
2,3 2 1
how to make it 2,3,2,1 ?
Reply
#5
I don't understand. What is this base you are getting execute and fetchall from? Is it meant to read the table?

Note that join is the opposite of split, so on line 6, b is just equal to data[0].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Jul-14-2017, 01:06 PM)ichabod801 Wrote: I don't understand. What is this base you are getting execute and fetchall from? Is it meant to read the table?

Note that join is the opposite of split, so on line 6, b is just equal to data[0].

Hi,
yes, this is fetch from table.
I am trying to do this,

base.execute("select num from tbl_a where nama=%s",(arg1))
hsl = base.fetchall()
print(hsl)
and it returns

Output:
(('2,3',), ('2',), ('1',))
I just want to make it 2,3,2,1
and then I need the biggest value, in this case it should returns 3..
Reply
#7
Well, they're all strings, so I think the idea of joining them first is a good one:

words = [row[0] for row in hsl]
num_text = ','.join(words)
The first row will pull the individual strings out of the tuples. The second row will join them together with commas. You will end up with '2,3,2,1', which is what you want, but as a string, not numbers.

Then you can convert them to numbers and get the max:

max_value = max([int(x) for x in num_text.split(',')])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Jul-19-2017, 04:20 AM)dwiga Wrote:
Output:
(('2,3',), ('2',), ('1',))
I just want to make it 2,3,2,1
and then I need the biggest value, in this case it should returns 3..

  1. Iterating through hsl gives you:
    for row in hsl:
        print(row)
    Written as a list comprehension:
    [row for row in hsl]
    Output:
    [('2,3',), ('2',), ('1',)]
    Each row is a tuple with one element, which is a string:
    ('2,3',)
    You need the first element:
    ('2,3',)[0]
    '2,3'
  2. Split the first element of the tuple. The separator is a comma:
    ('2,3',)[0].split(',')
    It returns a list:
    Output:
    ['2', '3']
  3. Combine it with a list comprehension:
    [column for row in hsl for column in row[0].split(',')]
    Output:
    ['2', '3', '2', '1']
    You see a list of strings. Not good to compare the highest value. You have to convert it into an integer:
    [int(column) for row in hsl for column in row[0].split(',')]
    You get a list with integers:
    Output:
    [2, 3, 2, 1]
  4. Use the function max, to get the highest value from this flat list:
    max([int(column) for row in hsl for column in row[0].split(',')])
    You use a generator expression inside the built-in function max. Just remove the square brackets and then you've your generator expression inside the function call:
    max(int(column) for row in hsl for column in row[0].split(','))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
(Jul-14-2017, 02:30 AM)dwiga Wrote: Hi,
I am new user on python, and I am new user here.
so, can you please help me to fix my python problem...

this table called tbl_a
===================
id    |   nama   |   num   |
===================
1    |  A          |    1,2   |
2    |  B          |    1,3   |
3    |  C          |    2     |
4    |  D          |  4,2,1  |
5    |  E          |  5,3     |
===================

i want to get the biggest value from that table, in that case should be 5 as the biggest value.
I have no idea to write a code.
thanks

Is it too late to redesign the table structure so it isn't so terrible?  num looks like a varchar, instead of a num like it claims, which is insanity.  This wouldn't even need to be a question you'd have to ask if the table was sane, as you could just have the database's index give you the answer lightning-fast: select max(num) from tbl_a where nama=%s.
Reply
#10
(Jul-19-2017, 01:11 PM)ichabod801 Wrote: Well, they're all strings, so I think the idea of joining them first is a good one:

words = [row[0] for row in hsl]
num_text = ','.join(words)
The first row will pull the individual strings out of the tuples. The second row will join them together with commas. You will end up with '2,3,2,1', which is what you want, but as a string, not numbers.

Then you can convert them to numbers and get the max:

max_value = max([int(x) for x in num_text.split(',')])

you're cool dude....
thank you so much ichabod801...

(Jul-19-2017, 04:50 PM)nilamo Wrote:
(Jul-14-2017, 02:30 AM)dwiga Wrote: Hi,
I am new user on python, and I am new user here.
so, can you please help me to fix my python problem...

this table called tbl_a
===================
id    |   nama   |   num   |
===================
1    |  A          |    1,2   |
2    |  B          |    1,3   |
3    |  C          |    2     |
4    |  D          |  4,2,1  |
5    |  E          |  5,3     |
===================

i want to get the biggest value from that table, in that case should be 5 as the biggest value.
I have no idea to write a code.
thanks

Is it too late to redesign the table structure so it isn't so terrible?  num looks like a varchar, instead of a num like it claims, which is insanity.  This wouldn't even need to be a question you'd have to ask if the table was sane, as you could just have the database's index give you the answer lightning-fast: select max(num) from tbl_a where nama=%s.

sorry dude, I am new here.
but, i've to make my num as a varchar.
thanks for your answer, it's priceless when you give me some solution to go out from my problem..

(Jul-19-2017, 01:51 PM)DeaD_EyE Wrote:
(Jul-19-2017, 04:20 AM)dwiga Wrote:
Output:
(('2,3',), ('2',), ('1',))
I just want to make it 2,3,2,1
and then I need the biggest value, in this case it should returns 3..

  1. Iterating through hsl gives you:
    for row in hsl:
        print(row)
    Written as a list comprehension:
    [row for row in hsl]
    Output:
    [('2,3',), ('2',), ('1',)]
    Each row is a tuple with one element, which is a string:
    ('2,3',)
    You need the first element:
    ('2,3',)[0]
    '2,3'
  2. Split the first element of the tuple. The separator is a comma:
    ('2,3',)[0].split(',')
    It returns a list:
    Output:
    ['2', '3']
  3. Combine it with a list comprehension:
    [column for row in hsl for column in row[0].split(',')]
    Output:
    ['2', '3', '2', '1']
    You see a list of strings. Not good to compare the highest value. You have to convert it into an integer:
    [int(column) for row in hsl for column in row[0].split(',')]
    You get a list with integers:
    Output:
    [2, 3, 2, 1]
  4. Use the function max, to get the highest value from this flat list:
    max([int(column) for row in hsl for column in row[0].split(',')])
    You use a generator expression inside the built-in function max. Just remove the square brackets and then you've your generator expression inside the function call:
    max(int(column) for row in hsl for column in row[0].split(','))

it's so clear when you explain it to me.
thanks a lot dude..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get the biggest number from a two dimensional list rs74 13 3,944 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Help with array smallest biggest number thanikos 4 3,256 Nov-30-2017, 01:06 PM
Last Post: thanikos

Forum Jump:

User Panel Messages

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