简体   繁体   中英

How to print out certain elements of a list in python?

So I have a list li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] and I only want to print out elements that are a part of an arithmetic sequence 6n - 5 (1st, 7th and 13th).

How can I do that if I have a list with n elements?

You could simply say

print([x for x in li if x % 6 == 1])

or, alternatively, if you just want the sequence and don't want to bother about creating li in the first place,

print([6*n-5 for n in range(1, (13+5)//6+1)])

Use the code:

li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 
new=[]
for i in li:
    if int((i+5)/6)==((i+5)/6): 
    #You can also use
    #if ((i+5)/6).is_integer():
         new.append(i)

I tried to make it as easy as possible.

Hope it helps :)

li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for n in range(1,len(li)+1): #choose n such that it is length of the list since it cant have more values than the number of values in the list.
    for i in li:
        if (6*n - 5) == i:
           print(i)

Hope this helps

Thanks

Michael

From what I understand, you want the elements whose positions are generated by the sequence. Hence, you want elements from an array of length n whose index is from the sequence function 6x-5 .

NOTE : I am assuming you are using 1-based indexing, which means, when you say 1st element in your list, you intend to get 1 and not 2 .

n = 13
li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

# generate the sequence till n
seq = []
x = 1
while 6*x-5 <= n:
    seq.append(6*x-5)
    x += 1

# Print the elements from the sequence:
for idx in seq:
    print(li[idx-1])

# If you want to store it in another list:
li2 = [li[idx-1] for idx in seq]

Below is a more generic and efficient way for above code:

n = 13
li = list(range(1, n+1)) # More easy to write

# More efficient way is to create a generator function
def get_seq(n):
    x = 1
    while 6*x-5 <= n:
        yield 6*x-5
        x += 1

# Get the generator object
seq = get_seq(n)

# Print the elements from the sequence:
for idx in seq:
    print(li[idx-1])

# Want to store it in another list:
seq = get_seq(n) # Don't forget to get a new generator object.
li2 = [li[idx-1] for idx in seq]

Output for both snippets:

1
7
13

Hope the answer helps, and remove confusion for others as well ;)

You can simply generate the sequence for any n.

for example:

n = 10
print([ 6*x - 5 for x in range(1,n)])

output:

[1, 7, 13, 19, 25, 31, 37, 43, 49]
>>>  [Finished in 0.2s]

But if you just want to filter your existing list li:

li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
print([ x for x in li if x % 6 == 1 ])

output:

[1, 7, 13]
>>> 
[Finished in 0.3s]

li[1] is 2, li[7] is 8 and the element with the index 13 is out of range.

li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 

for num, i in enumerate(li):
   if ((num + 5)/6).is_integer():
       print(i)
# 2
# 8

If you want to start with the index 1 add start=1 to the enumerate() function.

for num, i in enumerate(li, start=1):
   if ((num + 5)/6).is_integer():
       print(i)
# 1
# 7
# 13

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM