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

  • Home
  • SEARCH
  • 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 3285026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:13:14+00:00 2026-05-17T20:13:14+00:00

I am trying to learn Python lists. In this code I am trying to

  • 0

I am trying to learn Python lists. In this code I am trying to add a string s coming from a form to table row as long as the string is the same. When the string is different; the new string is written to the next column.

I could write the string from column 0 to column 1 but I had problems adding the same string on column 1 correctly.

The way it is; the script works only up to placing the different string to the next column.

I realize that this is not the right way of doing this.

I would appreciate any help. Thank you. I include the template too.

EDIT2

@JerseyMike: Thanks for your answer. I don’t yet understand how AddString(str) works but trying it in IDLE I noticed that instead of adding the new string it replaces it with the new string. As I said, I have not yet studied how it works; but here is the result (I changed str to str1):

>>> def AddString(str1):
    try:
        idx = map(lambda (s, v): ('N', 'Y')[s == str1], L).index('Y')
    except:
        L.append((str1, 1))
    else: L[idx] = (str1, L[idx][1] + 1)


>>> L = []
>>> str1 = 'hello'
>>> AddString(str1)
>>> L
[('hello', 1)]
>>> AddString(str1)
>>> L
[('hello', 2)]
>>> 

EDIT

@JerseyMike:

Thanks, I am sorry, I realized the question was not clear. In this app; the user types in the same sentence; like practicing foreign language. So the input will be

Hello world
Hello world
Hello world

and if the user types in next “Hello universe” that will go to the next column:

Hello world  Hello Universe
Hello world
Hello world

and if the user keeps typing “Hello Universe” they should go under the same column

Hello world  Hello Universe
Hello world  Hello Universe
Hello world  Hello Universe
             Hello Universe
             Hello Universe

The list L containing this looks like this:

L = [
     ['Hello world', 'Hello Universe'], 
     ['Hello world', 'Hello Universe'], 
     ['Hello world', 'Hello Universe'], 
     ['',            'Hello Universe'], 
     ['',            'Hello Universe']
    ]

Initially the list is empty and I add the string s with L.append(s).

L = [
     ['Hello world'], 
     ['Hello world'], 
     ['Hello world'], 
    ]

If the last string s does not match the new input I create the new column with L[0].insert(1,s).

L = [
     ['Hello world', 'Hello Universe'], 
     ['Hello world'], 
     ['Hello world'], 
    ]             

Now I need to write under 'Hello Universe' This turned out to be difficult for me to figure out for several reasons. But now I think it may be better to append the new string s to the list before checking if it is same as the previous string. To simplify the list, assume L is like this:

L = [['A'], ['A'], ['A'], ['B']]

Now ['B'] needs to be inserted into L[0]. To do this I search the list to the left to find the last sub-list with 1 element (or something like this). I have not looked into how to search lists yet. Thanks again for the help.

END EDİT


class Test(webapp.RequestHandler):
    myList = []
    def get(self):        
#        del self.myList[:]        
        s = [self.request.get('sentence')]
        r = len(self.myList)

        if r == 0: 
            self.myList.append(s)
            htmlcode1 = HTML.table(self.myList)
            lastItem = s  
        else:   
            if len(self.myList[0]) == 1:
                lastItem = self.myList[r-1]        
                if s == lastItem:
                    self.myList.append(s)             
                    htmlcode1 = HTML.table(self.myList)                
                else:
                    s = self.request.get('sentence') 
                    self.myList[0].insert(1,s)       
                    htmlcode1 = HTML.table(self.myList)
            if len(self.myList[0]) == 2:                
                    self.myList[1].insert(1,s)
                    htmlcode1 = HTML.table(self.myList)
            elif len(self.myList[1]) == 2:             
                    self.myList[2].insert(1,s)
                    htmlcode1 = HTML.table(self.myList)

        template_values = {'htmlcode1': htmlcode1,
                           's': s,
                           'r': r,
                           'myList': self.myList,
#                           'myListLen': myListLen,
                           'lastItem': lastItem,
                          }

        path = os.path.join(os.path.dirname(__file__), 'test.mako')
        templ = Template(filename=path)
        self.response.out.write(templ.render(**template_values))    

Template

<html>
<head>
</head>
<body>

<p>| <a href="/delete">CLEAR</a> |</p>

<form action="/test" method="get">
<input name="sentence" type="text" size="30"><br />
      <input type="submit" value="Enter">
</form>

<p>${htmlcode1}</p>
<p>s: ${s}</p>
<p>r: ${r}</p>
<p>myList: ${myList}</p>
<p>lastItem: ${lastItem}</p>

</html>
</body>
  • 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-05-17T20:13:15+00:00Added an answer on May 17, 2026 at 8:13 pm

    If the order of the strings is important, a dictionary will be a problem. You are on the right path using lists, but I think you need more than just that. Honestly, I’m not clear on what your output will look like for different data sets. If I read your question correctly, is the following true?

    Input:

    Sentence 1
    Sentence 3
    Sentence 1
    Sentence 1
    Sentence 2
    Sentence 2
    

    Output after last line:

    Sentence 1 | Sentence 3 | Sentence 2
    Sentence 1 |            | Sentence 2
    Sentence 1 |            |
    

    If not, please rephrase your expected output with an example.


    Ok, so it looks like my take on your problem was correct. That’s a good start. 😉

    I think we need to look at this from a different perspective. Currently, you are looking at this as a big list of all the data and maybe that’s too much. What I see here is a list of tuples, where each tuple represents a column (string, count). The tuple will be what string should be in this column and how many of them should be there. So your example would end up looking like:

    L = [("Hello World", 3), ("Hello Universe", 5)]

    I know this isn’t obvious as to how to deal with this data, but I think it’s the right way to represent it internally. I’ll give you some sample code to do some basic manipulations of this data type (others may have more efficient ways of doing the same thing).

    Add a new String:

    def AddString(str):
    try:
        idx = map(lambda (s, v): ('N', 'Y')[s == str], L).index('Y')
    except:
        L.append( (str, 1) )
    else:
        L[idx] = (str, L[idx][1] + 1)
    

    Print the inside of an HTML table:

    def PrintChart():
    try:
        max = reduce(lambda x, y: [x, y][x[1] < y[1]], L)[1]
    except:
        print "<td></td>"
    else:
        for i in range(max):
            print "<tr>"
            for (s, c) in L:
                if i+1 <= c:
                    print "  <td" + s + "</td>"
                else:
                    print "  <td></td>"
            print "</tr>"
    

    So anyway, that is the way I’d do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.