Hướng dẫn how do you evaluate a list in python? - làm thế nào để bạn đánh giá một danh sách trong python?

Điều này có lẽ khá đơn giản, nhưng tôi có một danh sách với các chuỗi cũng tương ứng với các tên biến:

listname = ['name1', 'name2," ... ]

name1 = "somestring"
name2 = "some other string"

Những gì tôi muốn có thể làm là một cái gì đó như:

for variable in listname:
    [perform some operation on the string associated with the variables 
    named in listname, i.e. "somestring" and then "some other string," etc.]

Có một cách dễ dàng để buộc đánh giá các chuỗi trong listname là biến?

Đã hỏi ngày 15 tháng 7 năm 2013 lúc 5:07Jul 15, 2013 at 5:07

Hướng dẫn how do you evaluate a list in python? - làm thế nào để bạn đánh giá một danh sách trong python?

Bạn không muốn làm điều này. Sử dụng từ điển:

d = {'name1':name1, 'name2':name2}

for myvar in listname:
    myvar = d.get(myvar)
    do_stuff(myvar)

Đã trả lời ngày 15 tháng 7 năm 2013 lúc 5:10Jul 15, 2013 at 5:10

Hướng dẫn how do you evaluate a list in python? - làm thế nào để bạn đánh giá một danh sách trong python?

TerryaterryaTerryA

57.3K11 Huy hiệu vàng117 Huy hiệu bạc139 Huy hiệu đồng11 gold badges117 silver badges139 bronze badges

0

Có những lúc điều này có thể hữu ích

for variable in listname:
    target = vars().get(variable)

Thông thường, tốt hơn là chỉ cần có một danh sách các đối tượng hoặc sử dụng một không gian tên riêng như @haidro gợi ý

Đã trả lời ngày 15 tháng 7 năm 2013 lúc 5:10Jul 15, 2013 at 5:10

Hướng dẫn how do you evaluate a list in python? - làm thế nào để bạn đánh giá một danh sách trong python?

TerryaterryaJohn La Rooy

57.3K11 Huy hiệu vàng117 Huy hiệu bạc139 Huy hiệu đồng51 gold badges359 silver badges500 bronze badges

1

For item in string_list:
   # possibly do some string manipulation such as
   # item = item + '+= 1'
   eval(item)
   # or even exec(item)

Đã trả lời ngày 15 tháng 7 năm 2013 lúc 5:10Jul 15, 2013 at 5:10

Hướng dẫn how do you evaluate a list in python? - làm thế nào để bạn đánh giá một danh sách trong python?

TerryaterryaSteve Barnes

57.3K11 Huy hiệu vàng117 Huy hiệu bạc139 Huy hiệu đồng6 gold badges60 silver badges70 bronze badges

4

Có những lúc điều này có thể hữu ích

name1 = "some string"
name2 = "some other string"

listname =[name1, name2]

for s in listname:
    do something with s

Thông thường, tốt hơn là chỉ cần có một danh sách các đối tượng hoặc sử dụng một không gian tên riêng như @haidro gợi ýJul 15, 2013 at 5:27

John La Rooyjohn La RooyKeith

Phù bằng vàng 287K5110 gold badges56 silver badges75 bronze badges

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Hướng dẫn how do you evaluate a list in python? - làm thế nào để bạn đánh giá một danh sách trong python?

     Examples:

    Input : list = [10, 20, 30, 40, 50] 
            given value = 20 
    Output : No
    
    Input : list = [10, 20, 30, 40, 50] 
            given value = 5 
    Output : Yes

    Bàn luận{IDE} first, before moving on to the solution.

    Cho một danh sách, in tất cả các giá trị trong một danh sách lớn hơn giá trị đã cho & nbsp;

    Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp.

    Implementation:

    Python

    def check(list1, val):

    Phương pháp 1: Danh sách truyền tải

    Bằng cách đi qua trong danh sách, chúng ta có thể so sánh mọi yếu tố và kiểm tra xem tất cả các phần tử trong danh sách đã cho có lớn hơn giá trị đã cho hay không. & NBSP;

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0____11
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    2
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    3
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    4

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    5
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    6
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    7
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    9

    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    6
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    8
    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    9
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    1
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    3
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    5
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    7
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    9
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    0

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    1
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    3

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    6
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    5

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    8

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    9
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    0

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    3

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    1
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    for variable in listname:
        target = vars().get(variable)
    
    1

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    6
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    5

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    8

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    9
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    0

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    3

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    6
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    5

    Phương pháp 3: Sử dụng phương thức Min ()

    Implementation:

    Python

    def check(list1, val):

    Các

    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    6
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    8
    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    9
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    1
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    3
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    5
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    7
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    9
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    0

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    1
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    3

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    6
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    5

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    8

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    9
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    0

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    3

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    1
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    for variable in listname:
        target = vars().get(variable)
    
    1

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    6
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    5

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    8

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    9
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    0

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    3

    Phương pháp 3: Sử dụng phương thức Min ()

    Python3

    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    6
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    8
    d = {'name1':name1, 'name2':name2}
    
    for myvar in listname:
        myvar = d.get(myvar)
        do_stuff(myvar)
    
    9
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    1
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    3
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    5
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    7
    for variable in listname:
        target = vars().get(variable)
    
    0
    for variable in listname:
        target = vars().get(variable)
    
    9
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    0

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    1
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    3

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    6listname1
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    39
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    40
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    8
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    42

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7listname1
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    8
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    47

    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    9
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    0

    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    0
    For item in string_list:
       # possibly do some string manipulation such as
       # item = item + '+= 1'
       eval(item)
       # or even exec(item)
    
    7listname1
    name1 = "some string"
    name2 = "some other string"
    
    listname =[name1, name2]
    
    for s in listname:
        do something with s
    
    3
    for variable in listname:
        [perform some operation on the string associated with the variables 
        named in listname, i.e. "somestring" and then "some other string," etc.]
    
    47


    Làm cách nào để kiểm tra một danh sách trong Python?

    Chúng ta có thể sử dụng phương thức danh sách Python được xây dựng, Count (), để kiểm tra xem phần tử được truyền có tồn tại trong danh sách không. Nếu phần tử được truyền tồn tại trong danh sách, phương thức đếm () sẽ hiển thị số lần nó xảy ra trong toàn bộ danh sách. Nếu đó là một số dương khác không, nó có nghĩa là một yếu tố tồn tại trong danh sách.use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

    Làm thế nào để bạn nhập một danh sách trong eval?

    Đầu tiên, chúng tôi xác định một chuỗi, mang cú pháp của danh sách.Cuối cùng, chúng ta có thể chỉ ra rằng nó có các thuộc tính của danh sách Python.Một ví dụ khác, trong đó chúng tôi cho phép người dùng nhập chuỗi được đánh giá. Next, we use eval to evaluate it. Finally, we can show that it has the properties of a Python list. Another example, where we allow the user to input the string to be evaluated.

    Làm thế nào để bạn so sánh các giá trị trong danh sách Python?

    Phương thức python sort () và == Toán tử để so sánh các danh sách chúng ta có thể câu lạc bộ phương thức python sort () với toán tử == để so sánh hai danh sách.Phương thức python sort () được sử dụng để sắp xếp các danh sách đầu vào với mục đích nếu hai danh sách đầu vào bằng nhau, thì các phần tử sẽ nằm ở cùng một vị trí chỉ mục. to compare lists We can club the Python sort() method with the == operator to compare two lists. Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

    Làm thế nào để bạn đếm các mục trong một danh sách trong Python?

    Sử dụng hàm Len () để có được số lượng các phần tử chúng ta có thể sử dụng hàm Len () để trả về số lượng các phần tử có trong danh sách. to Get the Number of Elements We can use the len( ) function to return the number of elements present in the list.