473,218 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Printing only one index of a list

If i had a list of lists, for example:

list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

how can i write a function that only writes out index[1] of each list in the list?

i need the output to contain 3,5,4,2,2

and then my final goal is to create a loop that goes through the major list and only print out index[0] of the minor lists if they are not in index[1], so in this case, 4 and 9. I tried using a loop in a loop, but its not working. any ideas?
Nov 26 '07 #1
9 1840
bartonc
6,596 Expert 4TB
If i had a list of lists, for example:

list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

how can i write a function that only writes out index[1] of each list in the list?

i need the output to contain 3,5,4,2,2

and then my final goal is to create a loop that goes through the major list and only print out index[0] of the minor lists if they are not in index[1], so in this case, 4 and 9. I tried using a loop in a loop, but its not working. any ideas?
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
  2. >>> print [item[1] for item in aList]
  3. [3, 5, 5, 2, 2]
  4. >>> 
Nov 26 '07 #2
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
  2. >>> print [item[1] for item in aList]
  3. [3, 5, 5, 2, 2]
  4. >>> 
This will strip the braces off the ends:
Expand|Select|Wrap|Line Numbers
  1. >>> print str([item[1] for item in aList])[1:-1]
  2.  3, 5, 5, 2, 2
  3. >>> 
Nov 26 '07 #3
but then could i actually put that output into a list? like, have all the index[1]'s in a list so that i could use it to search back through and remove any index[0] values that are also in index[1]?
Nov 26 '07 #4
dazzler
75
If i had a list of lists, for example:

list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

how can i write a function that only writes out index[1] of each list in the list?
my version ^_^

do not use "list" as your list's variable name

so, in your example
list[0] = [2,3]
list[1] = [4,5] ....
so:
list[0][0] = 2
list[0][1] = 3
list[1][0] = 4
list[1][1] = 5

the loop:
Expand|Select|Wrap|Line Numbers
  1.  
  2. a_list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]
  3.  
  4. index = 0
  5. while index != len(a_list):    #len(a_list) lenght of the list
  6.    print a_list[index][1]        #prints a_list[0][1],[1][1],[2][1],etc...
  7.    index += 1
  8.  
Nov 26 '07 #5
dazzler
75
list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

and then my final goal is to create a loop that goes through the major list and only print out index[0] of the minor lists if they are not in index[1], so in this case, 4 and 9. I tried using a loop in a loop, but its not working. any ideas?
I don't understand this one, how you get 4 & 9 ? uhm, can you explain more please =)
Nov 26 '07 #6
I don't understand this one, how you get 4 & 9 ? uhm, can you explain more please =)

because 4 and 9 are the only numbers that appear in index[0] and do not appear in index[1], does that make sense?
Nov 26 '07 #7
dazzler
75
oh, it is searching the whole list for duplicates, for each index[0] values, now I understand =D

here's the code =)
I have used index[0] & index[1] names in comments to make it maybe a little clearer for you

Expand|Select|Wrap|Line Numbers
  1. list_a = [[2,3],[4,5],[2,5],[9,2],[3,2]]
  2. new_list = []
  3.  
  4. index1 =0
  5. while index1 != len(list_a): #this is looping "index[0]" values
  6.  
  7.    index2 = 0
  8.    samevalue = 0 #"reseting" this value for the next round
  9.    while index2 != len(list_a): #this is looping "index[1]" values
  10.  
  11.       if list_a[index1][0] == list_a[index2][1]: #checking if we have same number in "index[0]" & "index[1]"
  12.          samevalue = 1 #let's remember that there were same number
  13.  
  14.       index2 += 1 #let's try different "index[1]" value
  15.  
  16.    if samevalue == 0: #if we didn't find pair for the number, samevalue is still 0
  17.       new_list.append(list_a[index1][0]) # .append saves the value to new list (adds it to the end of list)
  18.  
  19.    index1 += 1 #trying the next "index[0]"
  20.  
  21. print new_list
  22.  
Nov 27 '07 #8
bvdet
2,851 Expert Mod 2GB
This method uses list comprehensions:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
  2. >>> prntList = [item[1] for item in aList]
  3. >>> print ", ".join([str(item) for item in prntList])
  4. 3, 5, 5, 2, 2
  5. >>> print ", ".join([str(item[0]) for item in aList if item[0] not in prntList])
  6. 4, 9
  7. >>> 
Nov 27 '07 #9
ghostdog74
511 Expert 256MB
unorthodox way:
Expand|Select|Wrap|Line Numbers
  1. >>> str(list).replace("[","").replace("]","").split(",")[1::2]
  2. [' 3', ' 5', ' 5', ' 2', ' 2']
  3.  
Nov 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Mario | last post by:
Hello all, I'm trying hard to make possible to print some simple text from python to the default printer using wxPython, after days of internet searches I found this page:...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
16
by: cyranoVR | last post by:
This is the approach I used to automate printing of Microsoft Access reports to PDF format i.e. unattended and without annoying "Save As..." dialogs, and - more importantly - without having to use...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
3
by: Mikael Syska | last post by:
Hi, I'm reading Beginning Visual C-Sharp by wrox, great book by the way. In the book the describe how I can print, and it works great, but what if I has like 300 lines, that wont fit on a...
5
by: micklee74 | last post by:
hi i have a list with contents like this alist = how can i "convert" this list into a dictionary such that dictionary = { '>QWER':'askfhs' , '>REWR' : 'sfsdf' , '>FGDG', 'sdfsdgffdgfdg' }
0
by: Vincent | last post by:
Dear all, I have implemented a class to export the content of RichTextBox to image in WYSISYG mode so that line breaks on the screen are the same as exported. C# Code: public struct...
2
by: CC | last post by:
Hi: I've conjured up the idea of building a hex line editor as a first real Python programming exercise. To begin figuring out how to display a line of data as two-digit hex bytes, I created...
13
by: Umesh | last post by:
I was just wondering how to print two strings coulmnwise. char string1="abcd"; char string2="efgh"; now i want to print something like this: a e b f c g
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.