Python, keep successively increasing longer list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python, keep successively increasing longer list
# 1  
Old 01-06-2009
Python, keep successively increasing longer list

Suppose I have a
listname=["john" "mary" "anne" "lisa"]

I want to generate the following as output

john
john mary
john mary anne
john mary anne lisa


Is there any way to do this?
print listname[0]
print listname[0] listname[1]
print listname[0] listname[1] listname [2]
.......

what is a smart way of putting this into a loop structure? Thanks.
# 2  
Old 01-07-2009
Code:
for i in range(0, len(listname)+1):
     print ' '.join(listname[0:i])

# 3  
Old 01-07-2009
Code:
awk -F\" '{for(i=2;i<=NF;i=i+2){a=a" "$i;print a}}'


Last edited by radoulov; 01-07-2009 at 07:00 AM.. Reason: fixed code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Search or find a element inside list python

I have a list as follows: From this i need to grep the element using keyword as "primary" and return output as 12:13-internet-wifi-primary i used as follows if (i <= (len(system_info))): ss = system_info print... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

2. Shell Programming and Scripting

Python concat list with a string variable

Hello! #!/usr/bin/env python hn = "simsa-svr" for container in containerslist: Name = container I want to print Name along with hn i,e simsa-svr. I tried like Name = container'-'.join(hn) did not work. Needed result: lilly2232321-simsa-svr (2 Replies)
Discussion started by: ashokvpp
2 Replies

3. Post Here to Contact Site Administrators and Moderators

LinkedIn tiles on Members list no longer working?

Is it just me or are the links to LinkedIn on the "Members" list not working any more? Clicking on Facebook, YouTube, etc work just fine. I'm getting a 404 error (page no longer exists) but I know for sure that my page is there. Perhaps there's something wrong with my browser? Any ideas? (7 Replies)
Discussion started by: hicksd8
7 Replies

4. Programming

Python Regex List Creation

Here is a snippet of my code: blahblahblah... blah for link in goodies.soup.find_all('a'): blah.append(link.get('href')) blah=list(set(blah)) which gives my list of urls. So now I use a regex to search for the relevant urls which I want in a list: for r... (0 Replies)
Discussion started by: metallica1973
0 Replies

5. Shell Programming and Scripting

Python: list with tuples to a file

I have few list value which are shown as below and want to print the same to a text file in python. But I couldn't able to write to a file. Could anyone please help me how to print the list with tuples to a file . My code: ====== fout=open('output.txt','w') mylist = fout.write()... (1 Reply)
Discussion started by: scriptscript
1 Replies

6. Shell Programming and Scripting

Searching for a list of strings in a file with Python

Hi guys, I'm trying to search for several strings, which I have in a .txt file line by line, on another file. So the idea is, take input.txt and search for each line in that file in another file, let's call it rules.txt. So far, I've been able to do this, to search for individual strings: ... (1 Reply)
Discussion started by: starriol
1 Replies

7. UNIX for Dummies Questions & Answers

Run executable in one directory and then move to another successively

Hello, I have several hundred subdirectories which contain input files for a binary executable. I need to get into each of the subdirectories, run the executable and then move to the next one and repeat the process. What is the best way to do this? Arbitrarily my file structures look like... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

8. UNIX for Dummies Questions & Answers

apply a function twice successively with the same input in awk program

Hi ! It is a general question. When an awk script looks like: #! bin/awk function example(i){ <body> } { example(1) #the function uses input_1 and return output_a } { example(2) #the function uses previous output_a as an input and returns... (15 Replies)
Discussion started by: beca123456
15 Replies

9. Shell Programming and Scripting

[python]string to list conversion

I have a file command.txt. It's content are as follows:- The content of file is actually a command with script name and respective arguments. arg1 and arg2 are dummy arguments , format : -arg arg_value test is a argument specifying run mode , format : -arg In my python code, i read it and... (1 Reply)
Discussion started by: animesharma
1 Replies

10. UNIX for Dummies Questions & Answers

Extract n number of lines from a file successively

Hello, I have a file with over 100,000 lines. I would like to be able extract 5000 lines at a time and give it as an input to another program. sed -n '1,5000p' <myfile> > myOut Similarly for 5001-10000 10001-15000 .... How can I do this in a loop? Thanks, Guss (5 Replies)
Discussion started by: Gussifinknottle
5 Replies
Login or Register to Ask a Question