I have 3 lists:
color = ['red','orange','purple','black']
number = ['2','4','6','77']
letter = ['K','R','U','Z']
what I want to end up with is:
mylist=[['K','R','U','Z','red','2'],['K','R','U','Z','orange','4'],['K','R','U','Z','purple','6'], ['K','R','U','Z','black','77']]
I tried:
for i in range(4):
letter.append(color[i])
letter.append(number[i])
This does not give me what I need.
You can use a list comprehension for that :
color = ['red','orange','purple','black']
number = ['2','4','6','77']
letter = ['K','R','U','Z']
mylist = [ letter + [c, n] for c, n in zip(color, number) ]
print(mylist)
Not so beautiful but pretty simple in my opinion:
color = ['red', 'orange', 'purple', 'black']
number = ['2', '4', '6', '77']
letter = ['K', 'R', 'U', 'Z']
my_list = []
for i in range(len(color)):
new_list = letter[:]
my_list.append(new_list)
new_list.append(color[i])
new_list.append(number[i])
print(my_list)
Output:
[['K', 'R', 'U', 'Z', 'red', '2'], ['K', 'R', 'U', 'Z', 'orange', '4'], ['K', 'R', 'U', 'Z', 'purple', '6'], ['K', 'R', 'U', 'Z', 'black', '77']]
Related
I have a list: messages = ['d h y u y r w u u r', 'y h w e u h e h d d'].
The list can be longer than this, and contain strings of greater lengths.
How do I extract each string element of the list messages to be in their own list?
The spaces are important..I don't want to include them in the list.
This is what I tried:
newlist=[]
for i in messages:
for j in messages[0]:
if j != ' ':
newlist+=[j]
print(newlist)
This is what I got:
['d', 'h', 'y', 'u', 'y', 'r', 'w', 'u', 'u', 'r']
['d', 'h', 'y', 'u', 'y', 'r', 'w', 'u', 'u', 'r', 'd', 'h', 'y', 'u', 'y', 'r', 'w', 'u', 'u', 'r']
I know that messages[0] would only look at the first index of messages, but when i try messages[i], I get an error that says:
<TypeError: list indices must be integers or slices, not str>
What is the result you are trying to get?
If you want all letters in one list regardless of which one of the strings in the initial list they were from:
newlist=[]
for i in messages:
for j in i:
if j != ' ':
newlist+=[j]
print(newlist)
gives
['d', 'h', 'y', 'u', 'y', 'r', 'w', 'u', 'u', 'r', 'y', 'h', 'w', 'e', 'u', 'h', 'e', 'h', 'd', 'd']
as in your first line "i" will be the string type entry of "messages" (not its index)
OR, if you want to keep the initial hierarchy:
newlist=[]
for i in messages:
new_sublist = []
for j in i:
if j != ' ':
new_sublist+=[j]
newlist += [new_sublist]
print(newlist)
or, shorter
[sub_message.split() for sub_message in messages]
both give
[['d', 'h', 'y', 'u', 'y', 'r', 'w', 'u', 'u', 'r'], ['y', 'h', 'w', 'e', 'u', 'h', 'e', 'h', 'd', 'd']]
I have these lists in Python
text_0 = ['Weight','Weight', 'Weight']
text_1 = [['x','y','z'],['x','y','z'],['x','y','z']]
text_2 = [['1','2,','3'],['4','5','6'],['7','8','9']]
I would like would to create a new list like this
new_list = ['Weight','x','1','y','2','z','3','Weight','x','4','y','5','z','6','Weight','x','7','y','8','z','9']
How do I do this in Python?
In your case we can do with python
sum([[x] + [ items for item in zip(y,z) for items in item]
for x, y, z in zip(text_0,text_1,text_2)],[])
['Weight', 'x', '1', 'y', '2', 'z', '3', 'Weight', 'x', '4', 'y', '5', 'z', '6', 'Weight', 'x', '7', 'y', '8', 'z', '9']
I am not able to modify the code when I implement minimum password length like minimum length must be 8 I tried using a while loop but code is not running as expected Please help me in this case
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your
password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
password.extend(random.sample(letters, nr_letters))
password.extend(random.sample(symbols, nr_symbols))
password.extend(random.sample(numbers, nr_numbers))
random.shuffle(password)
finalPassword = ""
print(f"Here is you password: {finalPassword.join(password)}")
Simply keep the input statement within a while loop that checks your criteria:
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters = 0
nr_symbols = 0
nr_numbers = 0
while nr_letters + nr_symbols + nr_numbers < 8:
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
password.extend(random.sample(letters, nr_letters))
password.extend(random.sample(symbols, nr_symbols))
password.extend(random.sample(numbers, nr_numbers))
random.shuffle(password)
finalPassword = ""
print(f"Here is you password: {finalPassword.join(password)}")
Could someone describe to me how I can have the same results as shown in approach 1, but using an approach 2? Both of which are using List Comprehension.
Approach 1
planets = 'MercuryVenusEarthMarsJupiterSaturnUranusNeptune'
capital_letters = [planet for planet in planets if planet.isupper()]
>>> ['M', 'V', 'E', 'M', 'J', 'S', 'U', 'N']
Approach 2
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
capital_letters = [planet for planet in planets if planet.islower()]
>>> []
Some ideas:
To use slicing;
Try the below code, it gives the output as in approach 1 for your approach 2.
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
capital_letters = [planet[0] for planet in planets]
>>>['M', 'V', 'E', 'M', 'J', 'S', 'U', 'N']
I have 2 matrices:
list_alpha = [['a'],
['b'],
['c'],
['d'],
['e']]
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
And my goal is if a letter from list_alpha is in a sublist of list_beta, then the first element of that line in list_beta (the #) is added to the correct line in list_alpha.
So my output would be:
final_list = [['a', '1', '3', '4'],
['b', '1'],
['c', '4'],
['d', '2', '4'],
['e', '1']]
But I'm pretty new to python and coding in general and I'm not sure how to do this. Is there a way to code this? Or do I have to change the way the data is stored in either list?
Edit:
Changing list_alpha to a dictionary helped!
Final code:
dict_alpha = {'a': [], 'b': [], 'c': [], 'd': [], 'e':[]}
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
['5', 'X', 'X', 'e'],
['6', 'c', 'X', 'X']]
for letter in dict_alpha:
for item in list_beta:
if letter in item:
dict_alpha.get(letter).append(item[0])
print(dict_alpha)
You can use dict_alpha as same as list_alpha , then fix your for loop.
For example:
dict_alpha = [['a'],
['b'],
['c'],
['d'],
['e']]
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
['5', 'X', 'X', 'e'],
['6', 'c', 'X', 'X']]
for al in dict_alpha:
for bt in list_beta:
for i in range(1, len(bt)):
if (bt[i] == al[0]):
al.append(bt[0])
print(dict_alpha)
Output:
[['a', '1', '3', '4'],
['b', '1'],
['c', '4', '6'],
['d', '2', '4'],
['e', '1', '5']]
Hope to helpful!