简体   繁体   English

在python中打印列表的第3和第4项

[英]Print alternate 3rd and 4th items of a list in python

Suppose I have the below list in python: myList = ['a','b','c','d','e','f','g','h','i',1,2,3] 假设我在python中具有以下列表:myList = ['a','b','c','d','e','f','g','h','i',1, 2,3]

Now, i basically want a function which will take the list and a enter code here range as input and print below as the output: 现在,我基本上想要一个将列表和一个enter code here作为输入并在下面输出作为输出的函数:

['c','g',1] ['c','g',1]

def alternate_num(l,r):
    if r >=3 :
        if r <= len(myList):
            myResult = l[0:r]
            first_loc = myResult.index(myResult[2])
            second_loc = myResult.index(myResult[6])
            newList = list(myResult[first_loc:second_loc + 1:4])
            len_list = len(myResult)
            print(newList)
            newList.append(myResult[i] for i in first_loc)
            print(newList)
        else:
            print("The range provided is greater than the length of the list")
    else:
        print("Please provide a range greater than 3")

eg suppose i have given a range input as 10, in that case it will take first the 3rd element from the list, then next,it should pick up the alternate 4th element and then it should the next 3rd element and so on. 例如,假设我给定一个范围输入为10,在这种情况下,它将首先从列表中获取第3个元素,然后是下一个,它应该获取替代的第4个元素,然后应该获取下一个第3个元素,依此类推。

so, in this example, my third element is c, hence it is picked up, then it iterates through the list and pick the alternate fourth element ie g and then it picked the alternate 3rd element which is 1. 因此,在此示例中,我的第三个元素是c,因此被选中,然后遍历列表并选择另一个第四个元素g,然后选择了另一个第三个元素1。

you can use itertools.cycle and itertools.accumulate to get a generator that yields the desired indexes. 您可以使用itertools.cycleitertools.accumulate获得生成所需索引的生成器。

try this: 尝试这个:

myList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 1, 2, 3]


def alternate_num(l, r):
    if r < 3:
        print("Please provide a range greater than 3")
        return
    if r > len(l):
        print("The range provided is greater than the length of the list")
        return
    from itertools import cycle, accumulate
    indexes = accumulate(cycle([3, 4]))
    new_list = []
    for i in indexes:
        if i > r:
            return new_list
        new_list.append(l[i-1])


print(alternate_num(myList, 10))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,一起添加 CSV 文件的第 3 行和第 4 行 - Python, Adding 3rd and 4th rows of a CSV file together 来自 python 列表的子列表,使得第一个子列表包含列表的前 2 个元素,然后是前 3 个元素,依此类推,Num 为第 3 个,第 4 个,依此类推 - Sublists from a python list such that first sublist contains first 2 elements of list, then first 3 elements and so on and Num as 3rd, 4th and so on 在 Python 的邮政编码的第 3 和第 4 个字符之间放置一个空格 - putting a space between the 3rd and 4th characters of postal code in Python Python处理日期格式,例如“ 1、2、3、4” - Python deal with date format like “1st, 2nd, 3rd, 4th” Python散点图中用颜色编码的第三和第四列 - Color-coded 3rd and 4th columns in Python scatter plot Python 比较 2 列并用第 3 列中的值写入第 4 列(熊猫) - Python Compare 2 Columns And Write A 4th Column With Values From 3rd Column (pandas ) 从数字中找出第 3 和第 4 位数字 - Find the 3rd & 4th digits from a number Writeline 不写入第 3 行和第 4 行 - Writeline doesn't write to 3rd and 4th lines 来自txt(csv)文件的Python图形,共有5列,如何为y轴选择第3列或第4列? - Python graph from txt(csv) file and there are 5 columns, How Can I pick 3rd or 4th columns for y axis? Python / R:如果2列在多行中具有相同的值,请在第3列中添加值,然后对第4、5和6列取平均值 - Python/R : If 2 columns have same value in multiple rows, add the values in the 3rd column and average the 4th, 5th and 6th column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM