[英]How to preserve the order of elements in list while converting it to set in Python and how to implement `scanf()` in Python?
Actually I have two questions:其实我有两个问题:
>>>set([7, 10, 78, 96, 13, 42, 88, 7, 12, 16])
{96, 7, 10, 42, 12, 13, 78, 16, 88}
The order was lost during conversion.订单在转换过程中丢失。 How to preserve the order?如何维护订单?
For example, if the code was like: n = int(input())
and if the input given was just <Enter>
but the next line contains the actual input how to wait until a valid input is obtained?例如,如果代码是这样的: n = int(input())
并且如果给出的输入只是<Enter>
但下一行包含实际输入,如何等到获得有效输入?
I mean, I want like this as in C
language.我的意思是,我想要像C
语言那样。 If you write the code like scanf("%d",&n);
如果你写像scanf("%d",&n);
这样的代码then it will ignore all the spaces and Enter key presses and will get the actual input.然后它将忽略所有空格和 Enter 按键并获得实际输入。 Also, there is another feature like getting the time like scanf("%d:%d", &h, &m);
此外,还有另一个功能,例如获取时间,例如scanf("%d:%d", &h, &m);
which actually allows :
as an input that separates two different inputs.这实际上允许:
作为分隔两个不同输入的输入。
I tried the input in two different ways: First:我以两种不同的方式尝试了输入:首先:
>>>n = int(input())
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
n = int(input())
ValueError: invalid literal for int() with base 10: ''
Second:第二:
>>> from sys import stdin
>>> n = int(stdin.readline())
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
n = int(stdin.readline())
ValueError: invalid literal for int() with base 10: '\n'
The only noticeable difference is that stdin
captures the return key while input()
just captures it as an empty string.唯一明显的区别是stdin
捕获返回键,而input()
只是将其捕获为空字符串。 But none of them implements the scanf()
function as explained above.但是它们都没有实现如上所述的scanf()
function。 How to implement this?如何实施?
In order to preserve the order you cannot use the set
object.But you can use frozenset
.Frozen set is just an immutable version of a Python set object.为了保持顺序,您不能使用set
object。但是您可以使用frozenset
。Frozen 集合只是 Python 集合 object 的不可变版本。 So it goes like this,所以事情是这样的,
>>> a = [1,2,3,4,5]
>>> b = frozenset(a)
>>> print(a)
[1, 2, 3, 4, 5]
>>> print(b)
frozenset({1, 2, 3, 4, 5})
For your second issue I suggest using this code.It doesn't break from the loop until an integer is given.对于您的第二个问题,我建议使用此代码。在给出 integer 之前,它不会从循环中中断。
>>> while 1:
try:
x = int(input().strip())
break
except ValueError:
pass
string.strip() is used to remove all leading and trailing whitespaces in a python string.It still works without string.strip() though. string.strip() 用于删除 python 字符串中的所有前导和尾随空格。但它仍然可以在没有 string.strip() 的情况下工作。 But it doesn't account for any '\n' of string values and loops until an integer is given.但在给出 integer 之前,它不考虑任何 '\n' 字符串值和循环。
In order to seperate the inputs by the ":" character in them you can simply use string.split(":")
.It returns a string with the values seperated.为了通过其中的“:”字符分隔输入,您可以简单地使用string.split(":")
它返回一个带有分隔值的字符串。
>>> a = "1:2:3"
>>> b = a.split(":")
>>> print(b)
['1', '2', '3']
Hope this helps.希望这可以帮助。
Sets do not have an order (or index).集合没有顺序(或索引)。 Assuming you're converting a list to a set to remove duplicates (and converting it back to a list without sorting), convert the list to NumPy array to remove duplicates.假设您要将列表转换为集合以删除重复项(并将其转换回列表而不进行排序),请将列表转换为 NumPy 数组以删除重复项。
>>> l = [2, 5, 10, 6, 3, 10, 5, 7]
>>> l = np.array(l)
>>> unique_index = np.unique(l, return_index = True)[-1]
>>> unique_l = [l[i] for i in sorted(unique_index)]
>>> unique_l
[2, 5, 10, 6, 3, 7]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.