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?
9 1849
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?
-
>>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
-
>>> print [item[1] for item in aList]
-
[3, 5, 5, 2, 2]
-
>>>
-
>>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
-
>>> print [item[1] for item in aList]
-
[3, 5, 5, 2, 2]
-
>>>
This will strip the braces off the ends: -
>>> print str([item[1] for item in aList])[1:-1]
-
3, 5, 5, 2, 2
-
>>>
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]?
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: -
-
a_list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]
-
-
index = 0
-
while index != len(a_list): #len(a_list) lenght of the list
-
print a_list[index][1] #prints a_list[0][1],[1][1],[2][1],etc...
-
index += 1
-
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 =)
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?
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 -
list_a = [[2,3],[4,5],[2,5],[9,2],[3,2]]
-
new_list = []
-
-
index1 =0
-
while index1 != len(list_a): #this is looping "index[0]" values
-
-
index2 = 0
-
samevalue = 0 #"reseting" this value for the next round
-
while index2 != len(list_a): #this is looping "index[1]" values
-
-
if list_a[index1][0] == list_a[index2][1]: #checking if we have same number in "index[0]" & "index[1]"
-
samevalue = 1 #let's remember that there were same number
-
-
index2 += 1 #let's try different "index[1]" value
-
-
if samevalue == 0: #if we didn't find pair for the number, samevalue is still 0
-
new_list.append(list_a[index1][0]) # .append saves the value to new list (adds it to the end of list)
-
-
index1 += 1 #trying the next "index[0]"
-
-
print new_list
-
bvdet 2,851
Expert Mod 2GB
This method uses list comprehensions: - >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
-
>>> prntList = [item[1] for item in aList]
-
>>> print ", ".join([str(item) for item in prntList])
-
3, 5, 5, 2, 2
-
>>> print ", ".join([str(item[0]) for item in aList if item[0] not in prntList])
-
4, 9
-
>>>
unorthodox way: -
>>> str(list).replace("[","").replace("]","").split(",")[1::2]
-
[' 3', ' 5', ' 5', ' 2', ' 2']
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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:...
|
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...
|
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...
|
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...
|
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...
|
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' }
|
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...
|
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...
|
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
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |