Posts: 30
Threads: 8
Joined: Jul 2017
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 30
Threads: 8
Joined: Jul 2017
(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
Posts: 30
Threads: 8
Joined: Jul 2017
Jul-14-2017, 06:24 AM
(This post was last modified: Jul-14-2017, 10:05 AM by Larz60+.)
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 ?
Posts: 4,220
Threads: 97
Joined: Sep 2016
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].
Posts: 30
Threads: 8
Joined: Jul 2017
Jul-19-2017, 04:20 AM
(This post was last modified: Jul-19-2017, 04:20 AM by dwiga.)
(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..
Posts: 4,220
Threads: 97
Joined: Sep 2016
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(',')])
Posts: 2,128
Threads: 11
Joined: May 2017
(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..
- 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'
- Split the first element of the tuple. The separator is a comma:
('2,3',)[0].split(',') It returns a list:
Output: ['2', '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]
- 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(','))
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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 .
Posts: 30
Threads: 8
Joined: Jul 2017
Jul-20-2017, 01:28 AM
(This post was last modified: Jul-20-2017, 01:44 AM by dwiga.)
(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..
- 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'
- Split the first element of the tuple. The separator is a comma:
('2,3',)[0].split(',') It returns a list:
Output: ['2', '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]
- 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..
|