0

I am working on a python code I have used temp variable because If there is any content present, that will be appended to temp variable and then it will be appended to header.
and if there is no content then temp variable will be appended.

so, it will restrict to append '' values to the list.

I am looking for any other finer method to get the same result but not using temp variable.
Any suggestions will be helpful.

    temp = ''
    header = []
    for ind,content in enumerate(data):   # enumerating for index & content in data
        if content and ind != 0:
            temp = content
            header.append(content)
        else:
            header.append(temp)

input :

['column1', '', '', '', 'column2', '', '']

expected output :

['column1', 'column1', 'column1', 'column1', 'column2', 'column2', 'column2']
4
  • 1
    Could you give some examples of input and output? Commented Apr 14, 2012 at 7:30
  • updated the question with expected output for input Commented Apr 14, 2012 at 7:35
  • 2
    may the first entry of the input list equal '' ? Commented Apr 14, 2012 at 7:44
  • I think the problem is naming temp variable as temp. It confused me for half of a minute what the program supposes to do. Renaming it to something like "previous_header" might help solving your problem in the first place. Although I am speaking from an observer point of view, naming variable is really important. Commented Apr 14, 2012 at 8:01

4 Answers 4

4

You can try something like this

>>> data=['column1', '', '', '', 'column2', '', '']
>>> header=data[:]
>>> for i in range(1,len(header)):
    if not header[i]:
        header[i]=header[i-1]


>>> header
['column1', 'column1', 'column1', 'column1', 'column2', 'column2', 'column2']
>>> 

Note** I have updated the answer to as not to update the original list.

Sign up to request clarification or add additional context in comments.

1 Comment

The above modifies the list data. If that's not allowed, make a copy first: header = list(data). Then you also need to handle the zero case: if data[0] is empty, what should it become? The original code leaves the output list element header[0] empty.
4
for i, x in enumerate(data[1:], 1):
    if not x:
        data[i] = data[i-1]

# or
for i, x in enumerate(data[1:]):
    if not x:
        data[i+1] = data[i]

Comments

3

Another version of the temp version

Code:

REPEAT = object()
def repeated(iterable):
    for x in iterable:
        R = last if (x is REPEAT) else x
        yield R
        last = R

Demo:

>>> input = ['column1', REPEAT, REPEAT, REPEAT, 'column2', REPEAT, REPEAT]
>>> list( repeated(input) )
['column1', 'column1', 'column1', 'column1', 'column2', 'column2', 'column2']

1 Comment

+1 for the nice trick. For a moment I thought you were writing something in PASCAL.... I don't know why.
1

If you have the flexibility, you could just do:

>>> ['column1']*4 + ['column2']*3
['column1', 'column1', 'column1', 'column1', 'column2', 'column2', 'column2']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.