Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:49:26+00:00 2026-06-02T20:49:26+00:00

I’m new to python and I’m having a problem that I’m not able to

  • 0

I’m new to python and I’m having a problem that I’m not able to solve.

I have the following 2D array:

valuearray = [['A', '21', '45'], ['A', '12', '23'], 
              ['A', '54', '21'], ['A', '15', '54'], 
              ['B', '23', '53'], ['B', '34', '53'], 
              ['B', '32', '54'], ['B', '24', '13'], 
              ['C', '31', '43'], ['C', '42', '54'], 
              ['C', '35', '54'], ['C', '12', '11']]

A  21 45
A  12 23
A  54 21
A  15 54
B  23 53
B  34 53
B  32 54
B  24 13
C  31 43
C  42 54
C  35 54
C  12 11

I need to generate from this array another array that have the unique values of valuearray[0] , the maximum of valuearray[1] for each valuearray[0] and the minimum valuearray[2] for each valuearray[0]

The result would be:

resarray[]

    A  54 21
    B  34 13
    C  42 11

EDIT: sorry for not presenting what I tried

    uniquenames = []
    un = []
    for i in range(len(valuearray)):
            un.append(valuearray[i][0])
    uniquenames=uniq(un)

test = []
for ci in range(len(valuearray)):
    for gn in range(len(uniquenames)):
        if(valuearray[ci][0] == uniquenames[gn]):
                      # i don't know what to do here
                      i tried append(valuearray[ci][0] , max(valuearray[ci][1]),min( valuearray[ci][2]))

but append only take one parametre, so I don’t know how to continue.

And uniq is a function that gives me the unique values from a list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:49:31+00:00Added an answer on June 2, 2026 at 8:49 pm

    We can do this pretty easily with itertools.groupby and zip():

    data = [
        ['A', '21', '45'],
        ['A', '12', '23'],
        ['A', '54', '21'],
        ['A', '15', '54'],
        ['B', '23', '53'],
        ['B', '34', '53'],
        ['B', '32', '54'],
        ['B', '24', '13'],
        ['C', '31', '43'],
        ['C', '42', '54'],
        ['C', '35', '54'],
        ['C', '12', '11']
    ]
    
    from itertools import groupby
    from operator import itemgetter
    
    for name, values in groupby(data, itemgetter(0)):
        _, first, second = zip(*values)
        print(name, max(first), min(second))
    

    What we are doing here is using groupby() to group the list items by the first item. This gives us three lists – a list for the records beginning with A, then a list for B, then C. It’s also worth noting that groupby() doesn’t require your list to be sorted.

    We then extract the values from these lists by using zip() to unzip the values from a list of triplets into three lists of single values. We throw away the first column as it’s just A, B or C as is relevant, and then take the maximum and minimum of the other columns to get the values you wanted.

    Which gives us:

    A 54 21
    B 34 13
    C 42 11
    

    Edit:

    If you have your values as text, then you can use a list comprehension and str.split() to make a list out of it:

    data = """\
    A  21 45
    A  12 23
    A  54 21
    A  15 54
    B  23 53
    B  34 53
    B  32 54
    B  24 13
    C  31 43
    C  42 54
    C  35 54
    C  12 11\
    """
    
    data = [value.split() for value in data.split("\n")]
    

    Another Edit:

    As per the chat, you can discard extra columns like so:

    Python 3.x:

    for name, values in groupby(data, itemgetter(0)):
        _, first, second, *_ = zip(*values)
        print(name, max(first), min(second))
    

    Python 2.x:

    for name, values in groupby(data, itemgetter(0)):
        first, second = zip(*values)[1:3]
        print name, max(first), min(second)
    

    And to make the output a list, rather than printing the values:

    def max_min_by_group(group):
        for name, values in group:
            _, first, second, *_ = zip(*values)
            yield [name, max(first), min(second)]
    
    new = [item for item in max_min_by_group(groupby(data, itemgetter(0)))]
    

    We simply use a list comprehension and a generator (we could do this in one big line but it would be unwieldy and unreadable). This gives us:

    [['A', '54', '21'], ['B', '34', '13'], ['C', '42', '11']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.