0
Go to python>>moretypes>>usefulfunctions>>list function.....I didnot understand any thing😑
9 Antworten
+ 7
To see the lists you are testing, you can put in these ines:
print([i > 5 for i in nums])
print([i % 2 == 0 for i in nums])
+ 7
Omar o You can find the functions here https://docs.python.org/3/library/functions.html. The three functions you are asking about involve iteration, which means going through a sequence of things one at a time.
As Max said, enumerate() produces tuples containing each index position (beginning with 0) and the corresponding value. all() is True only if the test (e.g. i > 5) is True for all members of the sequence. any() only requires one to be True.
+ 5
You need a program for those to make sense. I've never had a program that I'd use them in. But, consider a grading program, where you make honor roll if all grades are 80 or higher. That is a perfect place to use all. In that same program, you could use any to decide the student must repeat the year because of a score below 60. This is the enumerate form I'd use:
menu = ["exit", "add", "subtract", "multiply", "divide"]
print("Main menu:")
for command, item in enumerate(menu):
print(command, item)
print("Enter command number: ", end="")
cmd = int(input())
+ 4
Did you use the 'Try It Yourself'? That would be the best way to figure out what is happening. The first one is like this:
print(", ".join(["spam", "eggs", "ham"]))
#prints "spam, eggs, ham"
You call join with self of ", " and list of ["spam", "eggs", "ham"]. The results get printed as:
spam, eggs, ham
If we change things to:
print("-".join(["maps", "to", "go"]))
It prints:
maps-to-go
This should help you understand what join does. It makes a single string out of the strings in the list using the self string as the characters between them.
print("".join(["good", "bye"]))
outputs:
goodbye
placing an empty string between the two words.
+ 2
If you have a list l = [1,2,3,4,5,6] then [x>3 for x in l] will give you a list that looks like this : [false,false,false,true,true,true] so it gives you true in the places corresponding to the elements x in the list l if the condition x>3 is true and false otherwise
Any checks if the list contains any true value and all checks if all the values in the list are true
Enumerate just gives each element in the list an index that gets attached to each element via a tuple so for example enumerate(['a','b','c']) gives you [(1,'a'),(2,'b'),(3,'c')]
+ 2
With Any and all you can quickly check if a condition holds for all or any elements of the list. For example if the list contained the wage of employees you could use all to check if every employee makes more than minimum wage
By doing something like all([x>min_wage for x in employee_wages]). You could do the same using for loops but this way you need to type less.I have never used enumerate but it could be useful if you have multiple lists of the same length and need to switch between them. U could use enumerate to generate the right indices quickly
+ 1
Thank u all😊
0
I am talking about this code
nums = [55, 44, 33, 22, 11]
if all([i > 5 for i in nums]):
print("All larger than 5")
if any([i % 2 == 0 for i in nums]):
print("At least one is even")
for v in enumerate(nums):
print(v)
0
what is the benefit of all and any