2024 4 9 python作业

文章讲述了Python中基本的字符串操作,如循环、存储多字符串、转换大小写、替换特定字符,以及列表的使用技巧,包括添加元素、查找索引、去重和排序。还涉及了概率计算和字符串格式化。
摘要由CSDN通过智能技术生成

1.

Please input multiple strings through the keyboard, store them in the list, and then output the new string. At the same time, find the starting position of the given string. If not found, output “can not find”. (code + result screenshot)

Such as:

Input:4

     hello

     world

     hello

     students

Output:hello world hello students

Input: hello

Output:0

思路:期望通过次数的循环来输入字符

问题1:for循环中如何进行次数的循环

解决:for ()in range 一个范围

问题2:如何在储存多个字符串

解决:可以通过列表string来定义

问题3:如何定义一个空的列表

解决:定义一个空列表要用方括号来表示

eg.

empty_list = [] # 使用空的方括号定义一个空列表

问题4:当我转换空列表时

x=int(input('input:'))
string=[]
for i in range(0,x):
    string[i]=input()

无法通过这种方式来像列表内部进行输出

解决:应该用append函数来将字符串放在字符串的后方,注意append接收的时字符串

问题4:

x=int(input('input:'))
string=[]
for i in range(x):
    str_list=input("input:".format(i+1))
    string.append=(str_list)

这个代码报错;

解决:string.append与括号之间不应该有=,第一行中x和=和int之间要有空格

x = int(input('input:'))
string = []
for i in range(x):
    str_list = input("".format(i+1))
    string.append(str_list)

print("the line:")

for a in string:
    print(a)


修改版

问题5:如何使最后的输出出现在同一行

解决: print(a,end=' ')

问题6:如何判断对应的字符处于列表中的哪一个位置

解决:可以使用index()来获取对应括号内的元素处于列表的哪一个位置

6.5:b = print("Input:")
index = string.index(b)

为什么上面的这中方式是错误的

解决:低级错误应该是b=input()

问题7:

str_list = input("".format(i+1))

这里的format起到了什么作用,为什么.format()前面一定要加上”“

似懂非懂:format用来格式化字符串

2、There is a string in which all the letters x are written into X. Please correct the error, where the wrong letter is input from the keyboard.(code + result screenshot)

Such as:

Input: n

Input: LearNing makes me learN how to deal with problems.

Output: Learning makes me learn how to deal with problems.

问题1:如何将字符串中的大写字母转换为小写字母

解决:string.lower()

 问题2:不是将所有的字母全换成小写而是替换其中的某几个单词

解决:eg.

original_string = "Hello World"
new_string = original_string.replace('l', 'x')
print(new_string)  # 输出:Hexxo Worxd
 

 最终版:

letter=input("input the letter")
original_string=input("Input the  string:")
new_string=original_string.replace(letter,letter.lower())
print("Output:",new_string)

3、Please enter a string and output all words of length n in this string. (It is recommended to use the maketrans and translate functions together, or you have other better methods.) (Code+result screenshot)

Such as:

Input: hello world, nice to see you.

Input:5

Output: hello  world

问题1:

maketrans and translate functions这两个是什么函数。

gpt:

def find_words_of_length(s, n):
    # 将字符串分割成单词列表
    words = s.split()
    # 遍历单词列表,检查每个单词的长度
    words_of_length_n = [word for word in words if len(word) == n]
    return words_of_length_n

# 从用户那里获取输入
input_string = input("请输入一个字符串:")
word_length = int(input("请输入你想要查找的单词长度:"))

# 调用函数并输出结果
result = find_words_of_length(input_string, word_length)
print("长度为 {} 的单词有:".format(word_length), result)

问题1:s.split()是什么意思

解决:split()可以将字符串分割成单词列表

问题2: words_of_length_n =[word for word in words if len(word) == n]这里为什么要有方括号

解决:

[word]:如果条件为真(即单词的长度等于 n),则将当前单词 word 添加到新的列表中。注意这里的 [word] 实际上创建了一个包含单个元素 word 的列表。

4、Enter a string from the keyboard, enter the substring to be replaced and the replacement rule, and output the replaced string. (Code+result screenshot)

For example:

Input:

this py, it is a dif-py, hello pypy.

Input:

Please enter the substring to replace and the replacement rule:

py python

Output:

The replaced string is:

this python, it is a dif-python, hello pythonpython.

原始版本:

original_string=input("the orginal string")
rule=list(input("Please enter the substring to replace and the replacement rule:"))
new_string=original_string.replace(list[0],list[1])
print(new_string)

问题1:该怎么改才能正确的替换

将列表去掉直接返回list字符串,并用split进行分割。

original_string=input("the orginal string")
rule=input("Please enter the substring to replace and the replacement rule:").split()
new_string=original_string.replace(rule[0],rule[1])
print(new_string)

5. Write a program: input a string from the keyboard, and count the number of occurrences of each character (note: the number of occurrences before the decimal point represents the number of occurrences, and the number of occurrences after the decimal point represents the probability of occurrence! Secondly, letters are not case-sensitive, and blank characters are ignored). (Code+result screenshot)

For example:

Input:

hello hi nice you!.!

Output:

{'h': 2, 'e': 2, 'l': 2, 'o': 2, 'i': 2, 'n': 1, 'c': 1, 'y': 1, 'u': 1, '!': 2, '.': 1}

{'h': '2.12', 'e': '2.12', 'l': '2.12', 'o': '2.12', 'i': '2.12', 'n': '1.06', 'c': '1.06', 'y': '1.06', 'u': '1.06', '!': '2.12', '.': '1.06'}

6.Write a program that takes a series of words separated by spaces as input and prints them after deleting all duplicate words and sorting them. (Code+result screenshot)

For example:

Input:

hello world and practice makes perfect and hello world again

Output

again and hello makes perfect practice world

input_words = input("Enter a series of words separated by spaces: ")

words_list = input_words.split()

unique_sorted_words = sorted(set(words_list))

print("Unique and sorted words:")

print(' '.join(unique_sorted_words))

知识点:

1.sorted() 函数按照升序排序元素,但你也可以通过参数指定降序排序。sorted是默认升序排列。

2.set 是一个内置的数据类型,它存储了一个无序的集合对象。与列表和元组不同,集合中的元素是唯一的,这意味着集合会自动去除重复的元素。set会自动删除重复的元素。

3.问题:为什么不直接print()

这样做的原因是,unique_sorted_words 是一个列表,而 print 函数需要一个字符串作为输入。如果你直接打印一个列表(不使用 join 方法),Python 会输出列表的内存地址表示,而不是列表内容,这通常不是我们想要的结果。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值