473,396 Members | 1,754 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,396 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 1849
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.