473,385 Members | 1,893 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,385 software developers and data experts.

Getting a string from a list in a list

Magnie
3
I am making a game, and I'm wondering how I can get a string( /element ) out of a list in a list.

Expand|Select|Wrap|Line Numbers
  1. players = [["Magnie","apassword"],["Test","apassword"]]
  2. if username == players[0[0]] and password == players[0[1]]:
  3.     print "Welcome back,",username+"!"
  4.  
Something like that? ( That's not the real code, just something that I want. ;) )
Mar 19 '10 #1

✓ answered by YarrOfDoom

Almost correct, it's players[i][j] instead of players[[i]j] (where i and j are of course the actual values you want to use, or a variable holding that value).

3 1742
YarrOfDoom
1,247 Expert 1GB
Almost correct, it's players[i][j] instead of players[[i]j] (where i and j are of course the actual values you want to use, or a variable holding that value).
Mar 19 '10 #2
leegao
3
One way of doing this is to convert the 2d list into a dictionary via {}.update()
Expand|Select|Wrap|Line Numbers
  1. players_dict = {}
  2. players_dict.update(players)
  3. if username in players_dict: 
  4.     if password == players_dict[username]:
  5.         print "Welcome back,",username+"!"

Another (shorter, but messier) way:
Expand|Select|Wrap|Line Numbers
  1. lst = zip(*players)[0]
  2. if username in lst:
  3.     print "Welcome back" if password == players[lst.index(username)][1] else "Wrong Password"
Mar 20 '10 #3
Magnie
3
Yarr: Ohh, thanks! I thought that if a list is inside a list it would make more sense to do it the other way. :P Thanks again!
Mar 21 '10 #4

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

Similar topics

9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
2
by: Nuno Magalhaes | last post by:
In a sample code I do something like this: //Populate IP List IPHost=Dns.GetHostByName(Dns.GetHostName()); DefaultIPCombo.Items.Clear(); DefaultIPCombo.Items.Add(IPHost.AddressList.ToString());...
2
by: Jay | last post by:
I'm having a weird problem in ASP.NET 1.1. I have two DropDownLists in a form. Both lists are build identically but separately. Somehow the DropDownLists are getting crossed in memory, because when...
11
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to...
2
by: ankit | last post by:
I want to get the start and end of all the patterns mattched in regex. I know I can get it with start() and end() fn of matched objects. But re.search() return the match object of first matching...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
4
by: AshishMishra16 | last post by:
HI friends, I am using the Flex to upload files to server. I m getting all the details about the file, but I m not able to upload it to Server. Here is the code i m using for both flex & for...
1
imrosie
by: imrosie | last post by:
Please help with this one,,,,,I've been trying everything in my arsenal to fix this one. I'm stumped.... I"ve got a unbound combo box (customername) that has two events (on click); AfterUpdate and...
3
by: Alexnb | last post by:
Alexandr N Zamaraev wrote: Excellent! it works. But I have one more question. How can I test to see if the first character of a string is what I want, for example, how can I test to see if the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.