Python Forum
Split List and Sublist from Pyodbc
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split List and Sublist from Pyodbc
#1
My pyodbc returns cursor result as below
>>> result
[(Decimal('28000.0000'), 2010, 272), (Decimal('308000.0000'), 2011, 272), (Decimal('582000.0000'), 2012, 272), (Decimal('387000.0000'), 2013, 272), (Decimal('367000.0000'), 2010, 281), (Decimal('3037000.0000'), 2011, 281), (Decimal('4822000.0000'), 2012, 281), (Decimal('2936000.0000'), 2013, 281), (Decimal('637000.0000'), 2010, 282), (Decimal('3440000.0000'), 2011, 282), (Decimal('4415000.0000'), 2012, 282), (Decimal('3294000.0000'), 2013, 282), (Decimal('478000.0000'), 2010, 296), (Decimal('1209000.0000'), 2011, 296), (Decimal('478000.0000'), 2012, 296), (Decimal('1209000.0000'), 2013, 296)]
>>>
how to get data in list as below
Salesamount should split based on the year to sub list.

Salesamount = [[28000.00,308000.00,582000.00,387000.00],[367000.00,3037000.00,4822000.00,2936000.00],[637000.00,3440000.00,4415000.00,3294000.00],[478000.00,1209000.00,478000.00,1209000.00]]
CalendarYear = [2010,2011,2012,2013]
Employee = [272,281,282,296]
Also how to remove decimal as it is displaying as decimal in the list
>>> type(result)
<class 'list'>
Reply
#2
The result you're showing is a list of tuples
so something like (you can build your lists from this (replace print with list creation code):
Decimal = float
results = [
    (Decimal('28000.0000'), 2010, 272), (Decimal('308000.0000'), 2011, 272),
    (Decimal('582000.0000'), 2012, 272), (Decimal('387000.0000'), 2013, 272),
    (Decimal('367000.0000'), 2010, 281), (Decimal('3037000.0000'), 2011, 281),
    (Decimal('4822000.0000'), 2012, 281), (Decimal('2936000.0000'), 2013, 281),
    (Decimal('637000.0000'), 2010, 282), (Decimal('3440000.0000'), 2011, 282),
    (Decimal('4415000.0000'), 2012, 282), (Decimal('3294000.0000'), 2013, 282),
    (Decimal('478000.0000'), 2010, 296), (Decimal('1209000.0000'), 2011, 296),
    (Decimal('478000.0000'), 2012, 296), (Decimal('1209000.0000'), 2013, 296)
]

for decimal, year, employee_id in results:
    print(f'\ndecimal: {decimal}\nYear: {year}\nEmployeeId: {employee_id}')
output:
Output:
decimal: 28000.0 Year: 2010 EmployeeId: 272 decimal: 308000.0 Year: 2011 EmployeeId: 272 decimal: 582000.0 Year: 2012 EmployeeId: 272 decimal: 387000.0 Year: 2013 EmployeeId: 272 decimal: 367000.0 Year: 2010 EmployeeId: 281 decimal: 3037000.0 Year: 2011 EmployeeId: 281 decimal: 4822000.0 Year: 2012 EmployeeId: 281 decimal: 2936000.0 Year: 2013 EmployeeId: 281 decimal: 637000.0 Year: 2010 EmployeeId: 282 decimal: 3440000.0 Year: 2011 EmployeeId: 282 decimal: 4415000.0 Year: 2012 EmployeeId: 282 decimal: 3294000.0 Year: 2013 EmployeeId: 282 decimal: 478000.0 Year: 2010 EmployeeId: 296 decimal: 1209000.0 Year: 2011 EmployeeId: 296 decimal: 478000.0 Year: 2012 EmployeeId: 296 decimal: 1209000.0 Year: 2013 EmployeeId: 296
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using pyodbc&pandas to load a Table data to df tester_V 3 746 Sep-09-2023, 08:55 PM
Last Post: tester_V
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 1,235 Jul-11-2022, 08:52 AM
Last Post: japo85
  pyodbc gmerritt 8 2,798 Feb-21-2022, 07:21 PM
Last Post: gmerritt
  Use one list as search key for another list with sublist of list jc4d 4 2,105 Jan-11-2022, 12:10 PM
Last Post: jc4d
Question Sublist/ Subarray into string Python SantiagoPB 2 2,096 Apr-23-2021, 07:03 PM
Last Post: SantiagoPB
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,251 Aug-17-2020, 03:44 PM
Last Post: bearcats6001
  Get database used data space from pyodbc susja 1 2,201 Aug-14-2020, 02:01 PM
Last Post: susja
  List of objects with sublist medatib531 4 2,274 Mar-01-2020, 06:16 PM
Last Post: buran
  [split] question about list comprehension Armin 17 5,522 Jan-29-2020, 04:32 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

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