2020-1-17(78)

本文深入探讨了多个复杂的编程挑战,包括寻找数组中特殊元素的算法、确定子数组的最大和、识别唯一元素及重复项,同时展示了如何处理字符串回文判断、文件操作等实用技巧。文章通过具体代码实例,提供了理解和解决这些问题的有效途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#261、在一个给定的数组nums中,总是存在一个最大元素 。
#查找数组中的最大元素是否至少是数组中每个其他数字的两倍。
#如果是,则返回最大元素的索引,否则返回-1。

def is_double_max_num(l):
    max_num=max(l)  #最大元素
    max_num_index=l.index(max_num)
    l.remove(max_num)  #将最大元素从列表中删除
    
    for num in l:
        if max_num<num*2:
            return -1
    return max_num_index
    
print(is_double_max_num([3, 6, 1, 0]))
print(is_double_max_num([1, 2, 3, 4]))

#262、给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),
#返回其最大和。

def max_sum_sub_arr(l):
    sub_arr=[]
    for i in range(len(l)):
        for j in range(len(l)-i):
            sub_arr.append(l[i:i+j+1])
    print("全部子数组为",sub_arr)
    result=(sorted(sub_arr,key=sum))[-1]
    print("最大和的连续子数组为",result)
    
    return sum(result)
    
print(max_sum_sub_arr( [-2,1,-3,4,-1,2,1,-5,4]))

#263、给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。
#找出那个只出现了一次的元素。

def find_one_time_ele(l):
    for i in l:
        if l.count(i)==1:
            return i
        
print(find_one_time_ele([2,2,3,2]))

#264、给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),
#可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数

l=[1,3,4,2,2]
for i in l:
    if l.count(i)>1:
        result=i
print(result)

#遍历列表,把每个元素加入字典,如果加入后字典长度没有加1的话,则返回元素

l=[1,3,4,2,2]
d={}
for i in l:
    d_len=len(d)
    d[i]=1
    if len(d)!=d_len+1:
        print(i)

#264、给定一组唯一的单词, 找出所有不同的索引对(i, j),使得列表中的两个单词,
#words[i] + words[j] ,可拼接成回文串。

#words_list=["abcd","dcba","lls","s","sssll"]
words_list=["bat","tab","cat"]
result=[]
for i in range(len(words_list)):
    for j in range(len(words_list)):
        word=words_list[i]+words_list[j]
        if word[:]==word[::-1]:
            result.append([i,j])
print(result)

#265、文件过滤,显式一个文件的所有行,忽略(#)开头的行

with open("d:\\2019\\1.txt") as fp:
    for line in fp:
        if line[0]!="#":
            print(line)

#266、文件访问,提示输入数字N和文件F,然后显式文件F的前N行

try:
    F=input("请输入文件路径:")
    N=int(input("请输入需要显示的行数:"))
    with open(F) as fp:
        lines=fp.readlines()
    for i in range(len(lines)):
        if i==N:
            break
        else:
            print(lines[i])
except Exception as e:
    print(e)

#267、文件信息,提示输入一个文件名,然后显式这个文本文件的总行数

try:
    F=input("请输入文件路径:")
    with open(F) as fp:
        lines=fp.readlines()
    print(len(lines))
except Exception as e:
    print(e)

#268、写一个逐页显示文本文件的程序。
#提示输入一个文件名,每次显示文本文件的25行,暂停并向用户提示“按任意键继续”,按键后继续执行

n = 0
with open("d:\\2019\\1.txt") as file_obj:
    for line in file_obj:
        n += 1
        if n <= 25:
            print(line)
        else:
            
            ins = input("请按任意键继续.")
            if ins is not None:
                n = 0
[{'year': [{'2019': -65419.62, '2018': 30498.15, '2022': 13876.93, 'index': '经营活动产生的现金流量净额', '2021': 42973.8, '2020': 39619.51}, {'2019': 107400.65, '2018': 116614.64, '2022': -1181.36, 'index': '投资活动产生的现金流量净额', '2021': 47348.05, '2020': 20575.82}, {'2019': 12626.08, '2018': -189296.55, '2022': -19400.66, 'index': '筹资活动产生的现金流量净额', '2021': -93035.11, '2020': -123532.65}], 'middle': [{'2019': -54828.01, '2018': -497.32, '2022': 10639.37, 'index': '经营活动产生的现金流量净额', '2021': 23457.13, '2020': 14095.79}, {'2019': 70222.58, '2018': 9137.32, '2022': -822.17, 'index': '投资活动产生的现金流量净额', '2021': 49199.63, '2020': 14493.25}, {'2019': -10674.67, '2018': -27426.89, '2022': -11163.9, 'index': '筹资活动产生的现金流量净额', '2021': -64884.77, '2020': -84514.8}], 'one': [{'2019': -2054.79, '2023': -978.88, '2022': 6060.59, 'index': '经营活动产生的现金流量净额', '2021': 17091.63, '2020': -13929.39}, {'2019': -46.08, '2023': -622.78, '2022': -387.47, 'index': '投资活动产生的现金流量净额', '2021': 37272.44, '2020': -2023.12}, {'2019': 14477.87, '2023': 2171.58, '2022': -3195.9, 'index': '筹资活动产生的现金流量净额', '2021': -11139.03, '2020': -34388.22}], 'three': [{'2019': -59111.13, '2018': 15934.74, '2022': 13696.67, 'index': '经营活动产生的现金流量净额', '2021': 36815.74, '2020': 21584.93}, {'2019': 61471.57, '2018': 14192.29, '2022': -696.12, 'index': '投资活动产生的现金流量净额', '2021': 45656.4, '2020': 26267.88}, {'2019': 55374.45, '2018': -58583.67, '2022': -15444.32, 'index': '筹资活动产生的现金流量净额', '2021': -85809.77, '2020': -103091.12}]}]用pandas写到一个excel
06-19
可以使用 pandas 的 DataFrame 和 ExcelWriter,将数据写入 Excel 文件。以下是示例代码: ```python import pandas as pd # 定义数据 data = [{'year': [{'2019': -65419.62, '2018': 30498.15, '2022': 13876.93, 'index': '经营活动产生的现金流量净额', '2021': 42973.8, '2020': 39619.51}, {'2019': 107400.65, '2018': 116614.64, '2022': -1181.36, 'index': '投资活动产生的现金流量净额', '2021': 47348.05, '2020': 20575.82}, {'2019': 12626.08, '2018': -189296.55, '2022': -19400.66, 'index': '筹资活动产生的现金流量净额', '2021': -93035.11, '2020': -123532.65}], 'middle': [{'2019': -54828.01, '2018': -497.32, '2022': 10639.37, 'index': '经营活动产生的现金流量净额', '2021': 23457.13, '2020': 14095.79}, {'2019': 70222.58, '2018': 9137.32, '2022': -822.17, 'index': '投资活动产生的现金流量净额', '2021': 49199.63, '2020': 14493.25}, {'2019': -10674.67, '2018': -27426.89, '2022': -11163.9, 'index': '筹资活动产生的现金流量净额', '2021': -64884.77, '2020': -84514.8}], 'one': [{'2019': -2054.79, '2023': -978.88, '2022': 6060.59, 'index': '经营活动产生的现金流量净额', '2021': 17091.63, '2020': -13929.39}, {'2019': -46.08, '2023': -622.78, '2022': -387.47, 'index': '投资活动产生的现金流量净额', '2021': 37272.44, '2020': -2023.12}, {'2019': 14477.87, '2023': 2171.58, '2022': -3195.9, 'index': '筹资活动产生的现金流量净额', '2021': -11139.03, '2020': -34388.22}], 'three': [{'2019': -59111.13, '2018': 15934.74, '2022': 13696.67, 'index': '经营活动产生的现金流量净额', '2021': 36815.74, '2020': 21584.93}, {'2019': 61471.57, '2018': 14192.29, '2022': -696.12, 'index': '投资活动产生的现金流量净额', '2021': 45656.4, '2020': 26267.88}, {'2019': 55374.45, '2018': -58583.67, '2022': -15444.32, 'index': '筹资活动产生的现金流量净额', '2021': -85809.77, '2020': -103091.12}]}] # 定义 ExcelWriter writer = pd.ExcelWriter('output.xlsx') # 遍历数据 for i, d in enumerate(data): # 转换为 DataFrame df = pd.DataFrame(d) # 写入 Excel df.to_excel(writer, sheet_name=f"Sheet{i+1}", index=False) # 保存 Excel 文件 writer.save() ``` 以上代码将数据写入名为 `output.xlsx` 的 Excel 文件中,每个 sheet 表示一个字典。可以根据实际需求修改文件名和 sheet 名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值