<p>如果您只需要一个<code>set</code>,那么您不需要<code>a, b, c, d</code></p>
<pre><code>all = set() #or all = {*(),}
for _ in range(4):
all.add(input())
print(all)
</code></pre>
<p>或者</p>
<pre><code>all = {input() for _ in range(4)}
</code></pre>
<p>这是考虑到你在新的一行的输入。否则,如果输入以逗号分隔,例如:</p>
<pre><code>all = set(input().split(','))
print(all)
</code></pre>
<p>或</p>
<pre><code>all = {*input().split(',')}
print(all)
</code></pre>
<p>如果同时需要<code>a, b, c, d</code>和所有输入,可以执行以下操作:</p>
<pre><code>>>> all = a, b, c, d = {*input().split(',')}
# example
>>> all = a, b, c, d = {1, 2, 3, 4}
>>> all
{1, 2, 3, 4}
>>> a
1
>>> b
2
</code></pre>
<p>正如<a href="https://stackoverflow.com/questions/60136212/how-to-store-many-user-input-in-a-python-set/60136251#comment106360567_60136251">@Tomerikoo</a><code>all(iterable)</code>所指出的,是一种<a href="https://docs.python.org/3/library/functions.html#all" rel="nofollow noreferrer">built-in function</a>避免将变量命名为与python构建或关键字相同的变量。
另一点,如果您已经这样做了,为了获得所有人的默认行为,您可以:</p>
<pre><code>>>> import builtins
>>> all = builtins.all
# Or, more conveniently, as pointed out by @Martijn Pieters
>>> del all
</code></pre>
<ul>
<li><code>*</code>用于<a href="https://www.python.org/dev/peps/pep-3132/" rel="nofollow noreferrer">^{<cd6>}</a></li>
<li><code>_</code>用于<code>don't care</code>或<code>throwaway</code>或<code>anonymous variable</code>,
因为我们不需要循环中的变量。更多关于这个
<a href="https://stackoverflow.com/a/5893946/5431791">here</a></李>
<li><a href="https://stackoverflow.com/a/47180500/5431791">^{<cd11>}</a>只是创建空集的一种奇特方式,因为python没有空集文本。建议使用<code>set()</code></li>
</ul>